quival 0.2.8 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,20 +1,4 @@
1
1
  import { strict as assert } from 'assert';
2
- import {
3
- escapeRegExp,
4
- flattenObject,
5
- getByPath,
6
- isDigits,
7
- isEmpty,
8
- isNumeric,
9
- isPlainObject,
10
- isValidDate,
11
- parseCsvString,
12
- parseDate,
13
- parseDateByFormat,
14
- setByPath,
15
- toCamelCase,
16
- toSnakeCase,
17
- } from '../src/helpers.js';
18
2
  import Validator from '../src/Validator.js';
19
3
 
20
4
  globalThis.File = class {
@@ -29,163 +13,6 @@ globalThis.File = class {
29
13
  }
30
14
  };
31
15
 
32
- describe('Helpers', () => {
33
- it('toCamelCase', () => {
34
- assert.equal(toCamelCase('a_test_name'), 'aTestName');
35
- assert.equal(toCamelCase('_test_name'), 'testName');
36
- assert.equal(toCamelCase('a-test-name'), 'aTestName');
37
- assert.equal(toCamelCase('-test-name'), 'testName');
38
- assert.equal(toCamelCase('a test name'), 'aTestName');
39
- assert.equal(toCamelCase(' test name'), 'testName');
40
- });
41
-
42
- it('toSnakeCase', () => {
43
- assert.equal(toSnakeCase('aTestName'), 'a_test_name');
44
- assert.equal(toSnakeCase('a_testName'), 'a_test_name');
45
- assert.equal(toSnakeCase('ATESTNAME'), 'a_t_e_s_t_n_a_m_e');
46
- });
47
-
48
- it('escapeRegExp', () => {
49
- assert.equal(escapeRegExp('.*+?^${}()|[]\\'), '\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\');
50
- });
51
-
52
- it('getByPath', () => {
53
- assert.equal(getByPath({ a: { b: 1 } }, 'a.b'), 1);
54
- assert.equal(getByPath({ a: [1, { c: 2 }] }, 'a.1.c'), 2);
55
- });
56
-
57
- it('setByPath', () => {
58
- let obj = { a: { b: 1 } };
59
- setByPath(obj, 'a.b', 3);
60
-
61
- assert.deepEqual(obj, { a: { b: 3 } });
62
-
63
- obj = { a: [1, { c: 2 }] };
64
- setByPath(obj, 'a.1.c', 5);
65
-
66
- assert.deepEqual(obj, { a: [1, { c: 5 }] });
67
- });
68
-
69
- it('flattenObject', () => {
70
- assert.deepEqual(flattenObject({ a: 1, b: { x: 41, y: 42 } }), { a: 1, 'b.x': 41, 'b.y': 42 });
71
- assert.deepEqual(flattenObject({ a: 1, b: [71, 73, 75] }), { a: 1, 'b.0': 71, 'b.1': 73, 'b.2': 75 });
72
- });
73
-
74
- it('parseCsvString', () => {
75
- assert.deepEqual(parseCsvString('a,b,c'), ['a', 'b', 'c']);
76
- assert.deepEqual(parseCsvString('a, b, c'), ['a', ' b', ' c']);
77
- assert.deepEqual(parseCsvString('"a","b","c"'), ['a', 'b', 'c']);
78
- assert.deepEqual(parseCsvString('"a", "b", "c"'), ['a', 'b', 'c']);
79
- assert.deepEqual(parseCsvString('"a", "b", ""'), ['a', 'b', '']);
80
-
81
- assert.deepEqual(parseCsvString('"r2""c1","r2c2","r2c3"'), ['r2"c1', 'r2c2', 'r2c3']);
82
- assert.deepEqual(parseCsvString('"r2""c1",r2c2,r2c3'), ['r2"c1', 'r2c2', 'r2c3']);
83
- assert.deepEqual(parseCsvString('r1c1,"r1,c2",r1c3'), ['r1c1', 'r1,c2', 'r1c3']);
84
- assert.deepEqual(parseCsvString('"r2""c1",r2c2,"r2""""c3"'), ['r2"c1', 'r2c2', 'r2""c3']);
85
- assert.deepEqual(parseCsvString('r1c1,"r1,c2","r1"",""c3"'), ['r1c1', 'r1,c2', 'r1","c3']);
86
- });
87
-
88
- it('parseDate', () => {
89
- const date1 = new Date('2023/08/11 00:00:00');
90
- const date2 = new Date('2023/08/11 08:40:50 am');
91
- const date3 = new Date();
92
- date3.setHours(8);
93
- date3.setMinutes(40);
94
- date3.setSeconds(50);
95
- date3.setMilliseconds(0);
96
-
97
- assert.deepEqual(parseDate('11-08-2023'), date1);
98
- assert.deepEqual(parseDate('11-08-2023 08:40:50'), date2);
99
- assert.deepEqual(parseDate('11-08-2023 08:40:50 am'), date2);
100
- assert.deepEqual(parseDate('11/08/2023'), date1);
101
- assert.deepEqual(parseDate('11/08/2023 08:40:50'), date2);
102
- assert.deepEqual(parseDate('11/08/2023 08:40:50 am'), date2);
103
- assert.deepEqual(parseDate('11.08.2023'), date1);
104
- assert.deepEqual(parseDate('11.08.2023 08:40:50'), date2);
105
- assert.deepEqual(parseDate('11.08.2023 08:40:50 am'), date2);
106
-
107
- assert.deepEqual(parseDate('2023-08-11'), date1);
108
- assert.deepEqual(parseDate('2023-08-11 08:40:50'), date2);
109
- assert.deepEqual(parseDate('2023-08-11 08:40:50 am'), date2);
110
- assert.deepEqual(parseDate('2023/08/11'), date1);
111
- assert.deepEqual(parseDate('2023/08/11 08:40:50'), date2);
112
- assert.deepEqual(parseDate('2023/08/11 08:40:50 am'), date2);
113
- assert.deepEqual(parseDate('2023.08.11'), date1);
114
- assert.deepEqual(parseDate('2023.08.11 08:40:50'), date2);
115
- assert.deepEqual(parseDate('2023.08.11 08:40:50 am'), date2);
116
-
117
- assert.deepEqual(parseDate('08:40:50 2023-08-11'), date2);
118
- assert.deepEqual(parseDate('08:40:50 am 2023-08-11'), date2);
119
- assert.deepEqual(parseDate('08:40:50 2023/08/11'), date2);
120
- assert.deepEqual(parseDate('08:40:50 am 2023/08/11'), date2);
121
- assert.deepEqual(parseDate('08:40:50 2023.08.11'), date2);
122
- assert.deepEqual(parseDate('08:40:50 am 2023.08.11'), date2);
123
-
124
- assert.deepEqual(parseDate('08:40:50'), date3);
125
- assert.deepEqual(parseDate('08:40:50 am'), date3);
126
- assert.deepEqual(parseDate('08:40:50'), date3);
127
- assert.deepEqual(parseDate('08:40:50 am'), date3);
128
- assert.deepEqual(parseDate('08:40:50'), date3);
129
- assert.deepEqual(parseDate('08:40:50 am'), date3);
130
- });
131
-
132
- it('parseDateByFormat', () => {
133
- const date1 = new Date('2023/08/11 00:00:00');
134
- const date2 = new Date('2023/08/11 08:40:50 am');
135
- const date3 = new Date();
136
- date3.setHours(8);
137
- date3.setMinutes(40);
138
- date3.setSeconds(50);
139
- date3.setMilliseconds(0);
140
-
141
- assert.deepEqual(parseDateByFormat('08-11-2023', 'm-d-Y'), date1);
142
- assert.deepEqual(parseDateByFormat('08-11-23', 'm-d-y'), date1);
143
- assert.deepEqual(parseDateByFormat('08-11-2023 08:40:50', 'm-d-Y H:i:s'), date2);
144
- assert.deepEqual(parseDateByFormat('08-11-23 08:40:50', 'm-d-y H:i:s'), date2);
145
- assert.deepEqual(parseDateByFormat('08-11-2023 08:40:50 am', 'm-d-Y H:i:s a'), date2);
146
- assert.deepEqual(parseDateByFormat('08-11-23 08:40:50 am', 'm-d-y H:i:s a'), date2);
147
- });
148
-
149
- it('isDigits', () => {
150
- assert(isDigits(123));
151
- assert(isDigits('123'));
152
- assert(!isDigits('abc'));
153
- assert(!isDigits('abc123'));
154
- });
155
-
156
- it('isEmpty', () => {
157
- assert(isEmpty(''));
158
- assert(isEmpty(null));
159
- assert(isEmpty(undefined));
160
- assert(!isEmpty(123));
161
- assert(!isEmpty('abc'));
162
- assert(!isEmpty({}));
163
- assert(!isEmpty([]));
164
- });
165
-
166
- it('isNumeric', () => {
167
- assert(isNumeric(123));
168
- assert(isNumeric('123'));
169
- assert(!isNumeric('abc'));
170
- assert(!isNumeric(true));
171
- });
172
-
173
- it('isPlainObject', () => {
174
- assert(isPlainObject({}));
175
- assert(!isPlainObject([]));
176
- assert(!isPlainObject(new Date()));
177
- });
178
-
179
- it('isValidDate', () => {
180
- assert(isValidDate(new Date()));
181
- assert(isValidDate(new Date('2023/08/11 00:00:00')));
182
- assert(isValidDate(new Date('2023/08/11 08:40:50 am')));
183
- assert(isValidDate(new Date('08:40:50 am 2023/08/11')));
184
- assert(!isValidDate(new Date('')));
185
- assert(!isValidDate(new Date('08:40:50 am')));
186
- });
187
- });
188
-
189
16
  describe('Validation', () => {
190
17
  describe(`Empty rule`, () => {
191
18
  it(`Passes when the rule is empty`, async () => {
@@ -445,6 +272,16 @@ describe('Validation', () => {
445
272
  });
446
273
  });
447
274
 
275
+ describe(`Rule 'bail'`, () => {
276
+ it(`Passes when the field is null`, async () => {
277
+ const validator = new Validator({ field: '@bc' }, { field: 'bail|string|alpha|min:5' });
278
+
279
+ assert.deepEqual((await validator.validate()).messages(), {
280
+ field: ['validation.alpha'],
281
+ });
282
+ });
283
+ });
284
+
448
285
  describe(`Rule 'before'`, () => {
449
286
  const date = '2022-01-01';
450
287
  const rules1 = { field: `before:${date}` };
@@ -1259,56 +1096,65 @@ describe('Validation', () => {
1259
1096
 
1260
1097
  describe(`Rule 'hex_color'`, () => {
1261
1098
  it(`Passes when the field is a valid color`, async () => {
1262
- const validator = new Validator({
1263
- field_1: '#abc',
1264
- field_2: '#abcd',
1265
- field_3: '#abcabc',
1266
- field_4: '#abcdabcd',
1267
- }, {
1268
- field_1: 'hex_color',
1269
- field_2: 'hex_color',
1270
- field_3: 'hex_color',
1271
- field_4: 'hex_color',
1272
- });
1099
+ const validator = new Validator(
1100
+ {
1101
+ field_1: '#abc',
1102
+ field_2: '#abcd',
1103
+ field_3: '#abcabc',
1104
+ field_4: '#abcdabcd',
1105
+ },
1106
+ {
1107
+ field_1: 'hex_color',
1108
+ field_2: 'hex_color',
1109
+ field_3: 'hex_color',
1110
+ field_4: 'hex_color',
1111
+ },
1112
+ );
1273
1113
 
1274
1114
  assert(await validator.passes());
1275
1115
  });
1276
1116
 
1277
1117
  it(`Fails when the field is not a valid color`, async () => {
1278
- const validator = new Validator({
1279
- field_1: '#ghi',
1280
- field_2: '#ghij',
1281
- field_3: '#ghighi',
1282
- field_4: '#ghijghij',
1283
- field_5: 'abc',
1284
- field_6: 123,
1285
- field_7: [1, 2, 3],
1286
- }, {
1287
- field_1: 'hex_color',
1288
- field_2: 'hex_color',
1289
- field_3: 'hex_color',
1290
- field_4: 'hex_color',
1291
- field_5: 'hex_color',
1292
- field_6: 'hex_color',
1293
- field_7: 'hex_color',
1294
- });
1118
+ const validator = new Validator(
1119
+ {
1120
+ field_1: '#ghi',
1121
+ field_2: '#ghij',
1122
+ field_3: '#ghighi',
1123
+ field_4: '#ghijghij',
1124
+ field_5: 'abc',
1125
+ field_6: 123,
1126
+ field_7: [1, 2, 3],
1127
+ },
1128
+ {
1129
+ field_1: 'hex_color',
1130
+ field_2: 'hex_color',
1131
+ field_3: 'hex_color',
1132
+ field_4: 'hex_color',
1133
+ field_5: 'hex_color',
1134
+ field_6: 'hex_color',
1135
+ field_7: 'hex_color',
1136
+ },
1137
+ );
1295
1138
 
1296
1139
  assert(await validator.fails());
1297
1140
  });
1298
1141
 
1299
1142
  it(`Fails when the color's value is not 3, 4, 6 or 8 digits.`, async () => {
1300
- const validator = new Validator({
1301
- field_1: '#1',
1302
- field_2: '#12',
1303
- field_3: '#12345',
1304
- field_4: '#1234567',
1305
- field_4: '#123456789',
1306
- }, {
1307
- field_1: 'hex_color',
1308
- field_2: 'hex_color',
1309
- field_3: 'hex_color',
1310
- field_4: 'hex_color',
1311
- });
1143
+ const validator = new Validator(
1144
+ {
1145
+ field_1: '#1',
1146
+ field_2: '#12',
1147
+ field_3: '#12345',
1148
+ field_4: '#1234567',
1149
+ field_4: '#123456789',
1150
+ },
1151
+ {
1152
+ field_1: 'hex_color',
1153
+ field_2: 'hex_color',
1154
+ field_3: 'hex_color',
1155
+ field_4: 'hex_color',
1156
+ },
1157
+ );
1312
1158
 
1313
1159
  assert(await validator.fails());
1314
1160
  });
@@ -1496,7 +1342,7 @@ describe('Validation', () => {
1496
1342
  });
1497
1343
 
1498
1344
  it(`Fails when the field is not a list`, async () => {
1499
- const validator = new Validator({ field: { a: 1, b: 2, c: 3} }, rules);
1345
+ const validator = new Validator({ field: { a: 1, b: 2, c: 3 } }, rules);
1500
1346
  assert(await validator.fails());
1501
1347
 
1502
1348
  validator.setData({ field: 123 });