Skip to content

Commit

Permalink
v.1.6.0 Switch validator to 'jsonschema' with new v4 features
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleeye committed Jul 14, 2014
1 parent cb1742f commit d1cd192
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 16 deletions.
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "E-conomic",
"name": "schemagic",
"description": "JSON validation with schemas, and schema tools",
"version": "1.5.8",
"version": "1.6.0",
"repository": {
"type": "git",
"url": "https://github.com/e-conomic/schemagic"
Expand All @@ -14,24 +14,25 @@
"npm": ">=1.1.9"
},
"dependencies": {
"json-schema": "git+ssh://git@github.com/e-conomic/json-schema.git",
"underscore": "",
"async": "",
"jsonschema": "^0.4.0",
"lodash": "",
"moment": "",
"traverse": "",
"async": "",
"lodash": ""
"underscore": ""
},
"scripts": {
"test": "node ./bin/runTests.js"
},
"devDependencies": {
"mocha": "",
"chai": "",
"sinon": "",
"sinon-chai": "",
"jshint": ">=2.3.0",
"chai-subset": "^0.1.2",
"injectr": "",
"jshint": ">=2.3.0",
"jshint-teamcity-reporter": "",
"mocha": "",
"mocha-teamcity-reporter": "",
"jshint-teamcity-reporter": ""
"sinon": "",
"sinon-chai": ""
}
}
4 changes: 2 additions & 2 deletions source/util/schemaFactory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var jsonSchema = require("json-schema");
var util = require('util');
var readOnlyDocumentPrunerFactory = require("./readOnlyDocumentPruner");
var maxDecimalHandlerFactory = require("./maxDecimalHandler");
Expand All @@ -7,6 +6,7 @@ var foreignKeyValidationFactory = require("./foreignKeyValidationFactory");
var exampleJson = require("./exampleJson");
var emptyFieldsPrumer = require("./emptyFieldsPrumer");
var emptyStringsToNullFactory = require("./emptyStringsToNullFactory");
var _validate = require('jsonschema').validate;

function schemaFactory(rawSchema, foreignKeys) {

Expand Down Expand Up @@ -35,7 +35,7 @@ function schemaFactory(rawSchema, foreignKeys) {
errors = errors.concat(stringFormatValidator(document).errors);
}

errors = errors.concat(jsonSchema._validate(document, rawSchema, options).errors);
errors = errors.concat(_validate(document, rawSchema, options).errors);
var doForeignKeyValidation = options && options.foreignKey === true;
if (errors.length === 0 && doForeignKeyValidation && foreignKeys) {
if (!optionalCallback) {
Expand Down
1 change: 1 addition & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ chai.config.includeStack;
global.chai = chai;
global.expect = chai.expect;
chai.use(require('sinon-chai'));
require('chai-subset').addMethods(chai);
31 changes: 31 additions & 0 deletions test/exampleSchemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,5 +593,36 @@ module.exports = {
"required":false
}
}
},

oneOfSchema: {
description: "Simple object with 2 possible subdocuments",
required: true,
type: "object",
properties: {
a: {
type: "object",
oneOf: [
{
type: 'object',
properties: {
b: {
type: "string",
required: true
}
}
}
, {
type: 'object',
properties: {
c: {
type: "string",
required: true
}
}
}
]
}
}
}
};
63 changes: 59 additions & 4 deletions test/integration/schemaFactory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ describe("/source/util/schemaFactory run on simpleSchema, the returned object",
});

it("will have the correct error", function () {
expect(result.errors).to.eql([
expect(result.errors).to.containSubset([
{
"property":"a",
"message":"string value found, but a number is required"
"property": "instance.a",
"message": "is not of a type(s) number"
}
]
);
Expand Down Expand Up @@ -256,7 +256,7 @@ describe("/source/util/schemaFactory run on simpleSchema, the returned object",
});

it('should not validate the document', function () {
expect(result).to.have.deep.property('errors.0.message', 'Array value found, but a object is required');
expect(result).to.have.deep.property('errors.0.message', "is not of a type(s) object");
});
});

Expand Down Expand Up @@ -323,4 +323,59 @@ describe("/source/util/schemaFactory run on simpleSchema, the returned object",
});
});
});

describe('test v4 features', function() {
before(function() {
schema = schemaFactory(rawSchemas.oneOfSchema);
});

describe('test valid document', function() {
var result;

var document = {
a: {b: 'yess'}
};

before(function () {
result = schema.validate(document);
});

it('should validate with no erros', function() {
expect(result).to.have.property("valid", true);
});
});

describe('test 1st invalid document', function() {
var result;

var document = {
a: {}
};

before(function () {
result = schema.validate(document);
});

it('should validate with no erros', function() {
expect(result).to.have.property("valid", false);
});
});

describe('test 2nd invalid document', function() {
var result;

var document = {
a: {b: 'yess', c: 'de'}
};

before(function () {
result = schema.validate(document);
});

it('should validate with no erros', function() {
expect(result).to.have.property("valid", false);
});
});

});
});

0 comments on commit d1cd192

Please sign in to comment.