Add SerializedObject.check_no_missing_fields

This commit is contained in:
Nicholas Dudfield
2014-01-05 11:36:35 +07:00
parent b5d29533ee
commit 967317f37e
2 changed files with 50 additions and 0 deletions

View File

@@ -72,11 +72,40 @@ SerializedObject.from_json = function (obj) {
' TransactionType, LedgerEntryType or AffectedNodes.');
}
// ND: This from_*json* seems a reasonable place to put validation of `json`
SerializedObject.check_no_missing_fields(typedef, obj);
so.serialize(typedef, obj);
return so;
};
SerializedObject.check_no_missing_fields = function (typedef, obj) {
var missing_fields = [];
for (var i = typedef.length - 1; i >= 0; i--) {
var spec = typedef[i];
var field = spec[0]
var requirement = spec[1];
if (binformat.REQUIRED === requirement && obj[field] == null) {
missing_fields.push(field);
};
};
if (missing_fields.length > 0) {
var object_name;
if (obj.TransactionType != null) {
object_name = SerializedObject.lookup_type_tx(obj.TransactionType);
} else {
object_name = "TransactionMetaData";
} /*else {
TODO: LedgerEntryType ...
}*/
throw new Error(object_name + " is missing fields: " +
JSON.stringify(missing_fields));
};
}
SerializedObject.prototype.append = function (bytes) {
if (bytes instanceof SerializedObject) {
bytes = bytes.buffer;