validation-adapters
v0.0.17
Published
Pre-made validation adapters for the validation-adapter library, including adapters for Zod and Valibot
Downloads
2,140
Readme
Status: Experimental
validation-adapters
is a collection of pre-made adapters for popular validation libraries like Zod and Valibot, allowing for easy integration with the validation-adapter
library.
📖 Usage
import * as v from 'valibot';
import { createValidator } from 'validation-adapter';
import { vValidator } from 'validation-adapters/valibot';
import { zValidator } from 'validation-adapters/zod';
import * as z from 'zod';
const zodNameValidator = zValidator(
z
.string()
.min(2)
.max(10)
.regex(/^([^0-9]*)$/)
);
const valibotNameValidator = vValidator(
v.pipe(v.string(), v.minLength(2), v.maxLength(10), v.regex(/^([^0-9]*)$/))
);
const customValidator = createValidator([
{
key: 'custom',
validate: (cx) => {
if (cx.value !== 'CustomValue') {
cx.registerError({
code: 'custom-error',
message: 'Value must be CustomValue.'
});
}
}
}
]);
const combinedValidator = valibotNameValidator.clone().append(customValidator);
Zod Adapter
import { zValidator } from 'validation-adapters/zod';
import * as z from 'zod';
const zodNameValidator = zValidator(
z
.string()
.min(2)
.max(10)
.regex(/^([^0-9]*)$/)
);
Valibot Adapter
import * as v from 'valibot';
import { vValidator } from 'validation-adapters/valibot';
const valibotNameValidator = vValidator(
v.pipe(v.string(), v.minLength(2), v.maxLength(10), v.regex(/^([^0-9]*)$/))
);