Skip to content

Latest commit

 

History

History

validation-adapter

validation-adapter banner

GitHub License NPM bundle minzipped size NPM total downloads Join Discord

Status: Experimental

validation-adapter is a universal validation adapter that integrates different validators like Zod, Valibot, Yup, and more.

  • Universal Integration: Combine multiple validation libraries seamlessly
  • Flexible: Easily append and extend validation rules
  • Typesafe: Build with TypeScript for strong type safety

🌟 Motivation

Create a universal validation adapter that generalizes different validation libraries like Zod, Valibot, etc. for use in libraries & applications requiring diverse validation strategies like feature-form.

📖 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);