joi-model
Model encapsulation of a Joi schema
Usage
First create a schema
var Joi = ; var schema = a: Joi b: Joi;
Create a model class for that schema:
var joiModel = ; var MyModel = ;
Create a document based on the model:
var document = ;
Manipulate the document:
document; document; documenta = 'test';documentb = 'another'; documenta = 3; // throws a type error documenta = null; // throws a required error documentb = 'hel'; // throws a length error JSON; // '{"a":"test","b":"another"}'
The object is validated after each action and will throw any errors that are found.
Data can also be set to the document as it is created:
var document = a: 'hello' b: 'world';
More Complex Objects
var Joi = ;var joiModel = ; var adult = Joiobject name: Joi job: Joi; var child = Joiobject name: Joi age: Joi; var family = Joiobject surname: Joi adults: Joi children: Joi; var FamilyModel = ; var family = surname: 'Smith' adults: name: 'John' job: 'Clerk' name: 'Jane' job: 'Programmer' children: name: 'Jimmy' age: 3 name: 'Jenny' age: 5 ; familychildren; familychildren; // this push will throw an error as this would be the 5th child familychildren;