JS Schema validation library, compatible with json-schema
.
It is more suitable for js use, pursues concise and stylish code style, make it reusable as possible.
welcome to fork
,pr
,issues
;
install with npm
npm i -S '@tongchia/jsschema'
const {obj, str, num, int, date, arr, bool, buff, any} = require('jsschema');
const person = obj.properties({
name : str.maxLength(200).minLength(5),
binary : buff,
living : bool.default(true),
updated : date.default(Date.now),
age : int.min(0).max(65, true), // > 0 && <= 130
email : str.format('email'),
birthday : date.after('1890-01-01'),
mixed : any,
_someId : str.format('mongo-id'),
_array : arr,
array : [],
_ofString : arr.items(str),
ofString : [str], // same as `array.items(string)` ↑
ofNumber : [num],
ofDates : [date],
ofBuffer : [buff],
ofBoolean : [bool],
ofMixed : [any],
ofObjectId: [str.format('mongo-id')],
ofArrays : [[]],
ofArrayOfNumbers : [[num]],
books : [{ // => array.items(object.properties({...
title : str,
author : [str],
}],
}).required(['name', 'age', 'birthday']);
person.isValid({
name : 'TongChia',
age : 30,
birthday: new Date('10/18/1987')
}, (err) => {
assert.ifError(err);
});
// to json-schema;
console.log(JSON.stringify(person))
- string
- enum
- pattern
- minLength
- maxLength
- format
- * from chriso/validator.js ✨
alpha
email
url
base64
hex-color
md5
mongo-id
uuid
ip
(ipv4
,ipv6
)json
- ...
- data-time (full-date, full-time)
- hostname
- uri, iri
- uri-template
- regexp
- * from chriso/validator.js ✨
- String-Encoding Non-JSON Data
- number (integer)
- enum
- minimum, maximum
- exclusiveMinimum, exclusiveMaximum
- min, max (alias to
minimum, maximum, exclusiveMinimum, exclusiveMaximum
) - range (alias to
min, max
) - integer
- multipleOf
- converter (
numeric
)
- date (
js-schema
)- after
- before
- converter (
date-time
,full-date
,date-string
,time-stamp
)
- array
- minItems
- maxItems
- unique (
uniqueItems
) - items
- additionalItems
- contains
- entries ✨
- Overload function ✨
- object
- properties
- required
- patternProperties
- additionalProperties
- size (
minProperties
,maxProperties
) - propertyNames
- dependencies
- schema dependencies
- buffer (
js-schema
)- converter (
strings
,base64
)
- converter (
- null (nil)
- boolean
- function (
js-schema
) - any
- allOf
- anyOf
- oneOf
- not
- Constant values
- Enumerated values
- Metadata
- title
- description
- default
- examples
- json-schema
- generate json-schema
- parse json-schema
- Combining schemas ⚡️
- referenced schema
- $id ⚡️
- $ref ⚡️
- resolve method (browser & nodeJs) ⚡️
const {string} = require('jsschema');
string.addValidate(
'keyword', // the keyword
{
validator: (value, params) => check(value, parameter), // return true/false;
message: '`{value}` should valid for schema:{type}.{keyword}({params})',
}
)
string.addValidate(
'keyword',
{
isAsync: true,
validator: (value, params, callback) => {
// check value
if ('ok')
return callback(null, value)
return callback(new ValidationError('...'))
}
}
)
- custom validate;
- test report;
- automatic publish;
- browser test;
- toModel
- MobX;
- MongoDB;
- mQuery;
- mongoose schema;
- json-schema;
- parse json-schema;
- generate json-schema;
- test with
ajv
;
- google protocol-buffers;
- GraphQL;