Skip to content

Commit ec4bf9a

Browse files
authored
Merge pull request #393 from smartdevicelink/bugfix/validate_type_null
Update RpcStruct._validateType to not throw error for a null value
2 parents 30dd1f2 + ceed53a commit ec4bf9a

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

lib/js/src/rpc/RpcStruct.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,21 @@ class RpcStruct {
130130
* @param {Boolean} isArray - Whether or not the given variable is an array to be iterated through.
131131
*/
132132
_validateType (tClass, obj, isArray = false) {
133-
if (isArray) {
134-
if (!Array.isArray(obj)) {
135-
throw new Error(`${obj.name} must be an array containing items of type ${tClass.name}`);
136-
} else {
137-
for (const item of obj) {
138-
this._validateType(tClass, item, false);
133+
if (obj !== null) {
134+
if (isArray) {
135+
if (!Array.isArray(obj)) {
136+
throw new Error(`${obj.name} must be an array containing items of type ${tClass.name}`);
137+
} else {
138+
for (const item of obj) {
139+
this._validateType(tClass, item, false);
140+
}
139141
}
142+
} else if (
143+
(tClass.prototype instanceof Enum && tClass.keyForValue(obj) === null)
144+
|| (tClass.prototype instanceof RpcStruct && obj.constructor !== tClass)
145+
) {
146+
throw new Error(`${obj.name} must be of type ${tClass.name}`);
140147
}
141-
} else if (
142-
(tClass.prototype instanceof Enum && tClass.keyForValue(obj) === null)
143-
|| (tClass.prototype instanceof RpcStruct && obj !== null && obj.constructor !== tClass)
144-
) {
145-
throw new Error(`${obj.name} must be of type ${tClass.name}`);
146148
}
147149
}
148150
}

0 commit comments

Comments
 (0)