svelte-validator
Install
This is WIP project. Use at your own responsibility!
npm i -S svelte-validator
Usage
{#if 'required' in $errorStore} {$errorStore.required.message} {/if} {#if 'minLength' in $errorStore} {$errorStore.minLength.message} {/if} {#if 'notEqual' in $errorStore} {$errorStore.notEqual.message} {/if} Submit
svelteValidator.create
Options
rules
An array of validators.
initial
Initial value of valueStore
.
immediate
If false
, validation does not run until calling valueStore.activate()
. Default true
.
For example this can be used to prevent from displaying errors until first blur event occurs.
Builtin Validators
required(error)
equal(value, error)
minValue(min, error)
maxValue(max, error)
betweenValue([min, max], error)
minLength(length, error)
maxLength(length, error)
betweenLength([min, max], error)
format(regex, error)
not(validator)
error
argument
Arbitrary error value.
const valueStore errorStore = svelteValidator $errorStorerequiredfoo // === 'bar'
Custom Rule
You can implement your own validation rule. It should be an object which has name
, isValid
and error
properties.
const myRule = name: 'myRule' { // true or false } error: message: '...' color: 'red' const valueStore errorStore = svelteValidator// $errorStore.myRule appears when value violates the rule.