-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathMixedSchema.test.js
More file actions
115 lines (103 loc) · 2.88 KB
/
MixedSchema.test.js
File metadata and controls
115 lines (103 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict'
const { describe, it } = require('node:test')
const assert = require('node:assert/strict')
const { MixedSchema } = require('./MixedSchema')
const S = require('./FluentJSONSchema')
describe('MixedSchema', () => {
it('defined', () => {
assert.notStrictEqual(MixedSchema, undefined)
})
it('Expose symbol / 1', () => {
assert.notStrictEqual(
MixedSchema()[Symbol.for('fluent-schema-object')],
undefined
)
})
it('Expose symbol / 2', () => {
const types = [
S.TYPES.STRING,
S.TYPES.NUMBER,
S.TYPES.BOOLEAN,
S.TYPES.INTEGER,
S.TYPES.OBJECT,
S.TYPES.ARRAY,
S.TYPES.NULL
]
assert.notStrictEqual(
MixedSchema(types)[Symbol.for('fluent-schema-object')],
undefined
)
})
describe('factory', () => {
it('without params', () => {
assert.deepStrictEqual(MixedSchema().valueOf(), {
[Symbol.for('fluent-schema-object')]: true
})
})
})
describe('from S', () => {
it('valid', () => {
const types = [
S.TYPES.STRING,
S.TYPES.NUMBER,
S.TYPES.BOOLEAN,
S.TYPES.INTEGER,
S.TYPES.OBJECT,
S.TYPES.ARRAY,
S.TYPES.NULL
]
assert.deepStrictEqual(S.mixed(types).valueOf(), {
$schema: 'http://json-schema.org/draft-07/schema#',
type: types
})
})
it('invalid param', () => {
const types = ''
assert.throws(
() => S.mixed(types),
(err) =>
err instanceof S.FluentSchemaError &&
err.message ===
"Invalid 'types'. It must be an array of types. Valid types are string | number | boolean | integer | object | array | null"
)
})
it('invalid type', () => {
const types = ['string', 'invalid']
assert.throws(
() => S.mixed(types),
(err) =>
err instanceof S.FluentSchemaError &&
err.message ===
"Invalid 'types'. It must be an array of types. Valid types are string | number | boolean | integer | object | array | null"
)
})
})
it('sets a type object to the prop', () => {
assert.deepStrictEqual(
S.object()
.prop(
'prop',
S.mixed([S.TYPES.STRING, S.TYPES.NUMBER]).minimum(10).maxLength(5)
)
.valueOf(),
{
$schema: 'http://json-schema.org/draft-07/schema#',
properties: {
prop: { maxLength: 5, minimum: 10, type: ['string', 'number'] }
},
type: 'object'
}
)
})
describe('raw', () => {
it('allows to add a custom attribute', () => {
const types = [S.TYPES.STRING, S.TYPES.NUMBER]
const schema = S.mixed(types).raw({ customKeyword: true }).valueOf()
assert.deepStrictEqual(schema, {
$schema: 'http://json-schema.org/draft-07/schema#',
type: ['string', 'number'],
customKeyword: true
})
})
})
})