Skip to content

Commit 8392b84

Browse files
committed
type number checks numerical value
1 parent a2c5df0 commit 8392b84

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

moose.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ class Model {
2525
// * =================
2626

2727
// ! TESTING PURPOSES ONLY FOR NOW
28-
test = cb => {
28+
test = (data, cb) => {
2929
if (this.validate) {
30-
let valid = validate.valid(this.schema, {
31-
username: 'dog',
32-
number: 'cow'
33-
});
30+
let valid = validate.valid(this.schema, data);
3431
console.log(valid);
3532
cb(valid);
3633
} else {

validate.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ function htmlEntities(str) {
88
.replace(/"/g, '"');
99
}
1010

11-
const stringed = data => {
11+
function stringed(data) {
1212
for (var index in data) {
1313
data[index] = htmlEntities(data[index]);
1414
}
15-
};
15+
return data;
16+
}
1617

1718
const valid = (schema, data) => {
18-
//stringed(data);
1919
let ret = {};
20+
ret['body'] = {};
2021
ret['errors'] = { state: false };
2122
let errors = [];
2223
matchHash = false;
@@ -183,17 +184,28 @@ const valid = (schema, data) => {
183184
flag = true;
184185
}
185186
}
187+
if (k == 'type') {
188+
if (schema[key][k] == 'number') {
189+
if (typeof data[key] != 'number') {
190+
errors.push(
191+
key + ' needs to be a numerical value'
192+
);
193+
flag = true;
194+
}
195+
}
196+
}
186197
}
187198
}
188199
if (!flag) {
189-
ret[key] = data[key];
200+
ret['body'][key] = data[key];
190201
}
191202
}
192203
}
193204
if (errors.length > 0) {
194205
ret['errors'] = { state: true };
195206
ret['errors']['messages'] = errors;
196207
}
208+
ret['body'] = stringed(ret['body']);
197209
return ret;
198210
};
199211

0 commit comments

Comments
 (0)