@snps/validation
v0.0.2
Published
Zero-dependency validation library with comprehensive validators
Maintainers
Readme
@snps/validation
Zero-dependency validation library to replace joi/yup/zod.
Installation
pnpm install @snps/validationAPI
schema
The schema object provides a set of schema builders for creating validation schemas.
schema.string()
Creates a string schema.
import { schema } from '@snps/validation';
const stringSchema = schema.string().required().min(3).max(10);schema.number()
Creates a number schema.
import { schema } from '@snps/validation';
const numberSchema = schema.number().positive().integer();schema.boolean()
Creates a boolean schema.
import { schema } from '@snps/validation';
const booleanSchema = schema.boolean().required();schema.array(itemSchema?: Schema)
Creates an array schema.
import { schema } from '@snps/validation';
const arraySchema = schema.array(schema.string()).min(1);schema.object(shape?: Record<string, Schema>)
Creates an object schema.
import { schema } from '@snps/validation';
const objectSchema = schema.object({
name: schema.string().required(),
age: schema.number().positive().required(),
});validate(data: unknown, schema: Schema)
Validates data against a schema.
import { schema, validate } from '@snps/validation';
const userSchema = schema.object({
name: schema.string().required(),
age: schema.number().positive().required(),
});
const userData = { name: 'John', age: 30 };
const result = await validate(userData, userSchema);
if (result.valid) {
console.log('Validation successful');
} else {
console.log('Validation failed:', result.errors);
}