alejandrovrojas/sfl-validator
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
sfl-validator
0.1.0
dependency-free single-file library for schema validation with support for static type inference and custom error messages.
USAGE
import { Validator, Infer } from './validator.ts';
const v = new Validator();
const Address = v.object({
street: v.string(),
city: v.string(),
});
const Profile = v.object({
username: v.string({ min: 3 }, { min: 'Username too short' }),
age: v.optional(v.number()),
address: v.nullable(Address),
tags: v.array(v.string(), { min: 1, max: 5 }),
});
const new_profile: Infer<typeof Profile> = {
username: 'ale',
address: { street: 'ABC', city: 'DEF' },
tags: ['abc', 'def'],
}
const result = Profile.validate(new_profile);