Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sveltekit-yup

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sveltekit-yup

Svelte component library using Yup for form validation.

  • 0.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

sveltekit-yup

npm NPM GitHub top language npm

Svelte component library using Yup for form validation.

An update has been made for the svelte-yup project sveltekit.

Installation

$ npm i sveltekit-yup --save-dev
$ npm i yup

Sample code

<script>
    import * as yup from 'yup';
    import {YupForm, YupMessage} from 'sveltekit-yup';
    let schema = yup.object().shape({
        name: yup.string().required().max(30).label("Name"),
        email: yup.string().required().email().label("Email address"),
    });
    let fields = {email: "", name: ""};
    let submitted = false;
    let isValid;
    function formSubmit(){
        submitted = true;
        isValid = schema.isValidSync(fields);
        if(isValid){
            alert('Everything is validated!');
        }
    }
</script>

<YupForm class="form" {schema} {fields} submitHandler={formSubmit} {submitted}>
    <input type="text" bind:value={fields.name} placeholder="Name">
    <YupMessage name="name" />
    <input type="text" bind:value={fields.email} placeholder="Email address">
    <YupMessage name="email" />
    <button type="submit">Submit</button>
</YupForm>


Add isInvalid for making input border style.

Example:

<script>
...
import {YupMessage, isInvalid} from 'sveltekit-yup';
...
$: invalid = (name)=>{
    if(submitted){
        return isInvalid(schema, name, fields);
    }
    return false;
}
...
</script>

<input type="text" class:invalid={invalid("name")} bind:value={fields.name} placeholder="Name">
<style>
.invalid {
    border-color: red !important;
}
</style>

All messages in one place

Example below to put all messages in one place by YupAllMessages component.

import {YupAllMessages} from 'sveltekit-yup';
<YupAllMessages />

Custom Message color

<YupForm ... color="#b00020">

Components

namepropsdescription
YupFormschema, fields, submitHandler, submitted and color
YupMessagenameError message of a field name by name prop. name should be the schema field name (no label name)
YupAllMessagesPuts all field messages in one place

Functions

isInvalid(schema:Object, name:String, fields:Object)

Example disable button until everything is validated

...
let btnDisabled = false;
$: if(submitted){
    btnDisabled = true;
    isValid = schema.isValidSync(fields);
    if(isValid){
        btnDisabled = false;
    }
}
...
<button type="submit" disabled={btnDisabled}>Submit</button>

Author

Emrullah TUNCAY

License

MIT

Keywords

FAQs

Package last updated on 15 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc