Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RpcStruct._validateType to not throw error for a null value #393

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions lib/js/src/rpc/RpcStruct.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,21 @@ class RpcStruct {
* @param {Boolean} isArray - Whether or not the given variable is an array to be iterated through.
*/
_validateType (tClass, obj, isArray = false) {
if (isArray) {
if (!Array.isArray(obj)) {
throw new Error(`${obj.name} must be an array containing items of type ${tClass.name}`);
} else {
for (const item of obj) {
this._validateType(tClass, item, false);
if (obj !== null) {
if (isArray) {
if (!Array.isArray(obj)) {
throw new Error(`${obj.name} must be an array containing items of type ${tClass.name}`);
} else {
for (const item of obj) {
this._validateType(tClass, item, false);
}
}
} else if (
(tClass.prototype instanceof Enum && tClass.keyForValue(obj) === null)
|| (tClass.prototype instanceof RpcStruct && obj.constructor !== tClass)
) {
throw new Error(`${obj.name} must be of type ${tClass.name}`);
}
} else if (
(tClass.prototype instanceof Enum && tClass.keyForValue(obj) === null)
|| (tClass.prototype instanceof RpcStruct && obj !== null && obj.constructor !== tClass)
) {
throw new Error(`${obj.name} must be of type ${tClass.name}`);
}
}
}
Expand Down