@tongchia/jsschema-sugar
v0.1.7
Published
js schema
Downloads
7
Maintainers
Readme
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;