easy-vac

easily validate and clean data, with JSON Schema-like syntax

Usage no npm install needed!

<script type="module">
  import easyVac from 'https://cdn.skypack.dev/easy-vac';
</script>

README

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