UNPKG

5.73 kBJavaScriptView Raw
1const assert = require('assert');
2
3const { Validator } = require('../lib/index');
4
5const input = {
6 product: {
7 id: '1',
8 name: 'empty',
9 price: '12.50',
10 features: [
11 { name: 'Awesome', value: 'awesome' },
12 { name: 'Weak', value: 'weak' },
13 ],
14 },
15 cart: [
16 {
17 id: 1,
18 colors: {
19 name: 'black',
20 },
21 features: [11, 12, 13],
22 varients: [
23 { id: 11 },
24 { id: 12 },
25 {
26
27 id: 13,
28 colors: [
29 {
30 name: 'Varient Black',
31 props: [
32 {
33 name: 'p0',
34 tags: ['p01', 12, 13],
35 },
36 ],
37 },
38 {
39 name: 'Varitnt Blue',
40 props: [
41 {
42 name: 'p1',
43 tags: [3, 2],
44 },
45 ],
46 },
47 ],
48 },
49 { id: 14 },
50 { id: 15 },
51 { id: 16 },
52 { id: 17 },
53 { id: 18 },
54 { id: 19 },
55 { id: 20 },
56 { id: null }
57 ],
58 },
59 {
60 id: 2,
61 colors: {
62 name: '#fff',
63 },
64 varients: [
65 {
66 id: 21,
67 colors: [
68 {
69 name: '#f00', // cart.1.varients.1.colors.name
70 props: [
71 {
72 name: 'p02',
73 tags: [9, 8, 7],
74 },
75 ],
76 },
77 {
78 name: '#000000',
79 },
80 {
81 name: 'fail',
82 props: [
83 {
84 name: 'p12',
85 tags: [7, 8],
86 },
87 ],
88 },
89 ],
90 },
91 { id: 22 },
92 ],
93 },
94 ],
95};
96
97describe('Nested', () => {
98 describe('Single Level', () => {
99 it('should pass with top level required', async () => {
100 const v = new Validator(
101 {
102 features: input.product.features,
103 },
104 {
105 features: 'required|array',
106 'features.*.name': 'string',
107 'features.*.value': 'string',
108 },
109 );
110
111 const matched = await v.check();
112
113 assert.equal(matched, true);
114 });
115
116 it('should fail with child level', async () => {
117 const v = new Validator(
118 {
119 features: input.product.features,
120 },
121 {
122 features: 'required|array',
123 'features.*.name': 'string',
124 'features.*.value': 'integer',
125 },
126 );
127
128 const matched = await v.check();
129 assert.equal(matched, false);
130 v.errors.should.have.keys('features.0.value', 'features.1.value');
131 });
132 });
133
134 describe('Deep Level', () => {
135 // it('should pass with top level required', async () => {
136 // const v = new Validator(
137 // input,
138 // {
139 // product: 'required|object',
140 // 'product.id': 'required|integer',
141 // 'product.name': 'required|string',
142 // 'product.price': 'required|numeric',
143 // 'cart.*.id': 'required|integer',
144 // 'cart.*.features': 'required|array',
145 // 'cart.*.features.*': 'required|integer',
146 // 'cart.*.colors.name': 'required|string',
147 // 'cart.*.varients': 'required|array',
148 // 'cart.*.varients.*.id': 'required|integer',
149 // 'cart.*.varients.*.colors.*.name': 'required|string',
150
151 // },
152 // );
153
154 // const matched = await v.check();
155
156 // assert.equal(matched, true);
157 // });
158
159 it('should fail with child level required', async () => {
160 const v = new Validator(
161 input,
162 {
163 product: 'required|object',
164 'product.id': 'required|integer',
165 'product.name': 'required|string|minLength:10',
166 'product.price': 'required|numeric',
167 'product.features': 'required|array',
168 'product.features.*.name': 'required|string',
169 'product.features.*.value': 'required|integer',
170 'cart.*.id': 'required|integer',
171 'cart.*.colors': 'required|object',
172 'cart.*.colors.name': 'required|hexColor',
173 'cart.*.varients': 'required|array',
174 'cart.*.varients.*.id': 'required|integer',
175 'cart.*.varients.*.colors': 'required|array',
176 'cart.*.varients.*.colors.*.name': 'required|hexColor',
177 'cart.*.varients.*.colors.*.props.*.name': 'required|integer',
178 },
179 );
180
181 const matched = await v.check();
182
183 assert.equal(matched, false);
184
185 v.errors.should.have.keys('product.name',
186 'product.features.0.value',
187 'product.features.1.value');
188
189
190 v.errors.should.have.keys('cart.0.colors.name',
191 'cart.0.varients.2.colors.0.name',
192 'cart.0.varients.2.colors.1.name',
193 'cart.1.varients.0.colors.2.name',
194 'cart.1.varients.0.colors.2.props.0.name');
195 });
196
197
198 it('should fail with deep child level length', async () => {
199 const v = new Validator(
200 input,
201 {
202 'cart.*.varients.*.colors.*.props': 'required|array',
203 'cart.*.varients.*.colors.*.props.*.tags': 'required|lengthBetween:3,5',
204
205 },
206 );
207
208 const matched = await v.check();
209
210 assert.equal(matched, false);
211
212
213 v.errors.should.have.keys('cart.1.varients.0.colors.2.props.0.tags');
214 });
215
216 it('should fail with child required at array index > 9', async () => {
217 const v = new Validator(
218 input,
219 {
220 'cart.*.varients.*.id': 'required',
221 },
222 );
223
224 const matched = await v.check();
225
226 assert.equal(matched, false);
227
228 v.errors.should.have.keys('cart.0.varients.10.id');
229 });
230 });
231});