Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
## Why validate with JSON schemas?

- **Simple** - JSON schemas are a simple and expressive way to describe a data structure that your API expects.
- **Standard** - JSON schemas are not specific to javascript. They are used in many server side languages. The standard specification lives here [jscon-schema.org][json-schema-url].
- **Standard** - JSON schemas are not specific to javascript. They are used in many server side languages. The standard specification lives here [json-schema.org][json-schema-url].
- **Fail-Fast** - Validating a payload before handing it to your application code will catch errors early that would otherwise lead to more confusing errors later.
- **Separate Validation** - Manually inspecting a payload for errors can get lengthy and clutter up your application code.
- **Error Messaging** - Coming up with error messaging for every validation error becomes tedious and inconsistent.
- **Documentation** - Creating a JSON schema documents the API requirements.
- **Documentation** - Creating a JSON schema helps document the API requirements.

## Installation

Expand Down Expand Up @@ -182,7 +182,7 @@ schemas. It should be called once at the beginning of your application so that y
have the custom properties available.

```javascript
var addAttributes = require('express-jsonschema').addSchemaProperties;
var addSchemaProperties = require('express-jsonschema').addSchemaProperties;

addSchemaProperties({
contains: function(value, schema){
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ function validate(schemas, schemaDependencies) {
var validator = new jsonschema.Validator();

if (Array.isArray(schemaDependencies)) {
schemaDependencies.forEach(validator.addSchema.bind(validator));
schemaDependencies.forEach(function(dependency){
validator.addSchema(dependency);
});
}

Object.keys(customProperties).forEach(function(attr) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"merge": "^1.2.0",
"mocha": "^2.1.0",
"should": "^4.6.1",
"sinon": "^1.17.6",
"supertest": "^0.15.0"
},
"main": "./index.js",
Expand Down
3 changes: 1 addition & 2 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var request = require('supertest'),
helpers = require('./helpers'),
app = require('./testapp');

require('should');
describe('A route with validation middleware', function() {
it('should respond with a 200 if the posted body is valid', function(done) {
request(app)
Expand Down Expand Up @@ -81,7 +80,7 @@ describe('A route with validation middleware', function() {
body: [{
value: 'junk',
messages: [
'does not conform to the \'email\' format',
'does not conform to the "email" format',
'does not meet minimum length of 7',
'does not contain the string "terje.com"'
],
Expand Down
90 changes: 90 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* jshint mocha: true */
var sinon = require('sinon');

describe('addSchemaProperties', function() {
it('should raise a JsonSchemaCustomPropertyError if you try to add ' +
Expand All @@ -18,3 +19,92 @@ describe('addSchemaProperties', function() {
});
});
});

describe('using one schema dependency when validating a valid object', function() {
it('should validate the object with schema dependency', function() {
var next = sinon.spy(),
AddressSchema = {
"id": "/Address",
"type": "object",
"properties": {
"city": {"type": "string"},
"state": {"type": "string"},
"country": {"type": "string"}
}
},
PersonSchema = {
"id": "/Person",
"type": "object",
"properties": {
"name": {"type": "string"},
"address": {"$ref": "/Address"}
}
},
request = {
"post": {
"name": "Adrian",
"address" : {
"city": "Oakland",
"state": "CA",
"country": "USA"
}
}
},
validate = require('../index.js').validate,
validateRequest = validate({post: PersonSchema}, [AddressSchema]);

validateRequest(request, function(){}, next);
next.calledWith().should.be.eql(true);
});
});

describe('using two schema dependencies when validating a valid object', function() {
it('should validate the object with schema dependency', function() {
var next = sinon.spy(),
AddressSchema = {
"id": "/Address",
"type": "object",
"properties": {
"city": {"type": "string"},
"state": {"type": "string"},
"country": {"type": "string"},
"street": {"$ref": "/Street"}
}
},
StreetSchema = {
"id": "/Street",
"type": "object",
"properties": {
"number": {"type": "string"},
"name": {"type": "string"}
}
},
PersonSchema = {
"id": "/Person",
"type": "object",
"properties": {
"name": {"type": "string"},
"address": {"$ref": "/Address"}
}
},
request = {
"post": {
"name": "Adrian",
"address" : {
"city": "Oakland",
"state": "CA",
"country": "USA",
"street": {
"name": "Broadway St",
"number": 555
}
}
}
},
validate = require('../index.js').validate,
validateRequest = validate({post: PersonSchema}, [AddressSchema, StreetSchema]);

validateRequest(request, function(){}, next);
next.calledWith().should.be.eql(true);
});
});