UNPKG

3.96 kBJavaScriptView Raw
1const assert = require('assert');
2
3const { Validator } = require('../lib/index');
4
5
6describe('Objects', () => {
7 describe('Single Level', () => {
8 it('should pass with top level required', async () => {
9 const v = new Validator(
10 {
11 product: {
12 id: '1', name: 'Product', price: '12.50', active: 'yes',
13 },
14 },
15 {
16 product: 'required|object',
17 'product.id': 'integer',
18 'product.name': 'string',
19 'product.price': 'numeric',
20 'product.active': 'string',
21 },
22 {
23 product: 'The given product is invalid :value.',
24 },
25 );
26
27 const matched = await v.check();
28
29 assert.equal(matched, true);
30 });
31
32 it('should fail with child level required', async () => {
33 const v = new Validator(
34 {
35 product: {
36 id: 1, name: '', price: '', active: 'yes',
37 },
38 },
39 {
40 product: 'required|object',
41 'product.id': 'required|integer',
42 'product.name': 'required|string',
43 'product.price': 'required|numeric',
44 'product.active': 'required|string',
45 },
46 {
47 product: 'The given product is invalid :value.',
48 },
49 );
50
51 const matched = await v.check();
52
53 assert.equal(matched, false);
54 v.errors.should.have.keys('product.name', 'product.price');
55 });
56 });
57
58 describe('Deep Level', () => {
59 it('should pass with top level required', async () => {
60 const v = new Validator(
61 {
62 product: {
63 id: '1',
64 name: 'Product',
65 price: '12.50',
66 weight: { unit: 'gram', value: 100 },
67 },
68 },
69 {
70 product: 'required|object',
71 'product.id': 'integer',
72 'product.name': 'string',
73 'product.price': 'numeric',
74 'product.weight': 'object',
75 'product.weight.unit': 'in:gram',
76 'product.weight.value': 'numeric',
77 },
78 {
79 product: 'The given product is invalid :value.',
80 },
81 );
82
83 const matched = await v.check();
84
85 assert.equal(matched, true);
86 });
87
88 it('should fail with child level required', async () => {
89 const v = new Validator(
90 {
91 product: {
92 id: '1',
93 name: '',
94 price: '12.50',
95 weight: { unit: 'kilo', value: '' },
96 },
97 },
98 {
99 product: 'required|object',
100 'product.id': 'required|integer',
101 'product.name': 'required|string',
102 'product.price': 'required|numeric',
103 'product.weight': 'required|object',
104 'product.weight.unit': 'required|in:gram',
105 'product.weight.value': 'required|numeric',
106 },
107 {
108 product: 'The given product is invalid :value.',
109 },
110 );
111
112 const matched = await v.check();
113
114 assert.equal(matched, false);
115
116 v.errors.should.have.keys('product.name', 'product.weight.unit', 'product.weight.value');
117 });
118
119 it('should fail with child level missing', async () => {
120 const v = new Validator(
121 {
122 product: {
123 id: '1',
124 name: '',
125 price: '12.50',
126 },
127 },
128 {
129 product: 'required|object',
130 'product.id': 'required|integer',
131 'product.name': 'required|string',
132 'product.price': 'required|numeric',
133 'product.weight': 'required|object',
134 'product.weight.unit': 'required|in:gram',
135 'product.weight.value': 'required|numeric',
136 },
137 {
138 product: 'The given product is invalid :value.',
139 },
140 );
141
142 const matched = await v.check();
143
144 assert.equal(matched, false);
145
146 v.errors.should.have.keys('product.name', 'product.weight', 'product.weight.unit', 'product.weight.value');
147 });
148 });
149});