Merge branch 'fix/schema-ref-examples' of https://github.com/AdamVig/jsoneditor into AdamVig-fix/schema-ref-examples

This commit is contained in:
jos 2019-03-17 15:31:23 +01:00
commit 1d0870af36
3 changed files with 95 additions and 14 deletions

View File

@ -49,7 +49,8 @@
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
"minimum": 0,
"examples": [28, 32]
},
"job": {
"$ref": "job"
@ -74,7 +75,8 @@
},
"salary": {
"type": "number",
"minimum": 120
"minimum": 120,
"examples": [100, 110, 120]
}
}
};

View File

@ -2609,18 +2609,18 @@ Node._findSchema = function (schema, schemaRefs, path) {
for (var j = 0; j < allSchemas.length; j++) {
childSchema = allSchemas[j];
if ('$ref' in childSchema && typeof childSchema['$ref'] === 'string') {
childSchema = schemaRefs[childSchema['$ref']];
if (childSchema) {
foundSchema = Node._findSchema(childSchema, schemaRefs, path);
}
}
for (var i = 0; i < path.length && childSchema; i++) {
var nextPath = path.slice(i + 1, path.length);
var key = path[i];
// fix childSchema with $ref, and not display the select element on the child schema because of not found enum
if (typeof key === 'string' && childSchema['$ref']) {
childSchema = schemaRefs[childSchema['$ref']];
if (childSchema) {
foundSchema = Node._findSchema(childSchema, schemaRefs, nextPath);
}
}
else if (typeof key === 'string' && childSchema.patternProperties && !(childSchema.properties && key in childSchema.properties)) {
if (typeof key === 'string' && childSchema.patternProperties && !(childSchema.properties && key in childSchema.properties)) {
for (var prop in childSchema.patternProperties) {
if (key.match(prop)) {
foundSchema = Node._findSchema(childSchema.patternProperties[prop], schemaRefs, nextPath);
@ -2634,9 +2634,13 @@ Node._findSchema = function (schema, schemaRefs, path) {
}
}
else if (typeof key === 'string' && childSchema.properties) {
childSchema = childSchema.properties[key] || null;
if (childSchema) {
foundSchema = Node._findSchema(childSchema, schemaRefs, nextPath);
if (!(key in childSchema.properties)) {
foundSchema = null;
} else {
childSchema = childSchema.properties[key];
if (childSchema) {
foundSchema = Node._findSchema(childSchema, schemaRefs, nextPath);
}
}
}
else if (typeof key === 'number' && childSchema.items) {
@ -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
};

View File

@ -57,7 +57,46 @@ describe('Node', function () {
Node._findSchema(schema, {}, path),
schema.properties.levelTwo.properties.levelThree.properties.bool
);
})
});
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 () {
it('should find a referenced schema', function () {
var schema = {
type: 'object',
properties: {
foo: {
$ref: 'foo'
}
}
};
var fooSchema = {
type: 'number',
title: 'Foo'
};
var path = ['foo'];
assert.strictEqual(Node._findSchema(schema, {foo: fooSchema}, path), fooSchema);
});
});
describe('with pattern properties', function () {
it('should find schema', function () {
@ -193,6 +232,36 @@ describe('Node', function () {
'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);
});
});
});
});