Package Exports
- semantic-schema
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (semantic-schema) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Semantic Schema
Write Json Schema In a Graceful Way.
Json Schema is really a useful tool for validating the structure of json data. But it is also veeeeeerbose. So here comes the Semantic-schema. It let you write json schema in a semantic way, also, a simplified way, so you can get rid of the verbose grammar.
This project is uncompleted. But all functionality mentioned below is reliable.
example
const {object, string, integer, number, array, boolean, NULL} = require('semantic-schema').describer;
const Validator = require('semantic-schema').validator;
let schema = array().item(
object().properties({
type: integer().enum(1, 2, 3, 4), // [1, 2, 3, 4]
a: string().pattern(/hello/), // /hello/
b: integer().enum(9), // 9 or [9]
c: boolean(),
d: NULL(),
e: object().properties({
e1: boolean()
}).requiredAll(),
})
.if.properties({type: 1}).then.required('a')
.elseIf.properties({type: 2}).then.required('b')
.else.required('c')
.endIf
);
let validator = new Validator(schema);
let result = validator.validate([{
type: 1,
a: 'hello'
}, {
type: 2,
b: 9
}, {
type: 3,
c: true
}, {
type: 3,
a: 'hello',
b: 9,
c: false,
d: null,
e: {
e1: true
}
}])
console.log(result); // true
// if you want to use the schema file directly, just normalize it.
let jsonSchemaObj = schema.normalize(); // or let jsonSchemaObj = validator.jsonSchema;
console.log(JSON.stringify(jsonSchemaObj, null, 2))