Skip to content

Commit

Permalink
feat: support async schema/fragment loading, closes ajv-validator#5
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Nov 24, 2016
1 parent 6bc3853 commit 0466383
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
5 changes: 4 additions & 1 deletion keywords/add_keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ module.exports = function (ajv, keyword, jsonPatch, patchSchema) {
: $ref;
var validate = ajv.getSchema(id);
if (validate) return validate.schema;
throw new Error('can\'t resolve reference ' + $ref + ' from id ' + it.baseId);
var err = new Error('can\'t resolve reference ' + $ref + ' from id ' + it.baseId);
err.missingRef = it.resolve.url(it.baseId, $ref);
err.missingSchema = it.resolve.normalizeId(it.resolve.fullPath(err.missingRef));
throw err;
}
},
metaSchema: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv-merge-patch",
"version": "2.0.2",
"version": "2.1.0",
"description": "$merge and $patch keywords for Ajv JSON-Schema validator to extend schemas",
"main": "index.js",
"scripts": {
Expand Down
68 changes: 68 additions & 0 deletions spec/async.spec.js
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));
}
});
8 changes: 4 additions & 4 deletions spec/patch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe('keyword $patch', function() {
});

it('should extend schema defined in $ref', function() {
ajvInstances.forEach(testMerge);
ajvInstances.forEach(testPatch);

function testMerge(ajv) {
function testPatch(ajv) {
var sourceSchema = {
"id": "obj.json#",
"type": "object",
Expand All @@ -64,9 +64,9 @@ describe('keyword $patch', function() {
});

it('should extend schema defined with relative $ref', function() {
ajvInstances.forEach(testMerge);
ajvInstances.forEach(testPatch);

function testMerge(ajv) {
function testPatch(ajv) {
var schema = {
"id": "obj.json#",
"definitions": {
Expand Down

0 comments on commit 0466383

Please sign in to comment.