1 | const assert = require('assert');
|
2 | const { Validator } = require('../lib/index');
|
3 |
|
4 | describe('Root Level Array', () => {
|
5 | it('should pass with array as root level', async () => {
|
6 | const v = new Validator(
|
7 | [
|
8 | { field: 'admin@example.com' },
|
9 | ],
|
10 | { '*.field': 'required|email' },
|
11 | );
|
12 |
|
13 | const matched = await v.check();
|
14 | assert.equal(matched, true);
|
15 | });
|
16 |
|
17 | it('should fail with array as root level', async () => {
|
18 | const v = new Validator(
|
19 | [
|
20 | { field: 'string' },
|
21 | ],
|
22 | { '*.field': 'required|email' },
|
23 | );
|
24 |
|
25 | const matched = await v.check();
|
26 | assert.equal(matched, false);
|
27 | v.errors.should.have.key('0.field');
|
28 | });
|
29 |
|
30 | it('should pass with array as root level contains nested object', async () => {
|
31 | const v = new Validator([
|
32 | {
|
33 | field: {
|
34 | email: 'admin@example.com'
|
35 | },
|
36 | },
|
37 | ],
|
38 | { '*.field.email': 'required|email' },);
|
39 |
|
40 | const matched = await v.check();
|
41 | assert.equal(matched, true);
|
42 | });
|
43 |
|
44 | it('should fail with array as root level contains nested object', async () => {
|
45 | const v = new Validator(
|
46 | [
|
47 | {
|
48 | field: {
|
49 | email: 'string'
|
50 | },
|
51 | },
|
52 | ],
|
53 | { '*.field.email': 'required|email' },
|
54 | );
|
55 | const matched = await v.check();
|
56 | assert.equal(matched, false);
|
57 | v.errors.should.have.key('0.field.email');
|
58 | });
|
59 |
|
60 | it('should pass with array as root level contains nested object', async () => {
|
61 | const v = new Validator(
|
62 | [
|
63 | {
|
64 | field: {
|
65 | mails: [
|
66 | {
|
67 | email: 'admin@example.com'
|
68 | }
|
69 | ],
|
70 | },
|
71 | },
|
72 | ],
|
73 | { '*.field.mails.*.email': 'required|email' }
|
74 | );
|
75 |
|
76 | const matched = await v.check();
|
77 | assert.equal(matched, true);
|
78 | });
|
79 |
|
80 | it('should fail with array as root level contains nested object', async () => {
|
81 | const v = new Validator(
|
82 | [
|
83 | {
|
84 | field: {
|
85 | mails: [
|
86 | {
|
87 | email: 'admin@example.com'
|
88 | },
|
89 | {
|
90 | email: 'string'
|
91 | },
|
92 | {
|
93 | email: 'admin@example.com'
|
94 | }
|
95 | ],
|
96 | },
|
97 | },
|
98 | ],
|
99 | { '*.field.mails.*.email': 'required|email' }
|
100 | );
|
101 |
|
102 | const matched = await v.check();
|
103 | assert.equal(matched, false);
|
104 | v.errors.should.have.key('0.field.mails.1.email');
|
105 | });
|
106 | });
|