Skip to content

Commit

Permalink
Merge pull request #34 from open-rpc/feat/generate-method-id
Browse files Browse the repository at this point in the history
feat(ids): upgrade the content descriptor id generators
  • Loading branch information
BelfordZ authored Mar 22, 2019
2 parents 4f1c81e + 2d7a081 commit 416f68c
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 67 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"homepage": "https://github.com/open-rpc/schema-utils-js#readme",
"dependencies": {
"@open-rpc/meta-schema": "^1.0.6",
"@open-rpc/meta-schema": "^1.2.0",
"@types/ajv": "^1.0.0",
"ajv": "^6.10.0",
"fs-extra": "^7.0.1",
Expand Down
61 changes: 61 additions & 0 deletions src/generate-method-id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { generateMethodParamId, generateMethodResultId } from "./generate-method-id";
import { types } from "@open-rpc/meta-schema";

describe("methodParamId", () => {
it("returns an id for params", () => {
const method = {
name: "foo",
params: [{ name: "bar" }],
result: { name: "baz" },
};
const result = generateMethodParamId(method, method.params[0]);
expect(result).toBe("foo/0");
});

it("index by name when the method paramStructure is by-name", () => {
const method = {
name: "foo",
paramStructure: "by-name",
params: [{ name: "bar" }],
result: { name: "baz" },
} as types.MethodObject;

expect(generateMethodParamId(method, { name: "bar" })).toBe("foo/bar");
});

describe("throws when the content descriptor is not found in the params", () => {
it("by-position", () => {
const method = {
name: "foo",
params: [{ name: "u will never get dis" }],
result: { name: "baz" },
} as types.MethodObject;

expect(() => generateMethodParamId(method, { name: "123" }))
.toThrow("Content Descriptor not found in method.");
});

it("by-name", () => {
const method = {
name: "foo",
paramStructure: "by-name",
params: [{ name: "bar" }],
result: { name: "baz" },
} as types.MethodObject;

expect(() => generateMethodParamId(method, { name: "123" })).toThrow();
});
});
});

describe("methodResultId", () => {
it("returns an id for result", () => {
const method = {
name: "foo",
params: [{ name: "bar" }],
result: { name: "baz" },
};
const result = generateMethodResultId(method, method.result);
expect(result).toBe("foo/result");
});
});
28 changes: 14 additions & 14 deletions src/make-id-for-method-param.ts → src/generate-method-id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { types } from "@open-rpc/meta-schema";
import { some } from "lodash";

const makeNotFoundError = (method: types.MethodObject, contentDescriptor: types.ContentDescriptorObject) => {
const errorMessage = [
Expand All @@ -10,24 +11,23 @@ const makeNotFoundError = (method: types.MethodObject, contentDescriptor: types.
return new Error(errorMessage);
};

export const makeIdForMethodParam = (
export const generateMethodParamId = (
method: types.MethodObject,
contentDescriptor: types.ContentDescriptorObject,
) => {
if (method.params === undefined) { throw makeNotFoundError(method, contentDescriptor); }

let paramId;
if (method.paramStructure === "by-name") {
paramId = contentDescriptor.name;
} else {
const indexOfContentDescriptor = method.params.indexOf(contentDescriptor);

if (indexOfContentDescriptor !== -1) {
paramId = indexOfContentDescriptor;
} else {
throw makeNotFoundError(method, contentDescriptor);
}
if (!some(method.params, { name: contentDescriptor.name })) {
throw makeNotFoundError(method, contentDescriptor);
}

const isByName = method.paramStructure === "by-name";
const paramId = isByName ? contentDescriptor.name : method.params.indexOf(contentDescriptor);

return `${method.name}/${paramId}`;
};

export const generateMethodResultId = (
method: types.MethodObject,
contentDescriptor: types.ContentDescriptorObject,
) => {
return `${method.name}/result`;
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { parse } from "./parse";
export { makeIdForMethodParam } from "./make-id-for-method-param";
export { generateMethodParamId, generateMethodResultId } from "./generate-method-id";
export { getValidationErrors } from "./get-validation-errors";
export { MethodCallValidator, ParameterValidationError } from "./method-call-validator";
45 changes: 0 additions & 45 deletions src/make-id-for-method-param.test.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/method-call-validator/method-call-validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Ajv, { ErrorObject } from "ajv";
import * as _ from "lodash";
import { makeIdForMethodParam } from "../make-id-for-method-param";
import { generateMethodParamId } from "../generate-method-id";
import { types } from "@open-rpc/meta-schema";
import { ParameterValidationError } from "./parameter-validation-error";

Expand All @@ -17,7 +17,7 @@ export class MethodCallValidator {
params.forEach((param: types.ContentDescriptorObject, i: number) => {
if (param.schema === undefined) { return; }

this.ajvValidator.addSchema(param.schema, makeIdForMethodParam(method, param));
this.ajvValidator.addSchema(param.schema, generateMethodParamId(method, param));
});
});
}
Expand All @@ -33,7 +33,7 @@ export class MethodCallValidator {
.map((param: types.ContentDescriptorObject, index: number): ParameterValidationError | undefined => {
if (param.schema === undefined) { return; }

const idForMethod = makeIdForMethodParam(method, param);
const idForMethod = generateMethodParamId(method, param);
const isValid = this.ajvValidator.validate(idForMethod, params[index]);
const errors = this.ajvValidator.errors as ErrorObject[];

Expand Down

0 comments on commit 416f68c

Please sign in to comment.