Skip to content

Commit 39763f4

Browse files
committed
Add array to type validator (using ES6 isArray)
1 parent cdf72f2 commit 39763f4

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/validators/type.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ function isValid(data, model) {
1111
return true;
1212
}
1313

14-
if ((typeof (data)).toUpperCase() === model.type.toUpperCase()) {
14+
if ((typeof data).toUpperCase() === model.type.toUpperCase()) {
1515
return true;
16+
} else if (model.type.toUpperCase() === 'ARRAY') {
17+
return Array.isArray(data);
1618
}
1719

1820
return false;

spec/validators/type.spec.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const str = 'Hello world';
44
const obj = {hello: 'world'};
55
const num = 42;
66
const bool = true;
7+
const arr = [1, 2, 3];
78

89
const types = {
910
string: str,
1011
object: obj,
1112
number: num,
12-
boolean: bool
13+
boolean: bool,
14+
array: arr
1315
};
1416
const typesKeys = Object.keys(types);
1517

@@ -28,7 +30,14 @@ describe(' - Type validator', () => {
2830
let model = {type: typesKeys[validType]};
2931
for (let check in typesKeys) {
3032
it('shall only validate '+ typesKeys[validType] +' when model set, and '+ typesKeys[check] +' given', () => {
31-
expect(valid(types[typesKeys[check]], model)).toEqual( (validType === check) );
33+
let shallBeValid = (validType === check);
34+
35+
// Since arrays are also valid object types, shall do some special check
36+
if (typesKeys[validType] === 'object' && typesKeys[check] === 'array') {
37+
shallBeValid = true;
38+
}
39+
40+
expect(valid(types[typesKeys[check]], model)).toEqual(shallBeValid);
3241
});
3342
}
3443
}

0 commit comments

Comments
 (0)