Validators
Learn how to create custom validators.
๐ Do not hesitate to create PRs with your custom validators!
Anatomy of a validator
Definition / Rules
- A validator consist of a
Function
returning anotherFunction
(validator-child), itself returning aboolean
. - Any validator-child should return
true
if the condition is met, andfalse
if not. - ๐จ A validator should never crash, but rather return false if an invalid input is given.
๐ค Best practice is to store one validator per file, and export it by default (
export default
).Notes
- In the example below, we used type helpers from
vuito/lib/types
. VLengthy
is here to represent a value that might have alength
property, it is not required, and only useful in this case as we are using thelength
property.
Example
More examples in the validators directory of vuito core.
maxLength.ts
import type { VLengthy, VRow } from 'vuito/lib/types';
export default function (max: number): VRow['test'] {
return (value: VLengthy) => !value || value.length <= max;
}