From 527c506579dbda3c9a3c3ed254883c78e6eaa880 Mon Sep 17 00:00:00 2001 From: robinknaapen Date: Thu, 3 May 2018 12:33:36 +0200 Subject: [PATCH] Add support for to flatten array --- serializer.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/serializer.js b/serializer.js index 0b644fc..e939f9a 100644 --- a/serializer.js +++ b/serializer.js @@ -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; }