Permalink
Browse files

Merge branch 'master' of git://github.com/tdegrunt/jsonschema

Conflicts:
	lib/attribute.js
	lib/helpers.js
	lib/validator.js
  • Loading branch information...
awwright committed Dec 25, 2012
2 parents ea3c202 + bfa95a3 commit 882f43b3828170f3ab6ebc84ca7f38597087479d
Showing with 324 additions and 262 deletions.
  1. +84 −91 README.md
  2. +6 −7 examples/ref.js
  3. +107 −78 lib/attribute.js
  4. +64 −49 lib/helpers.js
  5. +62 −36 lib/validator.js
  6. +1 −1 test/suite
View
175 README.md
@@ -1,118 +1,111 @@
[![Build Status](https://secure.travis-ci.org/tdegrunt/jsonschema.png)](http://travis-ci.org/tdegrunt/jsonschema)
# jsonschema
-
Simple and fast [JSON schema](http://tools.ietf.org/html/draft-zyp-json-schema-03) validator.
## Usage
+
+### Simple
+Simple object validation using JSON schemas.
+
+```javascript
+ var v = new Validator();
+ var instance = 4;
+ var schema = {"type": "number"};
+ console.log(v.validate(instance, schema));
+```
+
+### Even simpler
+
+```javascript
+ var validate = require('jsonschema').validate;
+ console.log(validate(4, {"type": "number"}));
+```
+
+### Complex example, with split schemas and references
+
```javascript
- var v = new Validator();
- var instance = 4;
- var schema = {"type": "number"};
- console.log(v.validate(instance, schema));
+ var v = new Validator();
+
+ // Address, to be embedded on Person
+ var addressSchema = {
+ "id": "/SimpleAddress",
+ "type": "object",
+ "properties": {
+ "lines": {
+ "type": "array",
+ "items": {"type": "string"}
+ },
+ "zip": {"type": "string"},
+ "city": {"type": "string"},
+ "country": {"type": "string", "required": true}
+ }
+ };
+
+ // Person
+ var schema = {
+ "id": "/SimplePerson",
+ "type": "object",
+ "properties": {
+ "name": {"type": "string"},
+ "address": {"$ref": "/SimpleAddress"},
+ "votes": {"type": "integer", "minimum": 1}
+ }
+ };
+
+ var p = {
+ "name": "Barack Obama",
+ "address": {
+ "lines": [ "1600 Pennsylvania Avenue Northwest" ],
+ "zip": "DC 20500",
+ "city": "Washington",
+ "country": "USA"
+ },
+ "votes": "lots"
+ };
+
+ var v = new Validator();
+ v.addSchema(addressSchema, '/SimpleAddress');
+ console.log(v.validate(p, schema));
```
## Features
### Definitions
-
-Any non ticked off definition types are ignored.
-
-| Value | JSON Schema Draft | jsonschema | Comments |
-|:------|:-----------------:|:----------:|:---------|
-| type | ✔ | ✔ |
-| properties | ✔ | ✔ |
-| patternProperties | ✔ | ✔ |
-| additionalProperties | ✔ | ✔ |
-| items | ✔ | ✔ |
-| additionalItems | ✔ | ✔ |
-| required | ✔ | ✔ |
-| dependencies | ✔ | ✔ |
-| minimum | ✔ | ✔ |
-| maximum | ✔ | ✔ |
-| exclusiveMinimum | ✔ | ✔ |
-| exclusiveMaximum | ✔ | ✔ |
-| minItems | ✔ | ✔ |
-| maxItems | ✔ | ✔ |
-| uniqueItems | ✔ | ✔ |
-| pattern | ✔ | ✔ |
-| minLength | ✔ | ✔ |
-| maxLength | ✔ | ✔ |
-| enum | ✔ | ✔ |
-| default | ✔ | ✔ | informational only
-| title | ✔ | ✔ | informational only
-| description | ✔ | ✔ | informational only
-| format | ✔ | ✔ |
-| divisibleBy | ✔ | ✔ |
-| disallow | ✔ | ✔ |
-| extends | ✔ | ✔ |
-| id | ✔ | ✔ | informational only
-| $ref | ✔ | ✔ |
-| $schema | ✔ | | ignored
+All schema definitions are supported, $schema is ignored.
### Types
-
-| Value | JSON Schema Draft | jsonschema | Comments |
-|:------|:-----------------:|:----------:|:---------|
-| `string` | ✔ | ✔ |
-| `number` | ✔ | ✔ |
-| `integer` | ✔ | ✔ |
-| `boolean` | ✔ | ✔ |
-| `object` | ✔ | ✔ |
-| `array` | ✔ | ✔ |
-| `null` | ✔ | ✔ |
-| `date` | | ✔ |
-| `any` | ✔ | ✔ |
-| Union Types | ✔ | ✔ |
+All types are supported
### String Formats
-
-| Value | JSON Schema Draft | jsonschema | Comments |
-|:------|:-----------------:|:----------:|:---------|
-| `date-time` | ✔ | ✔ |
-| `date` | ✔ | ✔ |
-| `time` | ✔ | ✔ |
-| `utc-millisec` | ✔ | ✔ | Any number (integer or float) is allowed
-| `regex` | ✔ | ✔ | We test for valid regular expression
-| `color` | ✔ | ✔ |
-| `style` | ✔ | ✔ |
-| `phone` | ✔ | ✔ | Should follow [http://en.wikipedia.org/wiki/E.123](E.123) standard.
-| `uri` | ✔ | ✔ |
-| `email` | ✔ | ✔ |
-| `ip-address` | ✔ | ✔ |
-| `ipv6` | ✔ | ✔ |
-| `host-name` | ✔ | ✔ |
-| `alpha` | | ✔ |
-| `alphanumeric` | | ✔ |
+All formats are supported, phone numbers are expected to follow the [http://en.wikipedia.org/wiki/E.123](E.123) standard.
## Tests
-
Uses [https://github.com/Julian/JSON-Schema-Test-Suite](JSON Schema Test Suite) as well as our own.
You'll need to update and init the git submodules:
git submodule update --init
npm test
## License
-
jsonschema is licensed under MIT license.
- Copyright (C) 2012 Tom de Grunt <tom@degrunt.nl>
-
- Permission is hereby granted, free of charge, to any person obtaining a copy of
- this software and associated documentation files (the "Software"), to deal in
- the Software without restriction, including without limitation the rights to
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
- of the Software, and to permit persons to whom the Software is furnished to do
- so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
+ Copyright (C) 2012 Tom de Grunt <tom@degrunt.nl>
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
+ this software and associated documentation files (the "Software"), to deal in
+ the Software without restriction, including without limitation the rights to
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ of the Software, and to permit persons to whom the Software is furnished to do
+ so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all
+ copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
View
@@ -5,8 +5,8 @@ var Validator = require('../lib/validator');
// Address, to be embedded on Person
var addressSchema = {
- "id": "Simple#Address",
- "type" : "object",
+ "id": "/SimpleAddress",
+ "type": "object",
"properties": {
"lines": {
"type": "array",
@@ -20,11 +20,11 @@ var addressSchema = {
// Person model
var schema = {
- "id": "Simple#Person",
- "type" : "object",
+ "id": "/SimplePerson",
+ "type": "object",
"properties": {
"name": {"type": "string"},
- "address": {"$ref": "Simple#Address"},
+ "address": {"$ref": "/SimpleAddress"},
"votes": {"type": "integer", "minimum": 1}
}
};
@@ -41,6 +41,5 @@ var p = {
};
var v = new Validator();
-v.addSchema(addressSchema, 'Simple#Address');
-p.address = 1235;
+v.addSchema(addressSchema, '/SimpleAddress');
console.log(v.validate(p, schema));
Oops, something went wrong.

0 comments on commit 882f43b

Please sign in to comment.