Skip to content

Commit

Permalink
Search node schema in all possible sub schemas (#1297)
Browse files Browse the repository at this point in the history
* Search node schema in all possible sub schemas

* Apply linter fixes
  • Loading branch information
maufl committed Apr 17, 2021
1 parent 5bed047 commit fd948ff
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/js/Node.js
Expand Up @@ -4453,7 +4453,12 @@ Node._findSchema = (topLevelSchema, schemaRefs, path, currentSchema = topLevelSc
const nextPath = path.slice(1, path.length)
const nextKey = path[0]

const possibleSchemas = currentSchema.oneOf || currentSchema.anyOf || currentSchema.allOf || [currentSchema]
let possibleSchemas = [currentSchema]
for (const subSchemas of [currentSchema.oneOf, currentSchema.anyOf, currentSchema.allOf]) {
if (Array.isArray(subSchemas)) {
possibleSchemas = possibleSchemas.concat(subSchemas)
}
}

for (const schema of possibleSchemas) {
currentSchema = schema
Expand Down
38 changes: 38 additions & 0 deletions test/Node.test.js
Expand Up @@ -97,6 +97,44 @@ describe('Node', () => {
assert.strictEqual(Node._findSchema(schema, {}, path), null)
})

it('should find one of required properties', () => {
const schema = {
properties: {
company: {
type: 'string',
enum: ['1', '2']
},
worker: {
type: 'string',
enum: ['a', 'b']
},
manager: {
type: 'string',
enum: ['c', 'd']
}
},
additionalProperties: false,
oneOf: [
{
required: ['worker']
},
{
required: ['manager']
}
]
}
let path = ['company']
assert.deepStrictEqual(Node._findSchema(schema, {}, path), {
type: 'string',
enum: ['1', '2']
})
path = ['worker']
assert.deepStrictEqual(Node._findSchema(schema, {}, path), {
type: 'string',
enum: ['a', 'b']
})
})

describe('with $ref', () => {
it('should find a referenced schema', () => {
const schema = {
Expand Down

0 comments on commit fd948ff

Please sign in to comment.