micromanage
v0.3.0
Published
JSON Schema enforced records.
Downloads
2
Readme
Micromanage
This is a proof of concept
A utility for generating object factories validated by JSON schemas.
Micromanage uses jjv
to parse
JSON Schemas.
Why
Vanilla JavaScript objects are easy to work with. They behave as expected; they contain no implicit API. They can pass freely through most code without interoperability hiccups.
However it can be hard to determine their shape, set default values, and validate them.
This library provides a thin layer over the JSON Schema Draft 4 specification to make it easier to manipulate, reason about, and validate client-side data.
Specifically, it aims to provide an answer for:
- Data validation
- Structural consistency
- Test scaffolding (randomly generate content based on a schema)
Usage
var Record = require('micromanage').Record
var Cat = Record({
title: 'Cat',
properties: {
species: {
type: 'string',
default: 'feline'
},
name: {
type: 'string'
}
},
required: [ 'name' ]
})
// Created an object based on the Cat schema.
var kitten = Cat({ name: 'Felix' }) // => { name: 'felix', species: 'feline' }
// Update a record. This is non-destructive. Returns a new Cat object
var copyCat = Cat.update(kitten, { name: 'Carl' }) // => { name: 'Carl', species: 'feline'}
// Validate a record
var isValid = Cat.validate({}) // { errors: { name: 'required' } }