Damn simple value checker for Svelte. Works well with forms
npm i -D @bztes/svelte-ds-validator
yarn add -D @bztes/svelte-ds-validator
<script>
import { and, createChecker, email, number, required } from '@bztes/svelte-ds-validator';
export let data;
// apply validation rules
const checker = createChecker({
fields: {
email: {
value: () => data.email,
rule: and(required(), email()),
},
age: {
value: () => data.age,
rule: and(required(), number({ min: 0, max: 130, int: true })),
},
message: {
value: () => data.message,
// Default rule can be skipped
// rule: required(),
},
},
});
// validate on data changed
$: data, checker.validate();
</script>
<form>
<p>
<label for="email">E-Mail</label>
<input type="email" name="email" bind:value={data.email} />
<span>{$checker.fields.email.error}</span>
</p>
<p>
<label for="age">Age</label>
<input type="number" name="age" bind:value={data.age} />
<span>{$checker.fields.age.error}</span>
</p>
<p>
<label for="message">Message</label>
<textarea name="message" bind:value={data.message} />
<span>{$checker.fields.message.error}</span>
</p>
<p>
<button type="submit" disabled={!$checker.valid}>Send</button>
</p>
</form>
const checker = createChecker({
defaultRule: ...
fields: {
[field_name]: {
value: () => ...
rule: ...
}
}
});
defaultRule (Optional)
A default rule that will be used by all checker fields where no specified rule is defined
fields.[].rule (Optional)
The rule to be checked. Use and()
or or()
to combine rules. If no rule is provided checker.defaultRule
, settings.defaultRule
or required()
is used (in this order).
fields.[].value()
The function that provides the input value to be checked
<script>
...
const checker = ...
// validate on data changed
$: data, checker.validate();
</script>
<form>
<p>
<label for="message">Message</label>
<textarea name="message" bind:value={data.message} />
<span>{$checker.fields.message.error}</span>
</p>
<p>
<button type="submit" disabled={!$checker.valid}>Send</button>
</p>
</form>
checker.validate()
Triggers the validation. You probably want to call this function after the input has changed
$checker.fields.[].error
Contains the error message for the individual fields if the input is invalid, null
otherwise
$checker.fields.[].validtrue
if the specific input value is valid, false
otherwise
$checker.validtrue
if all input values are valid, false
otherwise
const settings = {
fields: {
userMail: {
rule: email(),
},
},
};
const options = {
min: 18,
msg = {
numberToSmall: 'adults only',
},
};
rule = number(options);
number.Options.msg.numberToSmall = 'adults only';
Static
rule = {
validate(input): true | string
value(fieldValue)?: any
error?: string
}
validate
Validation function that takes an input value and returns true on validation success or an error message otherwise
value(input) (Optional)
Function that can be used to provide a rule specific input value for validate(). If undefined the field value will be used as an input
error (Optional)
Can be used to provide a rule specific error message. If undefined the return value from validate will be used as error message
const isTrue = {
validate: (input) => input === true || 'Input value must be true',
};
With parameters
const equals = (value) => ({
validate: (input) => value == input || 'Invalid value',
});
const checker = createChecker({
fields: {
legal: {
value: () => data.legal,
rule: { ...equals(true), error: 'Legal rules have to be accepted' },
},
},
});
let message = { response: 'Succeed', error: null };
const checker = createChecker({
fields: {
message: {
value: () => message,
rule: and({ ...required(), value: (v) => v.response }, { ...falsy(), value: (v) => v.error }),
},
},
});