lib-marshal
marshalled object validation
Install
npm install --save lib-marshal
Usage
define your types
var marshal = ; // define some typesvar stringProto = ;var messageProto = ; messageProto;messageProto;messageProto;messageProto;
prepare data for the client
Prepare data for the client, by stripping out unused properties and validating data types.
// create a data objectvar m = from: 'bob' dest: 'kim' body: 'hello world'; // ready it for the world, or die tryingJSON
parse incoming data
Validate an incoming object, or throw an error.
var message = messageProto
options
numbers
var num = max: 10 min: 0 num// Error: Value <-1> must be greater than 0 at <var> num// Error: Value <20> must be less than 10 at <var>
strings
var str = min: 1 max: 20 match: /.@.\../ str// Error: Value <bob AT aol.com> must be match the pattern </.@.\../> at <var>
untyped (json)
If you're not ready for a schema yet, you can allow arbitrary data with the JsonType
type
var type = var json = type
errors
Handy dandy error messages
var message = ;var address = ;var content = ; contentcontentcontent addressaddress messagemessagemessage message;
You'll receive a helpful error when things fail to parse
Error: Expected <object> but Received "Bob" of type <string> at <var>.content.author
see also
- lib-schema generate a marshaller from a json schema