Skip to content

Commit fa46bc5

Browse files
authored
Merge pull request #18 from CodingCarlos/modifiers
First idea on modifiers
2 parents ec5df24 + 435704e commit fa46bc5

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
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/modelate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const util = require('./util');
22

33
const models = require('./model').models;
44
const validate = require('./validate').check;
5+
const modify = require('./modify').check;
56

67
/**
78
* Create a new modelate, validating all properties
@@ -19,6 +20,10 @@ function modelate(data, opts) {
1920
const result = util.clone(model) || {};
2021

2122
for (const prop in model) {
23+
// Step 1: Apply the modifiers
24+
data[prop] = modify(data[prop], model[prop]);
25+
26+
// Step 2: validate the result
2227
if (validate(data[prop], model[prop])) {
2328
result[prop] = data[prop];
2429
} else {

lib/modifiers/default.js

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

lib/modify.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
// Add modifier names here
3+
const modifiers = ['default'];
4+
5+
// Turn modifier names to modifier instances.
6+
for (let i = 0; i < modifiers.length; i++) {
7+
modifiers[i] = modifier(modifiers[i]);
8+
}
9+
10+
/**
11+
* Validate
12+
*
13+
* @param data {Object} Data to validate
14+
* @param model {Object} Model to apply validations
15+
*/
16+
function modify(data, model) {
17+
for (let i = 0; i < modifiers.length; i++) {
18+
const newValue = modifiers[i].check(data, model);
19+
return newValue ? newValue : data;
20+
}
21+
22+
return true;
23+
}
24+
25+
/**
26+
* Create new modifier instances
27+
*/
28+
function modifier(name) {
29+
return {
30+
name: name,
31+
check: require('./modifiers/' + name),
32+
};
33+
}
34+
35+
module.exports = {
36+
modifiers: modifiers,
37+
check: modify
38+
};

spec/modifiers/default.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var valid = require('../../lib/modifiers/default');
2+
3+
const model = {
4+
default: 'default value'
5+
};
6+
7+
describe(' - Default modifier', () => {
8+
it('shall be a function', () => {
9+
expect(typeof valid).toEqual('function');
10+
});
11+
12+
it('shall not modify data when no model set', () => {
13+
expect(valid('validData', {})).toEqual(null);
14+
expect(valid(undefined, {})).toEqual(null);
15+
});
16+
17+
it('shall not modify data if exists and model set', () => {
18+
expect(valid('validData', model)).toEqual(null);
19+
});
20+
21+
it('shall return the default value when not data and model set', () => {
22+
expect(valid(undefined, model)).toEqual(model.default);
23+
});
24+
});

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)