@tongchia/jsschema-sugar
js schema
Last updated 2 years ago by tongchia .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @tongchia/jsschema-sugar 
SYNC missed versions from official npm registry.

js-schema (with Sugar.js)

JS Schema validation, compatible with json-schema

QUICK START

install with npm

npm install -S '@tongchia/jsschema-sugar'
const Sugar = require('sugar');
const schema = Sugar.String.maxLength(200).minLength(5).match(/hello/);
// -> Schema {isValid: function (value) { /* check value */ }, ...}
schema.isValid('hello world');
// -> true
schema.isValid('hello');
// -> false
schema.isValid('foo', (err, data) => {
  assert.ifError(err);
  assert.equal('foo', data);
})
const schema = Sugar.Object.properties({
  name: Sugar.String.maxLength(20).required(),
  age : Sugar.Number.max(150).min(0),
  birthday: Sugar.Date
});
schema.isValid({
  name: 'Tom',
  age: 12,
  birthday: now Date()
});
// => true

toJsonSchema

Sugar.Number.max(10).min(1).toJSON();
// {
//   type: 'number',
//   max: 10,
//   min: 1
// }
Sugar.String.toJSON();
// {
//   type: 'string'
// }

VALIDATE

BUILT-IN

  • String
    • enum
    • match
    • minLength
    • maxLength
  • Number
    • min
    • max
    • integer
  • Date
    • after
    • before
  • Array
    • minItems
    • maxItems
    • unique
    • items
  • Object
    • properties
    • required

CUSTOM

Sync validate

SugarNamespace.validates.set(
  'keyword', // the keyword
  {
    validator: (value, parameter) => check(value, parameter), // return true/false;
    message: '${keyword} error, ${value} too big',
    error: RangeError // defalut ValidationError;
  }
)

Async validate

SugarNamespace.validates.set(
  'keyword',
  {
    validator: (value, parameter, callback) => {
      if (value)
        return callback(null, value);
      else
        return callback(new Error('...'))
    }
  }
)

TODO

  • [x] custom validate;
  • [x] async validator (with callback);
  • [ ] async validator (es2015);
  • [ ] format validate;
  • [ ] toModel
    • [ ] MobX;
    • [ ] MongoDB;
      • [ ] mQuery;
  • [ ] mongoose schema;
  • [ ] json-schema;
    • [ ] parse json-schema;
    • [ ] generate json-schema;
    • [ ] test with ajv;
  • [ ] google protocol-buffers;
  • [ ] GraphQL;

Current Tags

  • 0.1.7                                ...           latest (2 years ago)

7 Versions

  • 0.1.7                                ...           2 years ago
  • 0.1.6                                ...           2 years ago
  • 0.1.5                                ...           2 years ago
  • 0.1.4                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (3)
Dev Dependencies (11)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |