Write jsonschema with cson
Only support Jsonchema draft 4.
username: 'string'
age: 'integer'
verified: 'boolean'
gender: ['F', 'M']
created_at: 'date'
$defs:
$_:
'geo-point': ['number']
photo:
w: 'integer'
h: 'integer'
url: 'string'
user:
$include: "user.schema"
tag:
$raw:
type: 'string'
pattern: '^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$'
count:
$raw:
type: 'integer'
minimum: 1
maximum: 100
# Define a media object
owner: 'user'
avatar_url: 'user.avatar_url'
tags: ['tag']
tag_count: 'count'
desc: 'string'
photo: 'photo'
location: 'geo-point'
created_at: 'date'
$required: '-location -tags'
$ npm install csonschema
See csonschema-cli
// Include csonschema
csonschema = require('csonschema');
schema = [{
id: 'integer',
username: 'string'
}]
// Parse sync
jsonschema = csonschema.parse(schema);
// Parse async
csonschema.parse(schema, function(err, obj) {
jsonschema = obj
});
csonschema = require 'csonschema'
schema = [
id: 'integer'
username: 'string'
]
// Parse sync
jsonschema = csonschema.parse schema
// Parse async
csonschema.parse schema, (err, obj) ->
jsonschema = obj
Raw Field will be translated to json format directly without any modification, it is represented with $raw
keyword.
username:
$raw:
type: 'string'
pattern: '[1-9a-zA-Z]'
date:
$raw:
type: 'string'
format: 'date-time'
additionalProperties is false by default
$defs:
username: 'string'
user:
username: 'username'
created_at: 'date'
updated_at: 'date'
$required: '-username -created_at'
Array as root object
[
user: 'user'
]
Array in field
username: 'string'
photos: [
url: 'string'
]
Customized types are defined under $defs
.
foo.bar
$_
. e.g. $_.foo
is equal to foo
$defs:
$_:
location:
desc: 'string'
coordinates: ['number']
username: 'string'
By default all fields are required. However, using $required
it is possible to
define which ones are required:
firstname: 'string'
lastname: 'string'
$required: 'firstname'
This example will set firstname
as a required field.
Also, you can define which fields are not required by prefixing with a dash (-
):
firstname: 'string'
lastname: 'string'
$required: '-lastname'
Both examples will lead to the same result, having firstname
as required and
lastname
as optional.
$ npm test
Any contribution is more then welcome!