easy-vac
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

easy-vac

Build Status npm npm bundle size npm type definitions NPM License

[ 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

Install

easy-vac can be installed via:

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 }
  ]
}

Package Sidebar

Install

npm i easy-vac

Weekly Downloads

9

Version

2.0.2

License

MIT

Unpacked Size

99 kB

Total Files

7

Last publish

Collaborators

  • lyonbot