2019-08-31 17:21:29 +08:00
|
|
|
import assert from 'assert'
|
|
|
|
import './setup'
|
2019-08-31 23:38:57 +08:00
|
|
|
import { Node } from '../src/js/Node'
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
describe('Node', () => {
|
|
|
|
describe('_findSchema', () => {
|
|
|
|
it('should find schema', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
child: {
|
|
|
|
type: 'string'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
const path = ['child']
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema.properties.child)
|
|
|
|
})
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should find schema inside an array item', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
properties: {
|
|
|
|
job: {
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
company: {
|
|
|
|
enum: ['test1', 'test2']
|
2019-02-16 21:01:30 +08:00
|
|
|
}
|
2019-08-28 19:21:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, []), schema)
|
2019-05-29 21:51:42 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, ['job']), schema.properties.job)
|
2019-05-29 21:51:42 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, ['job', 0]),
|
|
|
|
schema.properties.job.items)
|
2019-05-29 21:51:42 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, ['job', 0, 'company']),
|
|
|
|
schema.properties.job.items.properties.company)
|
|
|
|
})
|
2019-05-29 21:51:42 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should find schema within multi-level object properties', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
levelTwo: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
levelThree: {
|
2019-02-16 21:01:30 +08:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
2019-08-28 19:21:14 +08:00
|
|
|
bool: {
|
|
|
|
type: 'boolean'
|
|
|
|
}
|
2019-02-16 21:01:30 +08:00
|
|
|
}
|
2019-08-28 19:21:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
let path = []
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema)
|
|
|
|
path = ['levelTwo']
|
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema.properties.levelTwo)
|
|
|
|
path = ['levelTwo', 'levelThree']
|
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema.properties.levelTwo.properties.levelThree)
|
|
|
|
path = ['levelTwo', 'levelThree', 'bool']
|
|
|
|
assert.strictEqual(
|
|
|
|
Node._findSchema(schema, {}, path),
|
|
|
|
schema.properties.levelTwo.properties.levelThree.properties.bool
|
|
|
|
)
|
|
|
|
})
|
2019-03-15 04:12:03 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should return null for path that has no schema', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
foo: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
baz: {
|
|
|
|
type: 'number'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
let path = ['bar']
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), null)
|
|
|
|
path = ['foo', 'bar']
|
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), null)
|
|
|
|
})
|
2019-03-16 03:29:08 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
describe('with $ref', () => {
|
|
|
|
it('should find a referenced schema', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
foo: {
|
|
|
|
$ref: 'foo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
const fooSchema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
type: 'number',
|
|
|
|
title: 'Foo'
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
const path = ['foo']
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, { foo: fooSchema }, path), fooSchema)
|
|
|
|
})
|
|
|
|
})
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
describe('with pattern properties', () => {
|
|
|
|
it('should find schema', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
str: {
|
|
|
|
title: 'str',
|
|
|
|
type: 'boolean'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
patternProperties: {
|
|
|
|
'^foo[0-9]': {
|
|
|
|
title: 'foo[0-] pattern property',
|
|
|
|
type: 'string'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
let path = []
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema, 'top level')
|
|
|
|
path = ['str']
|
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema.properties.str, 'normal property')
|
|
|
|
})
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should find schema within multi-level object properties', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
levelTwo: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
levelThree: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
bool: {
|
|
|
|
title: 'bool',
|
|
|
|
type: 'boolean'
|
2019-02-16 21:01:30 +08:00
|
|
|
}
|
2019-08-28 19:21:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
patternProperties: {
|
|
|
|
'^foo[0-9]': {
|
|
|
|
title: 'foo[0-9] pattern property',
|
|
|
|
type: 'string'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
let path = []
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema, 'top level')
|
|
|
|
path = ['levelTwo']
|
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema.properties.levelTwo, 'level two')
|
|
|
|
path = ['levelTwo', 'levelThree']
|
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema.properties.levelTwo.properties.levelThree, 'level three')
|
|
|
|
path = ['levelTwo', 'levelThree', 'bool']
|
|
|
|
assert.strictEqual(
|
|
|
|
Node._findSchema(schema, {}, path),
|
|
|
|
schema.properties.levelTwo.properties.levelThree.properties.bool,
|
|
|
|
'normal property'
|
|
|
|
)
|
|
|
|
})
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should find schema for pattern properties', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
type: 'object',
|
|
|
|
patternProperties: {
|
|
|
|
'^foo[0-9]': {
|
|
|
|
title: 'foo[0-9] pattern property',
|
|
|
|
type: 'string'
|
|
|
|
},
|
|
|
|
'^bar[0-9]': {
|
|
|
|
title: 'bar[0-9] pattern property',
|
|
|
|
type: 'string'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
let path = ['foo1']
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(
|
|
|
|
Node._findSchema(schema, {}, path),
|
|
|
|
schema.patternProperties['^foo[0-9]'],
|
|
|
|
'first pattern property'
|
|
|
|
)
|
|
|
|
path = ['bar5']
|
|
|
|
assert.strictEqual(
|
|
|
|
Node._findSchema(schema, {}, path),
|
|
|
|
schema.patternProperties['^bar[0-9]'],
|
|
|
|
'second pattern property'
|
|
|
|
)
|
|
|
|
})
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should find schema for multi-level pattern properties', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
type: 'object',
|
|
|
|
patternProperties: {
|
|
|
|
'^foo[0-9]': {
|
|
|
|
title: 'foo[0-9] pattern property',
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
fooChild: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
fooChild2: {
|
|
|
|
type: 'string'
|
2019-02-16 21:01:30 +08:00
|
|
|
}
|
2019-08-28 19:21:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'^bar[0-9]': {
|
|
|
|
title: 'bar[0-9] pattern property',
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
barChild: {
|
|
|
|
type: 'string'
|
|
|
|
}
|
2019-03-16 03:29:08 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
let path = ['foo1', 'fooChild', 'fooChild2']
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(
|
|
|
|
Node._findSchema(schema, {}, path),
|
|
|
|
schema.patternProperties['^foo[0-9]'].properties.fooChild.properties.fooChild2,
|
|
|
|
'first pattern property child of child'
|
|
|
|
)
|
|
|
|
path = ['bar5', 'barChild']
|
|
|
|
assert.strictEqual(
|
|
|
|
Node._findSchema(schema, {}, path),
|
|
|
|
schema.patternProperties['^bar[0-9]'].properties.barChild,
|
|
|
|
'second pattern property child'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should return null for path that has no schema', () => {
|
|
|
|
const schema = {
|
2019-08-28 19:21:14 +08:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:43:06 +08:00
|
|
|
let path = ['not-in-schema']
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), null)
|
|
|
|
path = ['levelOne', 'not-in-schema']
|
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|