Custom validation rules
Use the public synchronous ValidationSchema.custom predicate for field-level
HTTP validation. Return true for success, false or an error message for failure.
import assert from 'node:assert/strict';
import type { ValidationSchema } from '@tagna/udiot';
const code: ValidationSchema = {
type: 'string',
required: true,
custom: value => /^[A-Z]{3}$/.test(value) || 'Use three uppercase letters',
};
assert.equal(code.custom?.('ABC'), true);
assert.equal(code.custom?.('abc'), 'Use three uppercase letters');
For forms, use a validationRules helper or an application-owned object with
validate(value): boolean and message: string. Do not pass async callbacks
to synchronous rules. Keep regular expressions bounded and reviewed for hostile
input.
The internal natural-language parser, registerPattern and
registerFieldPattern are not public registry extension points. Unknown
natural-language text must not be used as a security validation rule.
From the canonical UDIOT public documentation.Try it in the playground ↗