UNPKG

5.41 kBJavaScriptView Raw
1const assert = require('assert');
2require('should');
3
4const niv = require('../lib/index');
5
6const { Validator, setLang, niceNames } = niv;
7
8niv.extendMessages({
9 even: 'The value of the field must be an even number.',
10 status: 'Invalid status.',
11});
12
13niv.extendMessages({
14 even: 'Even number bharo.',
15 status: 'Galat Status.',
16}, 'pb');
17
18
19niv.addCustomMessages({
20 'status.required': 'Status khali nahi hona chahiye.',
21}, 'hi');
22
23niv.addCustomMessages({
24 'status.required': 'Status khali nahi hona chahiye.',
25});
26
27niv.extend('even', ({ value }) => {
28 if ((parseInt(value) % 2) === 0) {
29 return true;
30 }
31
32 return false;
33});
34
35niv.extend('status', ({ value, args }) => {
36 if (args.indexOf(value) >= 0) {
37 return true;
38 }
39
40 return false;
41});
42
43niv.extend('sumOfFields', ({ value, args }, v) => {
44 if (args.length !== 2) {
45 throw new Error('Invalid seed for rule sumOfFields');
46 }
47
48 const anotherValue = Number(v.inputs[args[0]]);
49
50 const eq = Number(args[1]);
51
52 if ((Number(value) + anotherValue) !== eq) {
53 return false;
54 }
55
56 return true;
57});
58
59
60describe('Custom Rules', () => {
61 it('sumOfFields:should pass', async () => {
62 const v = new Validator(
63 { num1: '50', num2: '50' }, { num1: 'sumOfFields:num2,100|required' },
64 );
65
66 const matched = await v.check();
67
68 assert.equal(matched, true);
69 });
70
71 it('sumOfFields:should fails, value is greater', async () => {
72 const v = new Validator(
73 { num1: '50', num2: '51' }, { num1: 'sumOfFields:num2,100|required' },
74 );
75
76 const matched = await v.check();
77
78 assert.equal(matched, false);
79 });
80
81 it('sumOfFields:should fail, value is less', async () => {
82 const v = new Validator(
83 { num1: '50', num2: '49' }, { num1: 'sumOfFields:num2,100|required' },
84 );
85
86 const matched = await v.check();
87
88 assert.equal(matched, false);
89 });
90
91
92 it('even:should pass', async () => {
93 const v = new Validator(
94 { number: '4' }, { number: 'even|required' },
95 );
96
97 const matched = await v.check();
98
99 assert.equal(matched, true);
100 });
101
102 it('even:should fail', async () => {
103 setLang('en');
104 const v = new Validator(
105 { number: '9' }, { number: 'even|required' },
106 );
107
108 const matched = await v.check();
109
110 assert.equal(matched, false);
111
112 assert.equal(
113 v.errors.number.message,
114 v.getExistinParsedMessage({
115 rule: 'even',
116 value: '9',
117 attr: 'number',
118 args: [],
119 }),
120 );
121 });
122
123 it('even:should fail', async () => {
124 setLang('pb');
125 const v = new Validator(
126 { number: '9' }, { number: 'even|required' },
127 );
128
129 const matched = await v.check();
130
131 assert.equal(matched, false);
132
133 assert.equal(
134 v.errors.number.message,
135 'Even number bharo.',
136 );
137 setLang('en');
138 });
139
140 it('status:should pass', async () => {
141 const v = new Validator(
142 { status: 'draft' }, { status: 'status:draft,published|required' },
143 );
144
145 const matched = await v.check();
146
147 assert.equal(matched, true);
148 });
149
150 it('status:should fail', async () => {
151 niceNames({
152 status: 'STATUS Attribute',
153 });
154 const v = new Validator(
155 { status: 'completed' }, { status: 'status:draft,published|required' },
156 );
157
158 const matched = await v.fails();
159
160 assert.equal(matched, true);
161
162 assert.equal(
163 v.errors.status.message,
164 v.getExistinParsedMessage({
165 rule: 'status',
166 value: 'completed',
167 attr: 'STATUS Attribute',
168 args: [],
169 }),
170 );
171 });
172 niceNames({
173 status: 'status',
174 });
175});
176
177describe('Custom messages', () => {
178 it('should return status.required custom message', async () => {
179 const v = new Validator(
180 { status: '' },
181 { status: 'required' },
182 );
183
184 const matched = await v.check();
185
186 assert.equal(matched, false);
187
188 v.errors.should.have.property('status').and.be.a.Object();
189 v.errors.status.should.have.property('message', 'Status khali nahi hona chahiye.');
190 });
191
192 it('should return custom message for required', async () => {
193 const v = new Validator(
194 { number: '' },
195 { number: 'even|required' },
196 { 'number.required': 'Number is missing.' },
197 );
198
199 const matched = await v.check();
200
201 assert.equal(matched, false);
202
203 v.errors.should.have.property('number').and.be.a.Object();
204 v.errors.number.should.have.property('message', 'Number is missing.');
205 });
206
207 it('should return custom message for even', async () => {
208 const v = new Validator(
209 { number: '9' },
210 { number: 'even|required' },
211 { 'number.even': 'Invalid number :value.' },
212 );
213
214 const matched = await v.check();
215
216 assert.equal(matched, false);
217
218 v.errors.should.have.property('number').and.be.a.Object();
219 v.errors.number.should.have.property('message', 'Invalid number 9.');
220 });
221});
222
223describe('Nice Names', () => {
224 it('should change attribute name in message', async () => {
225 const v = new Validator(
226 { status: 'draft' },
227 { status: 'status:pending,published|required' },
228 { status: 'The :attribute value is invalid.' },
229 );
230
231 v.niceNames({
232 status: 'queue status',
233 });
234
235 const matched = await v.check();
236
237 assert.equal(matched, false);
238
239 v.errors.should.have.property('status').and.be.a.Object();
240 v.errors.status.should.have.property('message', 'The queue status value is invalid.');
241 });
242});