-
-
Notifications
You must be signed in to change notification settings - Fork 864
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asynchronous schema compilation with loading missing remote schemas u…
…sing supplied function, #43, mulesoft-labs/osprey-mock-service#11
- Loading branch information
1 parent
bec34c5
commit 4dc6cbe
Showing
5 changed files
with
227 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
'use strict'; | ||
|
||
|
||
var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv') | ||
, should = require('chai').should() | ||
, stableStringify = require('json-stable-stringify'); | ||
|
||
|
||
describe('compileAsync method', function() { | ||
var ajv; | ||
|
||
var SCHEMAS = { | ||
"http://example.com/object.json": { | ||
"id": "http://example.com/object.json", | ||
"properties": { | ||
"b": { "$ref": "int2plus.json" } | ||
} | ||
}, | ||
"http://example.com/int2plus.json": { | ||
"id": "http://example.com/int2plus.json", | ||
"type": "integer", | ||
"minimum": 2 | ||
}, | ||
"http://example.com/tree.json": { | ||
"id": "http://example.com/tree.json", | ||
"type": "array", | ||
"items": { "$ref": "leaf.json" } | ||
}, | ||
"http://example.com/leaf.json": { | ||
"id": "http://example.com/leaf.json", | ||
"properties": { | ||
"name": { "type": "string" }, | ||
"subtree": { "$ref": "tree.json" } | ||
} | ||
}, | ||
"http://example.com/recursive.json": { | ||
"id": "http://example.com/recursive.json", | ||
"properties": { | ||
"b": { "$ref": "parent.json" } | ||
}, | ||
"required": ["b"] | ||
} | ||
} | ||
|
||
beforeEach(function() { | ||
ajv = Ajv({ loadSchema: loadSchema }); | ||
}); | ||
|
||
|
||
it('should compile schemas loading missing schemas with options.loadSchema function', function (done) { | ||
var schema = { | ||
"id": "http://example.com/parent.json", | ||
"properties": { | ||
"a": { "$ref": "object.json" } | ||
} | ||
}; | ||
ajv.compileAsync(schema, function (err, validate) { | ||
should.not.exist(err); | ||
validate .should.be.a('function'); | ||
validate({ a: { b: 2 } }) .should.equal(true); | ||
validate({ a: { b: 1 } }) .should.equal(false); | ||
done(); | ||
}); | ||
}); | ||
|
||
|
||
it('should correctly load schemas when missing reference has JSON path', function (done) { | ||
var schema = { | ||
"id": "http://example.com/parent.json", | ||
"properties": { | ||
"a": { "$ref": "object.json#/properties/b" } | ||
} | ||
}; | ||
ajv.compileAsync(schema, function (err, validate) { | ||
should.not.exist(err); | ||
validate .should.be.a('function'); | ||
validate({ a: 2 }) .should.equal(true); | ||
validate({ a: 1 }) .should.equal(false); | ||
done(); | ||
}); | ||
}); | ||
|
||
|
||
it.skip('should correctly compile with remote schemas that have mutual references', function (done) { | ||
var schema = { | ||
"id": "http://example.com/root.json", | ||
"properties": { | ||
"tree": { "$ref": "tree.json" } | ||
} | ||
}; | ||
ajv.compileAsync(schema, function (err, validate) { | ||
should.not.exist(err); | ||
validate .should.be.a('function'); | ||
var validData = { tree: [ | ||
{ name: 'a', subtree: [ { name: 'a.a' } ] }, | ||
{ name: 'b' } | ||
] }; | ||
var invalidData = { tree: [ | ||
{ name: 'a', subtree: [ { name: 1 } ] } | ||
] }; | ||
validate(validData) .should.equal(true); | ||
validate(invalidData) .should.equal(false); | ||
done(); | ||
}); | ||
}); | ||
|
||
|
||
it('should correctly compile with remote schemas that reference the compiled schema', function (done) { | ||
var schema = { | ||
"id": "http://example.com/parent.json", | ||
"properties": { | ||
"a": { "$ref": "recursive.json" } | ||
} | ||
}; | ||
ajv.compileAsync(schema, function (err, validate) { | ||
should.not.exist(err); | ||
validate .should.be.a('function'); | ||
var validData = { a: { b: { a: { b: {} } } } }; | ||
var invalidData = { a: { b: { a: {} } } }; | ||
validate(validData) .should.equal(true); | ||
validate(invalidData) .should.equal(false); | ||
done(); | ||
}); | ||
}); | ||
|
||
|
||
function loadSchema(uri, callback) { | ||
setTimeout(function() { | ||
if (SCHEMAS[uri]) callback(null, SCHEMAS[uri]); | ||
else callback(new Error('404')); | ||
}, 10); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Maybe remove
console.log
in production?