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

fix(hermes): array validation for array types #568

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
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
52 changes: 37 additions & 15 deletions libraries/hermes/src/Rest/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,49 @@ function validateArray(
param: Params[],
routeDefinedArray: Indexable[] | string[],
) {
const type = routeDefinedArray[0];
let type: { type: string; required: boolean } | string | Indexable;
if (isArray(routeDefinedArray)) {
type = routeDefinedArray[0];
} else {
type = {
type: (routeDefinedArray as { type: any[]; required: boolean }).type[0],
required: (routeDefinedArray as { type: any[]; required: boolean }).required,
};
}

if (isObject(type)) {
if (type.required && isNil(param)) {
throw ConduitError.userInput(`${fieldName} is required`);
}
} else {
if (!isArray(param)) {
if (param) {
param = [param];
}
// throw ConduitError.userInput(`${fieldName} must be an array`);
}
if (param === null) {
return null;
} else if (param === undefined) {
return;
}
if (!isArray(param)) {
if (param) {
param = [param];
}
param.forEach((obj: any, index: number) => {
param[index] = validateType(`${fieldName}[${index}]`, type, obj, false);
});
// throw ConduitError.userInput(`${fieldName} must be an array`);
}
if (param === null) {
return null;
} else if (param === undefined) {
return;
}
param.forEach((obj: any, index: number) => {
if (isObject(type) && isObject(type.type)) {
validateObject(index as unknown as string, param, type.type);
} else if (isObject(type) && !type.hasOwnProperty('type')) {
validateObject(index as unknown as string, param, type);
} else if (isObject(type) && type.hasOwnProperty('type')) {
param[index] = validateType(
`${fieldName}[${index}]`,
type.type as string,
obj,
false,
);
} else {
param[index] = validateType(`${fieldName}[${index}]`, type as string, obj, false);
}
});

return param;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class CustomEndpointHandler {
sort = constructSortObj(params.sort);
}

const createObj = this.parseCreateQuery(createString);
const createObj = JSON.parse(`{${createString}}`);
let promise;
if (endpoint.operation === OperationsEnum.GET) {
if (endpoint.paginated) {
Expand Down Expand Up @@ -185,19 +185,4 @@ export class CustomEndpointHandler {
throw new GrpcError(status.INTERNAL, e.message);
});
}

private parseCreateQuery(query: string) {
// add brackets to each field
const arr = query.split(',').map(val => `{${val}}`);
const res: Indexable = {};
for (const el of arr) {
const tmp = JSON.parse(el);
const key = Object.keys(tmp)[0];
if (!key) continue;
const innerKey = Object.keys(tmp[key])[0];
if (!res.hasOwnProperty(key)) res[key] = tmp[key];
else res[key][innerKey] = tmp[key][innerKey];
}
return res;
}
}