npm install csonschemaWrite jsonschema with cson




Only support Jsonchema draft 4.
``coffee`
username: 'string'
age: 'integer'
verified: 'boolean'
gender: ['F', 'M']
created_at: 'date'
`coffee
$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
$required: '-location -tags'
`
[Node.js][] and [NPM][] is required.
$ npm install csonschema
[Node.js]: https://npmjs.org/
[NPM]: https://npmjs.org/
See [csonschema-cli][]
[csonschema-cli]: http://github.com/cybertk/csonschema-cli
`javascript
// 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
});
`
`coffee
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
Raw Field will be translated to json format directly without any modification, it is represented with $raw keyword.
`coffee
username:
$raw:
type: 'string'
pattern: '[1-9a-zA-Z]'
date:
$raw:
type: 'string'
format: 'date-time'
`
#### Object
additionalProperties is false by default
`coffee
$defs:
username: 'string'
user:
username: 'username'
created_at: 'date'
updated_at: 'date'
$required: '-username -created_at'
`
#### Array Field
Array as root object
`coffee`
[
user: 'user'
]
Array in field
`coffee`
username: 'string'
photos: [
url: 'string'
]
#### Customized types
Customized types are defined under $defs.foo.bar
- Support reference with cascading format like $_
- Global types without typing prefix . e.g. $_.foo is equal to foo
`coffee
$defs:
$_:
location:
desc: 'string'
coordinates: ['number']
username: 'string'
``
$ npm test
Any contribution is more then welcome!
