Install
npm install desy
Basic usage
Creating a simple string schema
import {d} from 'desy';
const mySchema = d.string();
mySchema.validate('tuna');
mySchema.validate(12);
Key ideas
- be
simple
- be as
fast as point 1 allows
Key features
- Stop validating on the first error. Desy stops validating on the first error and returns it.
- A string is an indicator. Desy returns an empty string in a valid case. In case of an error, Desy returns a string with a description of the error.
- No throwing errors. Desy only returns an empty or non-empty string.
Examples
Deep schema
const schema = d.array(
d.object({
type: d.string().oneOf(['person']),
hair: d.string().oneOf(['blue', 'brown']),
active: d.boolean(),
name: d.string(),
age: d.number().int(),
hobbies: d.array(d.string()),
address: d.object({
street: d.string(),
zip: d.string(),
country: d.string(),
}),
}),
);
schema.validate(people);
Get schema's types
import {d, InferDesy} from 'desy';
const userSchema = d.object({
username: d.string(),
});
const error = userSchema.validate({username: 'Ludwig'});
type User = InferDesy<typeof user>;
API
Common
const schema = d.mixed();
schema.validate('a');
schema.validate('');
schema.validate(null);
.test(func: (value: sting) => string)
const schema = d.mixed().test((value) => {
if (value === 'world') {
return '';
}
return 'MUST BE WORLD';
});
schema.validate('hello');
schema.validate('world');
mixed
const schema = d.mixed();
schema.validate('a');
schema.validate('');
schema.validate(null);
string
const schema = d.string();
schema.validate('a');
schema.validate('');
schema.validate(null);
const schema = d.string().length(1);
schema.validate('aa');
schema.validate('a');
const schema = d.string().optional();
schema.validate('');
schema.validate('a');
.oneOf(variants: string[])
const schema = d.string().oneOf(['hello', 'world']);
schema.validate('hello');
schema.validate('world');
schema.validate('foo');
const schema = d.string().regexp(/hello/i);
schema.validate('123hello');
schema.validate('hell');
const schema = d.string().min(1);
schema.validate('');
schema.validate('a');
const schema = d.string().max(1);
schema.validate('aa');
schema.validate('a');
number
const schema = d.number();
schema.validate(42);
schema.validate('42');
const schema = d.number().int();
schema.validate(42);
schema.validate(42.2);
const schema = d.number().float();
schema.validate(42);
schema.validate(42.2);
const schema = d.number().min(1);
schema.validate(0);
schema.validate(1);
const schema = d.number().max(1);
schema.validate(1);
schema.validate(2);
boolean
const schema = d.boolean();
schema.validate(true);
schema.validate(false);
schema.validate(1);
const schema = d.boolean().true();
schema.validate(true);
schema.validate(false);
const schema = d.boolean().false();
schema.validate(true);
schema.validate(false);
date
const schema = d.date();
schema.validate(0);
schema.validate('2024-03-15T23:21:48.605Z');
schema.validate(new Date());
schema.validate(undefined);
const now = Date.now();
const schema = d.date().min(now);
schema.validate(now);
schema.validate(now - 1);
const now = Date.now();
const schema = d.date().max(now);
schema.validate(now);
schema.validate(now + 1);
null
const schema = d.null();
schema.validate(null);
schema.validate(undefined);
object
.object(objschema: Record<string, Schema>)
const schema = d.object({
name: d.sting(),
});
schema.validate({name: 'alex'});
schema.validate({name: 'alex', age: 42});
schema.validate({name: 42});
const schema = d
.object({
name: d.sting(),
})
.notStrict();
schema.validate({name: 'alex'});
schema.validate({name: 'alex', age: 42});
schema.validate({name: 42});
.optionalFields(fileds: string[])
const schema = d
.object({
name: d.sting(),
})
.optionalFields(['name']);
schema.validate({name: 'alex'});
schema.validate({});
schema.validate({name: 42});
array
.array(schemas: Schema[])
const schema = d.arrar(d.sting());
schema.validate(['hello', 'world']);
schema.validate(['hello', 42]);
const schema = d.arrar(d.sting()).length(2);
schema.validate(['hello', 'world']);
schema.validate(['world']);
const schema = d.arrar(d.sting()).min(2);
schema.validate(['hello', 'world']);
schema.validate(['world']);
const schema = d.arrar(d.sting()).max(2);
schema.validate(['hello', 'world']);
schema.validate(['hello', 'world', 'foo']);
benchmark
smaller is better