Skip to content

Commit

Permalink
fix: method call validator support by-name
Browse files Browse the repository at this point in the history
fixes #206
  • Loading branch information
BelfordZ committed Mar 18, 2020
1 parent a9f3392 commit 6b86f83
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
20 changes: 19 additions & 1 deletion src/method-call-validator/method-call-validator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MethodCallValidator from "./method-call-validator";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC, OpenrpcDocument } from "@open-rpc/meta-schema";
import MethodCallParameterValidationError from "./parameter-validation-error";
import MethodCallMethodNotFoundError from "./method-not-found-error";

Expand Down Expand Up @@ -72,4 +72,22 @@ describe("MethodCallValidator", () => {
const result = methodCallValidator.validate("boo", ["123"]);
expect(result).toBeInstanceOf(MethodCallMethodNotFoundError);
});

it("validates methods that use by-name", () => {
const example = {
info: { title: "123", version: "1" },
methods: [
{
name: "foo",
paramStructure: "by-name",
params: [{ name: "foofoo", schema: { type: "string" } }],
result: { name: "foofoo", schema: { type: "integer" } },
},
],
openrpc: "1.0.0-rc1",
} as OpenrpcDocument;
const methodCallValidator = new MethodCallValidator(example);
const result = methodCallValidator.validate("boo", { foofoo: "123" });
expect(result).toBeInstanceOf(MethodCallMethodNotFoundError);
});
});
4 changes: 2 additions & 2 deletions src/method-call-validator/method-call-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class MethodCallValidator {
*/
public validate(
methodName: string,
params: any[],
params: any,
): ParameterValidationError[] | MethodNotFoundError {
if (methodName === "rpc.discover") { return []; }
const method = _.find(this.document.methods, { name: methodName }) as MethodObject;
Expand All @@ -75,7 +75,7 @@ export default class MethodCallValidator {
}

return _.chain(method.params as ContentDescriptorObject[])
.map((param: ContentDescriptorObject, index: number): ParameterValidationError | undefined => {
.map((param: ContentDescriptorObject, index: number | string): ParameterValidationError | undefined => {
if (param.schema === undefined) { return; }
if (!params[index] && !param.required) { return; }

Expand Down
17 changes: 12 additions & 5 deletions src/method-call-validator/method-not-found-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class MethodNotFoundError implements Error {
constructor(
public methodName: string,
public openrpcDocument: OpenRPC,
public receievedParams: any[] = [],
public receievedParams: any[] | object = [],
) {
const msg = [
`Method Not Found Error for OpenRPC API named "${openrpcDocument.info.title}"`,
Expand All @@ -29,11 +29,18 @@ export default class MethodNotFoundError implements Error {
);
}

if (receievedParams.length > 0) {
const stringedParams = receievedParams
.map((p) => { try { return JSON.stringify(p); } catch (e) { return p; } })
.join("\n");
let stringedParams;
if (receievedParams instanceof Array) {
if (receievedParams.length > 0) {
stringedParams = receievedParams
.map((p) => { try { return JSON.stringify(p); } catch (e) { return p; } })
.join("\n");
}
} else {
stringedParams = JSON.stringify(receievedParams);
}

if (stringedParams) {
msg.push("Params:");
msg.push(stringedParams);
}
Expand Down
6 changes: 4 additions & 2 deletions src/method-call-validator/parameter-validation-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ export default class ParameterValidationError implements Error {
* @param errors The errors recieved by ajv
*/
constructor(
public paramIndex: number,
public paramIndex: number | string,
public expectedSchema: JSONSchema,
public receievedParam: any,
private errors: ErrorObject[],
) {
this.message = [
"Expected param in position ",
`Expected param at ${typeof paramIndex === "string" ? "key" : "position"}: `,
paramIndex,
" to match the json schema: ",
JSON.stringify(expectedSchema, undefined, " "),
". The function received instead ",
receievedParam,
".",
"The Validation errors: \n",
errors
].join("");
}
}

0 comments on commit 6b86f83

Please # to comment.