Skip to content

Commit a21ae07

Browse files
committed
Separate validators
1 parent fd23f7a commit a21ae07

File tree

5 files changed

+80
-18
lines changed

5 files changed

+80
-18
lines changed

lib/modelate.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const models = require('./model').models;
44
const validate = require('./validate');
55

66
function modelate(data) {
7-
console.log('modeling', data, 'for object with type', this.modelName);
8-
97
const model = util.clone(models[this.name]);
108

119
for(let prop in model) {

lib/validate.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11

2+
const validators = ['type', 'length'];
23

3-
function validate(data, model) {
4-
let isValid = true;
4+
// Turn validator names to validator instances.
5+
for(let i = 0; i < validators.length; i++) {
6+
validators[i] = validator(validators[i]);
7+
}
58

6-
if(model.type && typeof(data) === model.type) {
7-
isValid = true;
8-
9-
// maxLength
10-
if(model.maxLength && data.length > model.maxLength) {
11-
isValid = false;
12-
}
13-
// minLenght
14-
if(model.minLength && data.length < model.minLength) {
15-
isValid = false;
9+
// Validate
10+
function validate(data, model) {
11+
for(let i = 0; i < validators.length; i++) {
12+
if(!validators[i].check(data, model)) {
13+
console.error('Validation for "' + data + '" failed! Reason: ', validators[i].name);
14+
return false;
1615
}
17-
} else {
18-
isValid = false;
19-
}
16+
}
2017

21-
return isValid;
18+
return true;
2219
}
2320

21+
// Create new validator instances
22+
function validator(name) {
23+
return {
24+
name: name,
25+
check: require('./validators/' + name)
26+
};
27+
}
28+
29+
2430
module.exports = validate;

lib/validators/length.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Length validator
3+
*
4+
* {
5+
* length: {
6+
* max: Number, // Maximum allowed value
7+
* min: Number // Minimum allowed value
8+
* }
9+
* }
10+
*/
11+
12+
function isValid(data, model) {
13+
if(!model.length) {
14+
return true;
15+
}
16+
17+
// Data is higher than max
18+
if(model.length.max && data.length > model.length.max) {
19+
return false;
20+
}
21+
22+
// Data is lower than min
23+
if(model.length.min && data.length < model.length.min) {
24+
return false;
25+
}
26+
27+
return true;
28+
}
29+
30+
module.exports = isValid;

lib/validators/type.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Typ(e validator
3+
*
4+
* {
5+
* type: String // Check if data has a JS type (uses typeof)
6+
* }
7+
*/
8+
9+
function isValid(data, model) {
10+
if(!model.type) {
11+
return false;
12+
}
13+
14+
if((typeof(data)).toUpperCase() === model.type.toUpperCase()) {
15+
return true;
16+
}
17+
18+
return false;
19+
}
20+
21+
module.exports = isValid;

test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ var model = {
88
max: 10,
99
min: 1
1010
}
11+
},
12+
surname: {
13+
type: 'string',
14+
length: { // For now, length do nothng.
15+
max: 3,
16+
min: 1
17+
}
1118
}
1219
};
1320
var user = Modelate('User').model(model);

0 commit comments

Comments
 (0)