0.5.0 • Published 9 years ago

lib-schema v0.5.0

Weekly downloads
3
License
ISC
Repository
github
Last release
9 years ago

lib-schema

install

npm install --save lib-schema

usage

Define a json schema

{
  "uid" : {"type": "number", "opts": {"min": 0}},
  "gid" : {"type": "number", "opts": {"min": 0}},
  "args": {"type": "array" , "kind": "string"},
  "envs": {"type": "map"   , "kind": "string"},
  "task": {"type": "struct", "props": {
      "exec" : "string",
      "args" : "args",
      "uid"  : "uid",
      "gid"  : "gid",
      "envs" : "envs"
    }, "require": ["exec", "args"]
  },
  "tasks": {"type": "array" , "kind": "task"},
  "job"  : {"type": "struct", "props": {
      "tasks": "tasks"
    }, "require": ["tasks"]
  }
}

Create a marshaller from the schema

var schema = require('lib-schema');
var types  = schema(json);

var job = types.job.marshal({
  tasks: [
    {exec: 'ls', args: ['-al']}
  ]
});
// job is a validated object against the schema

types.job.marshal({
  tasks: [
    {exec: 'pwd'}
  ]
}); // throws because args is required parameter

details

  • types string and number are build in
  • all other types must be declared before they're used

see also

0.5.0

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.0

10 years ago