Custom modifier
A modifier which creates a validation rule to ensure that a a custom assertion is true.
If you have specific needs that are not met by existing modifiers, the custom
modifier could be something to reach for. It works with all of the primary schema types, except object
.
Schemas
boolean
: Learn more.date
: Learn more.number
: Learn more.regex
: Learn more.string
: Learn more.array
: Learn more.
Arguments
assertions
: A tuple consisting of an assertion and the related error.
Example
import { custom, string, ValidationError } from '@nordic-ui/validathor';
const customRule = string([custom((value) =>
[[value.toLowerCase() === value, new ValidationError('Lowercase only')]]
)]);
try {
const parsedValue = customRule.parse('only_lowercase_chars');
console.log('Parsed value:', parsedValue);
} catch (error) {
console.error('Parsing failed:', error.message);
}