Skip to content

Latest commit

 

History

History
86 lines (71 loc) · 2.63 KB

File metadata and controls

86 lines (71 loc) · 2.63 KB

validation-adapters banner

GitHub License NPM bundle minzipped size NPM total downloads Join Discord

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]*)$/))
);