From fa04da717d8b0cb9dffadbcecad9dd5f6cc494bf Mon Sep 17 00:00:00 2001 From: Zachary Belford Date: Thu, 21 Mar 2019 17:12:41 -0700 Subject: [PATCH 1/2] feat(ids): upgrade the content descriptor id generators fixes #35 --- package.json | 2 +- src/generate-method-id.test.ts | 61 +++++++++++++++++++ ...-method-param.ts => generate-method-id.ts} | 27 ++++---- src/index.ts | 2 +- src/make-id-for-method-param.test.ts | 45 -------------- .../method-call-validator.ts | 6 +- 6 files changed, 81 insertions(+), 62 deletions(-) create mode 100644 src/generate-method-id.test.ts rename src/{make-id-for-method-param.ts => generate-method-id.ts} (51%) delete mode 100644 src/make-id-for-method-param.test.ts diff --git a/package.json b/package.json index 1f6ce09c..d133527f 100644 --- a/package.json +++ b/package.json @@ -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.1.0", "@types/ajv": "^1.0.0", "ajv": "^6.10.0", "fs-extra": "^7.0.1", diff --git a/src/generate-method-id.test.ts b/src/generate-method-id.test.ts new file mode 100644 index 00000000..4ab255cc --- /dev/null +++ b/src/generate-method-id.test.ts @@ -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"); + }); +}); diff --git a/src/make-id-for-method-param.ts b/src/generate-method-id.ts similarity index 51% rename from src/make-id-for-method-param.ts rename to src/generate-method-id.ts index a392f7cc..c50a61b4 100644 --- a/src/make-id-for-method-param.ts +++ b/src/generate-method-id.ts @@ -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 = [ @@ -10,24 +11,26 @@ const makeNotFoundError = (method: types.MethodObject, contentDescriptor: types. return new Error(errorMessage); }; -export const makeIdForMethodParam = ( +export const generateMethodParamId = ( method: types.MethodObject, contentDescriptor: types.ContentDescriptorObject, ) => { + // remove this once updated meta-schema with this PR: https://github.com/open-rpc/meta-schema/pull/72 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`; +}; diff --git a/src/index.ts b/src/index.ts index f8c75b97..164376f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/make-id-for-method-param.test.ts b/src/make-id-for-method-param.test.ts deleted file mode 100644 index 99260705..00000000 --- a/src/make-id-for-method-param.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { makeIdForMethodParam } from "./make-id-for-method-param"; -import { types } from "@open-rpc/meta-schema"; - -describe("makeIdForMethodParam", () => { - it("returns an id", () => { - const method = { - name: "foo", - params: [{ name: "bar" }], - result: { name: "baz" }, - }; - const result = makeIdForMethodParam(method, method.params[0]); - expect(result).toBe("foo/0"); - }); - - it("throws if there are no params on the method", () => { - const method = { - name: "foo", - result: { name: "baz" }, - }; - expect(() => makeIdForMethodParam(method, { name: "123" })) - .toThrow("Content Descriptor not found in method."); - }); - - it("works when the method paramStructure is by-name", () => { - const method = { - name: "foo", - paramStructure: "by-name", - params: [{ name: "bar" }], - result: { name: "baz" }, - } as types.MethodObject; - - expect(makeIdForMethodParam(method, { name: "123" })).toBe("foo/123"); - }); - - it("throws when the content descriptor is not found in the params", () => { - const method = { - name: "foo", - params: [{ name: "u will never get dis" }], - result: { name: "baz" }, - } as types.MethodObject; - - expect(() => makeIdForMethodParam(method, { name: "123" })) - .toThrow("Content Descriptor not found in method."); - }); -}); diff --git a/src/method-call-validator/method-call-validator.ts b/src/method-call-validator/method-call-validator.ts index 2b807281..56375b7c 100644 --- a/src/method-call-validator/method-call-validator.ts +++ b/src/method-call-validator/method-call-validator.ts @@ -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"; @@ -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)); }); }); } @@ -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[]; From 2d7a081075c60caede2726b323271cd96d718107 Mon Sep 17 00:00:00 2001 From: Zachary Belford Date: Thu, 21 Mar 2019 17:18:32 -0700 Subject: [PATCH 2/2] fix: remove unneeded check --- package-lock.json | 6 +++--- package.json | 2 +- src/generate-method-id.ts | 3 --- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9f6c689e..3ff739fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -378,9 +378,9 @@ "integrity": "sha512-R9WXQfSfFBPWqY5TLclnlMw3W8hi0m6iiWzkFfTORFM9GU9d+svqCkgWDfIr9rNqViJk0C4CWkdVP1gpwlcjXQ==" }, "@open-rpc/meta-schema": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@open-rpc/meta-schema/-/meta-schema-1.1.0.tgz", - "integrity": "sha512-8E4jHAxirytTkwtCx/aaEGhEqdGzILLSA+x0OXt/IdirAXWmImaIWlmXShIGq96cSRoPf9kaOV2r8znGY6VI7A==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@open-rpc/meta-schema/-/meta-schema-1.2.0.tgz", + "integrity": "sha512-ZHPaxueUpKj0zCf4dNF0UJ6JxMQawBElkjI+FGND+m1NoABJamwipR139XYp+g84UNk6cMADXcsx30YXoXUSuw==", "requires": { "@open-rpc/examples": "^1.0.13", "ajv": "^6.10.0", diff --git a/package.json b/package.json index d133527f..11cddb3f 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ }, "homepage": "https://github.com/open-rpc/schema-utils-js#readme", "dependencies": { - "@open-rpc/meta-schema": "^1.1.0", + "@open-rpc/meta-schema": "^1.2.0", "@types/ajv": "^1.0.0", "ajv": "^6.10.0", "fs-extra": "^7.0.1", diff --git a/src/generate-method-id.ts b/src/generate-method-id.ts index c50a61b4..8b06a998 100644 --- a/src/generate-method-id.ts +++ b/src/generate-method-id.ts @@ -15,9 +15,6 @@ export const generateMethodParamId = ( method: types.MethodObject, contentDescriptor: types.ContentDescriptorObject, ) => { - // remove this once updated meta-schema with this PR: https://github.com/open-rpc/meta-schema/pull/72 - if (method.params === undefined) { throw makeNotFoundError(method, contentDescriptor); } - if (!some(method.params, { name: contentDescriptor.name })) { throw makeNotFoundError(method, contentDescriptor); }