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