[ GitHub | Home | Playground | Documentation | Examples ]
easy-vac is a JavaScript library which helps you validate and clean data.
- Better than JSON Schema: get rid of JSON limits, define schemas in JavaScript style
- Auto Type Inferrence: works perfectly with TypeScript
- Type Tolerance: may convert values to correct type
- Highly Extensible: define your own types and reuse them everywhere
easy-vac can be installed via:
- via NPM:
npm install --save easy-vac
- via CDN:
- easy-vac only runs on modern browsers with ES6 support.
- UMD version is provided by default, with global name
EasyVAC
- JSDelivr: https://cdn.jsdelivr.net/npm/easy-vac
- UnPKG: https://unpkg.com/easy-vac
Example (more...)
import { VObject, VArray, VEnum, VTuple, VACError } from "easy-vac"
const OrderItem = VObject({
product: { type: "string", required: true },
count: { type: "int", default: 1, minimum: 1 },
})
const Order = VObject.required({
guest: { type: String }, // <- type can also be "string"
items: { type: VArray({ items: { type: OrderItem },
minItems: 1,
maxItems: 3 })
}
})
var incoming = {
_id: "xxxxx",
guest: 12345,
items: [
{ product: "Ice Cream" },
{ product: "Toast", count: 3 },
]
}
var order = Order.vac(incoming) // <-- throws VACError if failed
console.log(order)
Output:
{
"guest": "12345",
"items": [
{ "product": "Ice Cream", "count": 1 },
{ "product": "Toast", "count": 3 }
]
}