Skip to content

Commit 50cc5f7

Browse files
committed
first ideas
1 parent 5e9194d commit 50cc5f7

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

examples/validators/default.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Just a first basic test */
2+
var Modelate = require('../../index');
3+
// In production, just change the require for:
4+
// var Modelate = require('modelate');
5+
6+
var model = {
7+
name: {
8+
default: 'I am the default value'
9+
}
10+
};
11+
var user = Modelate('User').set(model);
12+
13+
var data = {};
14+
var result = user.modelate(data);
15+
16+
17+
console.log(result); // Shall be { name: 'I am the default value' }

lib/validate.js

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

22
// Add validator names here
3-
const validators = ['type', 'length', 'value', 'func', 'model', 'date'];
3+
const validators = ['type', 'length', 'value', 'func', 'model', 'date', 'value', 'default'];
44

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

lib/validators/default.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
/**
3+
* Default validator
4+
*
5+
* {
6+
* default: value // If no value set, write this.
7+
* }
8+
*/
9+
function isValid(data, model) {
10+
if (!model.default) {
11+
return true;
12+
}
13+
14+
if (!data) {
15+
data = model.default;
16+
}
17+
}
18+
19+
20+
module.exports = isValid;

spec/validators/value.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe(' - Date validator', () => {
248248
}
249249
};
250250

251-
expect(valid('string', model)).toEqual(false);
251+
expect(valid('whatever', model)).toEqual(false);
252252
});
253253

254254
// Again the for loop here! ^^

0 commit comments

Comments
 (0)