support resolving internal refs from macro keywords

master
Jonathan Stewmon 2018-06-21 15:57:39 -05:00
parent 0020556493
commit 4865e6e894
2 changed files with 39 additions and 1 deletions

View File

@ -70,7 +70,7 @@ function resolveSchema(root, ref) {
var p = URI.parse(ref)
, refPath = _getFullPath(p)
, baseId = getFullPath(this._getId(root.schema));
if (refPath !== baseId) {
if (Object.keys(root.schema).length === 0 || refPath !== baseId) {
var id = normalizeId(refPath);
var refVal = this._refs[id];
if (typeof refVal == 'string') {

View File

@ -209,6 +209,44 @@ describe('Custom keywords', function () {
testMultipleRangeKeyword({ type: 'number', macro: macroRange }, 2);
});
it('should support resolving $ref without id or $id', function () {
instances.forEach(function (_ajv) {
_ajv.addKeyword('macroRef', {
macro: function (schema, parentSchema, it) {
it.baseId .should.equal('#');
var ref = schema.$ref;
var validate = _ajv.getSchema(ref);
if (validate) return validate.schema;
throw new ajv.constructor.MissingRefError(it.baseId, ref);
},
metaSchema: {
"type": "object",
"required": [ "$ref" ],
"additionalProperties": false,
"properties": {
"$ref": {
"type": "string"
}
}
}
});
var schema = {
"macroRef": {
"$ref": "#/definitions/schema"
},
"definitions": {
"schema": {
"type": "string"
}
}
};
var validate;
(function compileMacroRef () { validate = _ajv.compile(schema); }).should.not.throw();
shouldBeValid(validate, 'foo');
shouldBeInvalid(validate, 1, 2);
});
});
it('should recursively expand macro keywords', function() {
instances.forEach(function (_ajv) {
_ajv.addKeyword('deepProperties', { type: 'object', macro: macroDeepProperties });