Fix Node._findSchema bug when path is not present in schema
Previously, the function would return the wrong schema when it was unable to find a path in the given schema.
This commit is contained in:
parent
f36cecdfde
commit
4f4a733fd6
|
@ -2634,11 +2634,15 @@ Node._findSchema = function (schema, schemaRefs, path) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (typeof key === 'string' && childSchema.properties) {
|
else if (typeof key === 'string' && childSchema.properties) {
|
||||||
childSchema = childSchema.properties[key] || null;
|
if (!(key in childSchema.properties)) {
|
||||||
|
foundSchema = null;
|
||||||
|
} else {
|
||||||
|
childSchema = childSchema.properties[key];
|
||||||
if (childSchema) {
|
if (childSchema) {
|
||||||
foundSchema = Node._findSchema(childSchema, schemaRefs, nextPath);
|
foundSchema = Node._findSchema(childSchema, schemaRefs, nextPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (typeof key === 'number' && childSchema.items) {
|
else if (typeof key === 'number' && childSchema.items) {
|
||||||
childSchema = childSchema.items;
|
childSchema = childSchema.items;
|
||||||
if (childSchema) {
|
if (childSchema) {
|
||||||
|
@ -2648,6 +2652,12 @@ Node._findSchema = function (schema, schemaRefs, path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the found schema is the input schema, the schema does not have the given path
|
||||||
|
if (foundSchema === schema && path.length > 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return foundSchema
|
return foundSchema
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,26 @@ describe('Node', function () {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return null for path that has no schema', function () {
|
||||||
|
var schema = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
foo: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
baz: {
|
||||||
|
type: 'number'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var path = ['bar'];
|
||||||
|
assert.strictEqual(Node._findSchema(schema, {}, path), null);
|
||||||
|
path = ['foo', 'bar'];
|
||||||
|
assert.strictEqual(Node._findSchema(schema, {}, path), null);
|
||||||
|
});
|
||||||
|
|
||||||
describe('with $ref', function () {
|
describe('with $ref', function () {
|
||||||
it('should find a referenced schema', function () {
|
it('should find a referenced schema', function () {
|
||||||
var schema = {
|
var schema = {
|
||||||
|
@ -212,6 +232,36 @@ describe('Node', function () {
|
||||||
'second pattern property child'
|
'second pattern property child'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return null for path that has no schema', function () {
|
||||||
|
var schema = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
levelTwo: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
levelThree: {
|
||||||
|
type: 'number'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
patternProperties: {
|
||||||
|
'^foo[0-9]': {
|
||||||
|
title: 'foo[0-9] pattern property',
|
||||||
|
type: 'string'
|
||||||
|
},
|
||||||
|
'^bar[0-9]': {
|
||||||
|
title: 'bar[0-9] pattern property',
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var path = ['not-in-schema'];
|
||||||
|
assert.strictEqual(Node._findSchema(schema, {}, path), null);
|
||||||
|
path = ['levelOne', 'not-in-schema'];
|
||||||
|
assert.strictEqual(Node._findSchema(schema, {}, path), null);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue