Add support for to flatten array

This commit is contained in:
Crow Crowcrow 2018-05-03 12:33:36 +02:00
parent 740f31092c
commit 527c506579
Signed by: Crow
GPG Key ID: 45A8E203AF859FD8
1 changed files with 21 additions and 7 deletions

View File

@ -22,6 +22,7 @@ class SerializerField {
this.field = f; this.field = f;
this.required = f.required; this.required = f.required;
this.type = this.field.getAttribute("type"); this.type = this.field.getAttribute("type");
this.flatten = this.field.hasAttribute("flatten");
} }
/** /**
@ -168,13 +169,26 @@ class Serializer {
for (let key in f) { for (let key in f) {
if (f[key]) { if (f[key]) {
let d = f[key].serialize(); let d = f[key].serialize();
if (d !== null) { if (d == null) {
if (k == "") { continue
json[key] = d;
continue;
}
json[k][key] = d;
} }
if(k == "") {
if(f[key].flatten){
json[key].push(d);
} else {
json[key] = d;
}
continue
}
if(f[key].flatten){
json[k].push(d);
continue;
}
json[k][key] = d;
} }
} }
} else { } else {
@ -246,7 +260,7 @@ class Validator {
let v = Validator.validateRequired(sf); let v = Validator.validateRequired(sf);
if (!v.valid) { if (!v.valid) {
console.log(e.errors); console.error(e.errors);
e.errors[sf.name].push(v.errors); e.errors[sf.name].push(v.errors);
continue; continue;
} }