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
Functionreturning anotherFunction(validator-child), itself returning aboolean. - Any validator-child should return
trueif the condition is met, andfalseif 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. VLengthyis here to represent a value that might have alengthproperty, it is not required, and only useful in this case as we are using thelengthproperty.
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;
}