Skip to content

Commit 2f7515e

Browse files
committed
Add value and custom function validators
1 parent a21ae07 commit 2f7515e

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

lib/validate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

2-
const validators = ['type', 'length'];
2+
// Add validator names here
3+
const validators = ['type', 'length', 'value', 'func'];
34

45
// Turn validator names to validator instances.
56
for(let i = 0; i < validators.length; i++) {

lib/validators/func.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Custom function validator
3+
*
4+
* {
5+
* func: function A function to validate. Must return true or false.
6+
* }
7+
*/
8+
9+
function isValid(data, model) {
10+
if(!model.func) {
11+
return true;
12+
}
13+
14+
return model.func(data);
15+
}
16+
17+
module.exports = isValid;

lib/validators/value.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Value validator
3+
*
4+
* {
5+
* value: {
6+
* eq: String || Number, // Exact allowed value (equals)
7+
* max: Number, // Maximum allowed value
8+
* min: Number, // Minimum allowed value
9+
* contains: String || Number // The value contains
10+
* }
11+
* }
12+
*/
13+
14+
function isValid(data, model) {
15+
if(!model.value) {
16+
return true;
17+
}
18+
19+
// Data is different than equals
20+
if(model.value.eq) {
21+
if(typeof(data) === typeof(model.value.eq) && data !== model.value.eq) {
22+
// Same data type and different values
23+
return false;
24+
} else {
25+
// Different types: Try to convert one to other.
26+
var parsed = data;
27+
var type = typeof(model.value.eq);
28+
switch(type.toLowerCase()) {
29+
case 'string':
30+
parsed = String(data);
31+
break;
32+
case 'number':
33+
parsed = Number(data);
34+
break;
35+
case 'boolean':
36+
parsed = Boolean(data);
37+
break;
38+
}
39+
40+
if(data !== model.value.eq) {
41+
return false;
42+
}
43+
}
44+
}
45+
46+
// Data is higher than max
47+
if(model.value.max && Number(data) > Number(model.value.max)) {
48+
return false;
49+
}
50+
51+
// Data is lower than min
52+
if(model.value.min && Number(data) < Number(model.value.min)) {
53+
return false;
54+
}
55+
56+
// Data does not contain something
57+
if(model.value.contains && data.indexOf(model.value.contains) === -1) {
58+
return false;
59+
}
60+
61+
return true;
62+
}
63+
64+
module.exports = isValid;

test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,50 @@ var model = {
77
length: { // For now, length do nothng.
88
max: 10,
99
min: 1
10+
},
11+
value: {
12+
eq: 'Paco'
1013
}
1114
},
1215
surname: {
1316
type: 'string',
1417
length: { // For now, length do nothng.
1518
max: 3,
1619
min: 1
20+
},
21+
value: {
22+
contains: 'os'
23+
}
24+
},
25+
age: {
26+
type: 'number',
27+
value: {
28+
max: 95,
29+
min: 18
30+
}
31+
},
32+
dni: {
33+
type: 'string',
34+
value: {
35+
max: 95,
36+
min: 18
37+
},
38+
func: function startOnFive(value) {
39+
if(value.charAt(0) === '5') {
40+
return true;
41+
}
42+
43+
return false;
1744
}
1845
}
1946
};
2047
var user = Modelate('User').model(model);
2148

2249
var data = {
2350
name: 'Paco',
24-
surname: 'santos' // Surname shall be removed
51+
surname: 'santos',
52+
age: 19,
53+
dni: '41402430A'
2554
};
2655
var result = user.modelate(data);
2756

0 commit comments

Comments
 (0)