-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcompile.js
154 lines (143 loc) · 3.27 KB
/
compile.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const AJS = require('..')
const { deepEqual } = require('./_util')
describe('compile', function () {
it('error: schema type', function () {
try {
AJS(111, 222)
} catch (e) {
deepEqual(!!e.message.match(/Schema must be object or array/), true)
}
})
it('error: schema value is null', function () {
try {
AJS({
type: null
})
} catch (e) {
deepEqual(!!e.message.match(/Schema key "type" is null, Schema is/), true)
}
})
it('leaf', function () {
const schema1 = AJS({ type: 'string' })
deepEqual(schema1, {
_leaf: true,
_children: { type: 'string' },
_parent: null,
_path: '$',
_schema: { type: 'string' }
})
const schema2 = AJS([{ type: 'string' }])
deepEqual(schema2, {
_array: true,
_leaf: true,
_children: { type: 'string' },
_parent: null,
_path: '$[]',
_schema: [{ type: 'string' }]
})
const schema3 = AJS('stringSchema', [{ type: 'string' }])
deepEqual(schema3, {
_array: true,
_leaf: true,
_name: 'stringSchema',
_children: { type: 'string' },
_parent: null,
_path: '$[]',
_schema: [{ type: 'string' }]
})
})
it('object', function () {
let schema1 = AJS({
author: {
type: 'string',
age: { type: 'number' }
}
})
let schema2 = AJS({
author: AJS({
type: 'string',
age: { type: 'number' }
})
})
try {
deepEqual(schema1, schema2)
} catch (e) {
deepEqual(e.message, 'Maximum call stack size exceeded')
}
schema1 = AJS({
author: {
type: { type: 'string' },
age: { type: 'number' }
}
})
schema2 = AJS({
author: AJS({
type: AJS({ type: 'string' }),
age: { type: 'number' }
})
})
try {
deepEqual(schema1, schema2)
} catch (e) {
deepEqual(e.message, 'Maximum call stack size exceeded')
}
schema1 = AJS({
author: {
name: { type: 'string' },
age: { type: 'number' }
}
})
schema2 = AJS({
author: AJS({
name: { type: 'string' },
age: { type: 'number' }
})
})
const schema3 = AJS({
author: AJS({
name: AJS({ type: 'string' }),
age: { type: 'number' }
})
})
try {
deepEqual(schema1, schema2)
} catch (e) {
deepEqual(e.message, 'Maximum call stack size exceeded')
}
try {
deepEqual(schema1, schema3)
} catch (e) {
deepEqual(e.message, 'Maximum call stack size exceeded')
}
})
it('array', function () {
const schema1 = AJS([{
authors: [{
names: [{ type: 'string' }],
age: { type: 'number' }
}]
}])
const schema2 = AJS([{
authors: AJS([{
names: [{ type: 'string' }],
age: { type: 'number' }
}])
}])
const schema3 = AJS([{
authors: AJS([{
names: AJS([{ type: 'string' }]),
age: { type: 'number' }
}])
}])
try {
deepEqual(schema1, schema2)
} catch (e) {
deepEqual(e.message, 'Maximum call stack size exceeded')
}
try {
deepEqual(schema1, schema3)
} catch (e) {
deepEqual(e.message, 'Maximum call stack size exceeded')
}
})
})