2019-08-28 19:21:14 +08:00
|
|
|
var assert = require('assert')
|
|
|
|
var setUpTestEnvironment = require('./setup')
|
|
|
|
setUpTestEnvironment()
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
var Node = require('../src/js/Node')
|
2019-02-16 21:01:30 +08:00
|
|
|
|
|
|
|
describe('Node', function () {
|
2019-08-28 19:21:14 +08:00
|
|
|
describe('_findSchema', function () {
|
|
|
|
it('should find schema', function () {
|
|
|
|
var schema = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
child: {
|
|
|
|
type: 'string'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var path = ['child']
|
|
|
|
assert.strictEqual(Node._findSchema(schema, {}, path), schema.properties.child)
|
|
|
|
})
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
it('should find schema inside an array item', function () {
|
|
|
|
var schema = {
|
|
|
|
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:21:14 +08:00
|
|
|
it('should find schema within multi-level object properties', function () {
|
|
|
|
var schema = {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var path = []
|
|
|
|
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:21:14 +08:00
|
|
|
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)
|
|
|
|
})
|
2019-03-16 03:29:08 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
2019-02-16 21:01:30 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
describe('with pattern properties', function () {
|
|
|
|
it('should find schema', function () {
|
|
|
|
var schema = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
str: {
|
|
|
|
title: 'str',
|
|
|
|
type: 'boolean'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
patternProperties: {
|
|
|
|
'^foo[0-9]': {
|
|
|
|
title: 'foo[0-] pattern property',
|
|
|
|
type: 'string'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var path = []
|
|
|
|
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:21:14 +08:00
|
|
|
it('should find schema within multi-level object properties', function () {
|
|
|
|
var schema = {
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var path = []
|
|
|
|
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:21:14 +08:00
|
|
|
it('should find schema for pattern properties', function () {
|
|
|
|
var schema = {
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var path = ['foo1']
|
|
|
|
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:21:14 +08:00
|
|
|
it('should find schema for multi-level pattern properties', function () {
|
|
|
|
var schema = {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var path = ['foo1', 'fooChild', 'fooChild2']
|
|
|
|
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'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|