forked from ajv-validator/ajv-merge-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support async schema/fragment loading, closes ajv-validator#5
- Loading branch information
1 parent
6bc3853
commit 0466383
Showing
4 changed files
with
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
var Ajv = require('ajv'); | ||
var addKeywords = require('..'); | ||
var test = require('./test_validate'); | ||
var assert = require('assert'); | ||
|
||
describe('async schema loading', function() { | ||
var ajv, loadCount; | ||
|
||
beforeEach(function() { | ||
ajv = new Ajv({v5: true, loadSchema: loadSchema}); | ||
addKeywords(ajv); | ||
loadCount = 0; | ||
}); | ||
|
||
describe('$merge', function() { | ||
it('should load missing schemas', function (done) { | ||
var schema = { | ||
"$merge": { | ||
"source": { "$ref": "obj.json#" }, | ||
"with": { | ||
"properties": { "q": { "type": "number" } } | ||
} | ||
} | ||
}; | ||
|
||
testAsync(schema, '$merge', done); | ||
}); | ||
}); | ||
|
||
describe('$patch', function() { | ||
it('should load missing schemas', function (done) { | ||
var schema = { | ||
"$patch": { | ||
"source": { "$ref": "obj.json#" }, | ||
"with": [ | ||
{ "op": "add", "path": "/properties/q", "value": { "type": "number" } } | ||
] | ||
} | ||
}; | ||
|
||
testAsync(schema, '$patch', done); | ||
}); | ||
}); | ||
|
||
function testAsync(schema, keyword, done) { | ||
ajv.compileAsync(schema, function (err, validate) { | ||
if (err) done(err); | ||
assert.strictEqual(loadCount, 1); | ||
test(validate, keyword); | ||
done(); | ||
}); | ||
} | ||
|
||
function loadSchema(ref, callback) { | ||
if (ref == 'obj.json') { | ||
loadCount++; | ||
return callback(null, { | ||
"id": "obj.json#", | ||
"type": "object", | ||
"properties": { "p": { "type": "string" } }, | ||
"additionalProperties": false | ||
}); | ||
} | ||
callback(new Error('404: ' + ref)); | ||
} | ||
}); |
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