Open BackdoorTech opened 4 years ago
Looping through each element is not that hard.
i will let you know when i finish
Not the best way but works just fine;
const Validator = require('Validator');
var data = {
name: 'John Doe',
test:{
birthday: '20000000000000'
},
company: 'Example Co.',
};
var rules = {
name: 'required',
test:{
birthday: 'required'
},
company: ['required', 'string']
};
function convertObj2SpaceName(MObj) {
const $type = (e) => (e.constructor)
var nameSpace = [];
var rlt = {};
const loop = (obj) => {
// get object indexs
const entries = Object.entries(obj);
for (const [i, val] of entries) {
nameSpace.push(i);
// if object
if ($type(val) === Object) {
loop(val);
} else { // if not object
rlt[nameSpace.join('/')] = val
}
nameSpace.pop();
}
return rlt;
}
return loop(MObj);
}
var data = convertObj2SpaceName(data);
var rules = convertObj2SpaceName(rules);
const v = Validator.make(data, rules);
if (v.fails()) {
const errors = v.getErrors();
console.log(errors);
} else {
console.log('good!')
}
Hello 👋
After checking the issue it seems that currently this package does not support validation of nested objects.
Two different approachs can be pursued, we either discard nested objects from validation or we support nested objects validation. @PeterDias55 if you can solve the issue in either way feel free to open a PR.
It will be some time until I can fix it.
Thanks, João Faustino
Ok, then i will open a PR as soon as I finish the implementation
Thanks, Peter Dias Dos Santos
Any news on this @PeterDias55 ?
No i don't. i won't be able to finish it my priority has changed.
my apology @jfstn
this is my data structure