0.5.3 • Published 10 years ago

csonschema v0.5.3

Weekly downloads
949
License
MIT
Repository
github
Last release
10 years ago

csonschema

Write jsonschema with cson

NPM Build Status Dependency Status Coverage Status

Features

Only support Jsonchema draft 4.

Simple csonschema

username: 'string'
age: 'integer'
verified: 'boolean'
gender: ['F', 'M']
created_at: 'date'

Advanced csonschema

$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'

Installation

Node.js and NPM is required.

$ npm install csonschema

Usage

CLI

See 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
});

Coffeescript

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.

username:
  $raw:
    type: 'string'
    pattern: '[1-9a-zA-Z]'

date:
  $raw:
    type: 'string'
    format: 'date-time'

Object

additionalProperties is false by default

$defs:
  username: 'string'

user:
  username: 'username'
  created_at: 'date'
  updated_at: 'date'
  $required: '-username -created_at'

Array Field

Array as root object

[
  user: 'user'
]

Array in field

username: 'string'
photos: [
  url: 'string'
]

Customized types

Customized types are defined under $defs.

  • Support reference with cascading format like foo.bar
  • Global types without typing prefix $_. e.g. $_.foo is equal to foo
$defs:
  $_:
    location:
      desc: 'string'
      coordinates: ['number']

  username: 'string'

Contribution

Run Tests

$ npm test

Any contribution is more then welcome!

Bitdeli Badge

0.5.3

10 years ago

0.5.3-beta.1

10 years ago

0.5.2

10 years ago

0.5.2-beta.3

10 years ago

0.5.1

11 years ago

0.5.0

11 years ago

0.4.0

11 years ago

0.3.0

11 years ago

0.2.0

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago

0.0.0

11 years ago