Skip to main content

Intro

Vality offers a simple and intuitive way to describe your schema:

Describe it

ts
import { v } from "vality";
 
// Models are defined like this:
const Person = () => ({
name: v.string,
age: v.number,
address: {
street: v.string,
city: v.string,
country: v.string,
},
});
ts
import { v } from "vality";
 
// Models are defined like this:
const Person = () => ({
name: v.string,
age: v.number,
address: {
street: v.string,
city: v.string,
country: v.string,
},
});

Type it

ts
import type { Parse } from "vality";
 
// And can easily be converted into a type:
type PersonModel = Parse<typeof Person>;
type PersonModel = { name: string; age: number; address: { street: string; city: string; country: string; }; }
ts
import type { Parse } from "vality";
 
// And can easily be converted into a type:
type PersonModel = Parse<typeof Person>;
type PersonModel = { name: string; age: number; address: { street: string; city: string; country: string; }; }

Validate it

ts
import { validate } from "vality";
 
// Or used directly in a validation function:
result = validate(Person, {
name: "Max",
age: "look ma no number",
address: {
street: "Main Street",
// city: "Dummytown",
country: "USA",
},
});
 
// Which yields the following result:
result = {
valid: false,
data: undefined,
errors: [{
message: "vality.number.base",
path: ["age"],
value: "look ma no number",
meta: { ... }
}, {
message: "vality.string.base",
path: ["address", "city"],
value: undefined,
meta: { ... }
}]
};
ts
import { validate } from "vality";
 
// Or used directly in a validation function:
result = validate(Person, {
name: "Max",
age: "look ma no number",
address: {
street: "Main Street",
// city: "Dummytown",
country: "USA",
},
});
 
// Which yields the following result:
result = {
valid: false,
data: undefined,
errors: [{
message: "vality.number.base",
path: ["age"],
value: "look ma no number",
meta: { ... }
}, {
message: "vality.string.base",
path: ["address", "city"],
value: undefined,
meta: { ... }
}]
};