Skip to content

Commit ace076b

Browse files
committed
Merge branch 'develop' into v1-candidate
2 parents 5b27ff5 + 3a1033d commit ace076b

File tree

4 files changed

+105
-5
lines changed

4 files changed

+105
-5
lines changed

lib/modifiers/type.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Type modifier
3+
*
4+
* {
5+
* type: value // Parse to expected type if possible.
6+
* }
7+
*/
8+
9+
function modify(data, model) {
10+
if (!model.type || !data || model.typeStrict == true) {
11+
return data;
12+
}
13+
14+
let parsed = data;
15+
16+
switch (model.type.toLowerCase()) {
17+
case 'string':
18+
if (data) {
19+
parsed = String(data);
20+
}
21+
break;
22+
case 'number':
23+
if (!isNaN(Number(data))) {
24+
parsed = Number(data);
25+
}
26+
break;
27+
case 'boolean':
28+
parsed = String(data).toLowerCase() === 'true';
29+
break;
30+
case 'object':
31+
case 'array':
32+
if(typeof data === 'string') {
33+
try {
34+
data = JSON.parse(data)
35+
} catch (e) {
36+
console.error('Tried to parse invalid json string to ' + model.type.toLowerCase());
37+
}
38+
}
39+
break;
40+
}
41+
42+
return parsed;
43+
}
44+
45+
46+
module.exports = modify;

lib/modify.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ const modifiers = fs.readdirSync(directory).map(function(file) {
1717
* @param model {Object} Model to apply validations
1818
*/
1919
function modify(data, model) {
20+
let newValue = data;
2021
for (let i = 0; i < modifiers.length; i++) {
21-
const newValue = modifiers[i].check(data, model);
22-
return newValue ? newValue : data;
22+
newValue = modifiers[i].check(data, model);
2323
}
24-
25-
return true;
24+
25+
return newValue;
2626
}
2727

2828
module.exports = {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "modelate",
3-
"version": "0.2.5",
3+
"version": "0.2.9",
44
"description": "A simple data modeling tool for NodeJS",
55
"main": "index.js",
66
"scripts": {

spec/modifiers/type.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var valid = require('../../lib/modifiers/type');
2+
3+
const str = 'Hello world';
4+
const obj = {hello: 'world'};
5+
const num = 42;
6+
const bool = true;
7+
const arr = [1, 2, 3];
8+
9+
const types = {
10+
string: str,
11+
object: obj,
12+
number: num,
13+
boolean: bool,
14+
array: arr
15+
};
16+
const typesKeys = Object.keys(types);
17+
18+
describe(' - Type modifier', () => {
19+
it('shall be a function', () => {
20+
expect(typeof valid).toEqual('function');
21+
});
22+
23+
for (let key in typesKeys) {
24+
it('shall let '+ typesKeys[key] +' when no model set', () => {
25+
expect(valid(types[key], {})).toEqual(types[key]);
26+
});
27+
}
28+
29+
for (let validType in typesKeys) {
30+
let model = {type: typesKeys[validType]};
31+
for (let check in typesKeys) {
32+
it('shall only validate '+ typesKeys[validType] +' when model set, and '+ typesKeys[check] +' given', () => {
33+
const modelated = valid(types[typesKeys[check]], model);
34+
let shallBeValid = (typeof modelated === typesKeys[validType]);
35+
36+
if (typesKeys[validType] === 'object' && typeof modelated === 'undefined') {
37+
shallBeValid = true;
38+
}
39+
40+
if ((typesKeys[validType] === 'array' && Array.isArray(modelated))
41+
|| (typesKeys[validType] === 'array' && typeof modelated === 'undefined')) {
42+
shallBeValid = true;
43+
}
44+
45+
if (typesKeys[validType] === 'number' && typeof modelated === 'undefined') {
46+
shallBeValid = true;
47+
}
48+
49+
expect(shallBeValid).toEqual(true);
50+
});
51+
}
52+
}
53+
54+
});

0 commit comments

Comments
 (0)