Skip to content

Commit 7e68fd9

Browse files
committed
Lenght validation
Also some ideas added to original validator
1 parent 28aa820 commit 7e68fd9

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

lib/validators/length.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function isValid(data, model) {
1515
return true;
1616
}
1717

18+
// ToDo: Number length (toString?)
19+
// ToDo: Object length (keys?)
20+
1821
// Data hasn't the exact eq lenght
1922
if (model.length.eq && data.length !== model.length.eq) {
2023
return false;

spec/validators/length.spec.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
var valid = require('../../lib/validators/length');
2+
3+
const str = 'Hello world';
4+
const strLen = str.length;
5+
const arr = ['Hello world', 'other thing'];
6+
const arrLen = arr.length;
7+
8+
const model = {
9+
length: {}
10+
};
11+
12+
13+
describe(' - Length validator', () => {
14+
it('shall be a function', () => {
15+
expect(typeof valid).toEqual('function');
16+
});
17+
18+
it('shall validate everything when empty model set', () => {
19+
expect(valid(str, model)).toEqual(true);
20+
expect(valid(arr, model)).toEqual(true);
21+
});
22+
23+
describe('equal filter', () => {
24+
describe('strings', () => {
25+
it('shall validate string with string.length', () => {
26+
const eqModel = { length: { eq: strLen } };
27+
expect(valid(str, eqModel)).toEqual(true);
28+
});
29+
30+
it('shall NOT validate string with string.length - 1', () => {
31+
const eqModel = { length: { eq: (strLen - 1) } };
32+
expect(valid(str, eqModel)).toEqual(false);
33+
});
34+
35+
it('shall NOT validate string with string.length + 1', () => {
36+
const eqModel = { length: { eq: (strLen + 1) } };
37+
expect(valid(str, eqModel)).toEqual(false);
38+
});
39+
});
40+
41+
describe('arrays', () => {
42+
it('shall validate array with array.length', () => {
43+
const eqModel = { length: { eq: arrLen } };
44+
expect(valid(arr, eqModel)).toEqual(true);
45+
});
46+
47+
it('shall NOT validate array with array.length - 1', () => {
48+
const eqModel = { length: { eq: (arrLen - 1) } };
49+
expect(valid(arr, eqModel)).toEqual(false);
50+
});
51+
52+
it('shall NOT validate array with array.length + 1', () => {
53+
const eqModel = { length: { eq: (arrLen + 1) } };
54+
expect(valid(arr, eqModel)).toEqual(false);
55+
});
56+
});
57+
});
58+
59+
describe('max filter', () => {
60+
describe('strings', () => {
61+
it('shall validate string with max string.length', () => {
62+
const eqModel = { length: { max: strLen } };
63+
expect(valid(str, eqModel)).toEqual(true);
64+
});
65+
66+
it('shall NOT validate string with max string.length - 1', () => {
67+
const eqModel = { length: { max: (strLen - 1) } };
68+
expect(valid(str, eqModel)).toEqual(false);
69+
});
70+
71+
it('shall validate string with max string.length + 1', () => {
72+
const eqModel = { length: { max: (strLen + 1) } };
73+
expect(valid(str, eqModel)).toEqual(true);
74+
});
75+
});
76+
77+
describe('arrays', () => {
78+
it('shall validate array with max array.length', () => {
79+
const eqModel = { length: { max: arrLen } };
80+
expect(valid(arr, eqModel)).toEqual(true);
81+
});
82+
83+
it('shall NOT validate array with max array.length - 1', () => {
84+
const eqModel = { length: { max: (arrLen - 1) } };
85+
expect(valid(arr, eqModel)).toEqual(false);
86+
});
87+
88+
it('shall validate array with max array.length + 1', () => {
89+
const eqModel = { length: { max: (arrLen + 1) } };
90+
expect(valid(arr, eqModel)).toEqual(true);
91+
});
92+
});
93+
});
94+
95+
describe('min filter', () => {
96+
describe('strings', () => {
97+
it('shall validate string with min string.length', () => {
98+
const eqModel = { length: { min: strLen } };
99+
expect(valid(str, eqModel)).toEqual(true);
100+
});
101+
102+
it('shall validate string with min string.length - 1', () => {
103+
const eqModel = { length: { min: (strLen - 1) } };
104+
expect(valid(str, eqModel)).toEqual(true);
105+
});
106+
107+
it('shall NOT validate string with min string.length + 1', () => {
108+
const eqModel = { length: { min: (strLen + 1) } };
109+
expect(valid(str, eqModel)).toEqual(false);
110+
});
111+
});
112+
113+
describe('arrays', () => {
114+
it('shall validate array with min array.length', () => {
115+
const eqModel = { length: { min: arrLen } };
116+
expect(valid(arr, eqModel)).toEqual(true);
117+
});
118+
119+
it('shall validate array with min array.length - 1', () => {
120+
const eqModel = { length: { min: (arrLen - 1) } };
121+
expect(valid(arr, eqModel)).toEqual(true);
122+
});
123+
124+
it('shall NOT validate array with min array.length + 1', () => {
125+
const eqModel = { length: { min: (arrLen + 1) } };
126+
expect(valid(arr, eqModel)).toEqual(false);
127+
});
128+
});
129+
});
130+
131+
});

spec/validators/type.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ describe(' - Type validator', () => {
1717
expect(typeof valid).toEqual('function');
1818
});
1919

20-
2120
for (let key in typesKeys) {
2221
it('shall validate '+ typesKeys[key] +' when no model set', () => {
2322
expect(valid(types[key], {})).toEqual(true);

0 commit comments

Comments
 (0)