Skip to content

Commit

Permalink
fix: tests passing again
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Apr 12, 2019
1 parent 7ae6f8d commit 4157813
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
9 changes: 5 additions & 4 deletions src/generate-method-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import { MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema";
* Provides an error interface for handling when we are unable to find a contentDescriptor in a methodObject
* when it is expected.
*/
export class ContentDescriptorNotFoundInMethodError extends Error {
export class ContentDescriptorNotFoundInMethodError implements Error {
public name = "OpenRPCDocumentDereferencingError";
public message: string;

/**
* @param method OpenRPC Method which was used for the lookup
* @param contentDescriptor OpenRPC Content Descriptor that was expected to be in the method param.
*/
constructor(public method: MethodObject, public contentDescriptor: ContentDescriptorObject) {
/* istanbul ignore next */
super([
this.message = [
"Content Descriptor not found in method.",
`Method: ${JSON.stringify(method, undefined, " ")}`,
`ContentDescriptor: ${JSON.stringify(contentDescriptor, undefined, " ")}`,
].join("\n")) /* istanbul ignore next */;
].join("\n");
}
}

Expand Down
3 changes: 2 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,6 @@
import MethodCallValidator from "./method-call-validator";
import { OpenRPC } from "@open-rpc/meta-schema";
import MethodCallParameterValidationError from "./parameter-validation-error";

const getExampleSchema = (): OpenRPC => ({
info: { title: "123", version: "1" },
Expand Down Expand Up @@ -47,7 +48,7 @@ describe("MethodCallValidator", () => {
const methodCallValidator = new MethodCallValidator(example);
const result = methodCallValidator.validate("foo", [123]);
expect(result.length).toBe(1);
expect(result[0]).toBeInstanceOf(Error);
expect(result[0]).toBeInstanceOf(MethodCallParameterValidationError);
});

});
3 changes: 2 additions & 1 deletion src/method-call-validator/parameter-validation-error.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ParameterValidationError from "./parameter-validation-error";
import { ErrorObject } from "ajv";
import MethodCallParameterValidationError from "./parameter-validation-error";

describe("ParameterValidationError", () => {
const errorObj = {
Expand All @@ -11,6 +12,6 @@ describe("ParameterValidationError", () => {

it("can be instantiated", () => {
const error = new ParameterValidationError(1, { type: "number" }, "hey mom", [errorObj]);
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(MethodCallParameterValidationError);
});
});
8 changes: 5 additions & 3 deletions src/method-call-validator/parameter-validation-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { Schema } from "@open-rpc/meta-schema";
/**
* Provides an error interface for handling when a function call has invalid parameters.
*/
export default class MethodCallParameterValidationError extends Error {
export default class MethodCallParameterValidationError implements Error {
public name = "OpenRPCDocumentDereferencingError";
public message: string;

/**
* @param paramIndex The index of the param that for this error (index
Expand All @@ -19,14 +21,14 @@ export default class MethodCallParameterValidationError extends Error {
public receievedParam: any,
private errors: ErrorObject[],
) {
super([
this.message = [
"Expected param in position ",
paramIndex,
" to match the json schema: ",
JSON.stringify(expectedSchema, undefined, " "),
". The function received instead ",
receievedParam,
".",
].join("")) /* istanbul ignore next */;
].join("");
}
}
22 changes: 11 additions & 11 deletions src/parse-open-rpc-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ jest.mock("fs-extra", () => ({
}));

import * as _fs from "fs-extra";
import parseOpenRPCDocument from "./parse-open-rpc-document";
import parseOpenRPCDocument, { OpenRPCDocumentDereferencingError } from "./parse-open-rpc-document";
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenRPCDocumentValidationError } from "./validate-open-rpc-document";

const fs: any = _fs;

Expand Down Expand Up @@ -82,8 +83,7 @@ describe("parseOpenRPCDocument", () => {

it("rejects when the schema cannot be dereferenced", () => {
expect.assertions(1);
fs.readJson.mockClear();
fs.readJson.mockResolvedValue({
const testDocument = {
...workingSchema,
methods: [
{
Expand All @@ -100,16 +100,16 @@ describe("parseOpenRPCDocument", () => {
},
},
],
});
return expect(parseOpenRPCDocument())
};

return expect(parseOpenRPCDocument(testDocument))
.rejects
.toThrow("The json schema provided cannot be dereferenced");
.toBeInstanceOf(OpenRPCDocumentDereferencingError);
});

it("rejects when the schema is invalid", () => {
expect.assertions(1);
fs.readJson.mockClear();
fs.readJson.mockResolvedValue({
const testDocument = {
...workingSchema,
methods: [
{
Expand All @@ -127,10 +127,10 @@ describe("parseOpenRPCDocument", () => {
zfloobars: 123,
},
],
});
return expect(parseOpenRPCDocument())
};
return expect(parseOpenRPCDocument(testDocument))
.rejects
.toThrow(/Error validating OpenRPC Document against @open-rpc\/meta-schema./);
.toBeInstanceOf(OpenRPCDocumentValidationError);
});

it("rejects when the json provided is invalid from file", () => {
Expand Down
7 changes: 4 additions & 3 deletions src/parse-open-rpc-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ const isJson = (jsonString: string) => {
/**
* Provides an error interface for OpenRPC Document dereferencing problems
*/
export class OpenRPCDocumentDereferencingError extends Error {
export class OpenRPCDocumentDereferencingError implements Error {
public name = "OpenRPCDocumentDereferencingError";
public message: string;

/**
* @param e The error that originated from jsonSchemaRefParser
*/
constructor(e: Error) {
/* istanbul ignore next */
super(`The json schema provided cannot be dereferenced. Received Error: \n ${e.message}`);
this.message = `The json schema provided cannot be dereferenced. Received Error: \n ${e.message}`;
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/validate-open-rpc-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ ajv.addMetaSchema(JsonSchemaDraft07, "https://json-schema.org/draft-07/schema#")
/**
* Provides an error interface for OpenRPC Document validation
*/
export class OpenRPCDocumentValidationError extends Error {
export class OpenRPCDocumentValidationError implements Error {
public name = "OpenRPCDocumentDereferencingError";
public message: string;

/**
* @param errors The errors received by ajv.errors.
*/
constructor(private errors: ErrorObject[]) {
super([
this.message = [
"Error validating OpenRPC Document against @open-rpc/meta-schema.",
"The errors found are as follows:",
JSON.stringify(errors, undefined, " "),
].join("\n")) /* istanbul ignore next */;
].join("\n");
}
}

Expand Down

0 comments on commit 4157813

Please # to comment.