https://github.com/lyonbot/easy-vac
Validate-and-Clean made easy with TypeScript / JavaScript.
https://github.com/lyonbot/easy-vac
Last synced: 5 months ago
JSON representation
Validate-and-Clean made easy with TypeScript / JavaScript.
- Host: GitHub
- URL: https://github.com/lyonbot/easy-vac
- Owner: lyonbot
- License: mit
- Created: 2019-03-10T16:11:06.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-26T21:57:36.000Z (over 1 year ago)
- Last Synced: 2025-07-01T11:08:34.914Z (12 months ago)
- Language: TypeScript
- Size: 137 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# easy-vac
[](https://travis-ci.org/lyonbot/easy-vac)
[](https://www.npmjs.com/package/easy-vac)



[
**[GitHub](https://github.com/lyonbot/easy-vac)** |
**[Home](https://lyonbot.github.io/easy-vac/)** |
**[Playground](https://lyonbot.github.io/easy-vac/#playground)** |
**[Documentation](https://github.com/lyonbot/easy-vac/wiki)** |
**[Examples](https://lyonbot.github.io/easy-vac/#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
## Install
easy-vac can be installed via:
- via [NPM](https://www.npmjs.com/package/easy-vac): `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:
- UnPKG:
## Example ([more...](https://lyonbot.github.io/easy-vac/#examples))
```typescript
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:
```javascript
{
"guest": "12345",
"items": [
{ "product": "Ice Cream", "count": 1 },
{ "product": "Toast", "count": 3 }
]
}
```