Skip to content

Commit be99520

Browse files
committed
add optional objects support brutusin#53
1 parent 1cc86c6 commit be99520

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/js/brutusin-json-forms.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -800,39 +800,44 @@ if (typeof brutusin === "undefined") {
800800
};
801801

802802
obj.getData = function () {
803-
function removeEmptiesAndNulls(object) {
803+
function removeEmptiesAndNulls(object, schema) {
804804
if (object instanceof Array) {
805805
if (object.length === 0) {
806806
return null;
807807
}
808808
var clone = new Array();
809809
for (var i = 0; i < object.length; i++) {
810-
clone[i] = removeEmptiesAndNulls(object[i]);
810+
clone[i] = removeEmptiesAndNulls(object[i], schema.items);
811811
}
812812
return clone;
813813
} else if (object === "") {
814814
return null;
815815
} else if (object instanceof Object) {
816816
var clone = new Object();
817+
var nonEmpty = false;
817818
for (var prop in object) {
818819
if (prop.startsWith("$") && prop.endsWith("$")) {
819820
continue;
820821
}
821-
var value = removeEmptiesAndNulls(object[prop]);
822+
var value = removeEmptiesAndNulls(object[prop], schema.properties[prop]);
822823
if (value !== null) {
823824
clone[prop] = value;
825+
nonEmpty = true;
824826
}
825827
}
826-
return clone;
828+
if (nonEmpty || schema.required) {
829+
return clone;
830+
} else {
831+
return null;
832+
}
827833
} else {
828834
return object;
829835
}
830836
}
831837
if (!container) {
832838
return null;
833839
} else {
834-
return removeEmptiesAndNulls(data);
835-
;
840+
return removeEmptiesAndNulls(data, schema);
836841
}
837842
};
838843

0 commit comments

Comments
 (0)