Skip to content

Commit

Permalink
feat: bump to new meta schema fix validators
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Jan 15, 2020
1 parent 40d596b commit 7fbaf91
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 25 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 @@ -30,7 +30,7 @@
},
"homepage": "https://github.com/open-rpc/schema-utils-js#readme",
"dependencies": {
"@open-rpc/meta-schema": "^1.5.3",
"@open-rpc/meta-schema": "^1.6.0",
"ajv": "^6.10.0",
"detect-node": "^2.0.4",
"fs-extra": "^8.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/get-open-rpc-document-from-file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import { readJson } from "fs-extra";
import { TGetOpenRPCDocument } from "./get-open-rpc-document";

Expand Down
2 changes: 1 addition & 1 deletion src/get-open-rpc-document-from-url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import fetch from "isomorphic-fetch";
import { TGetOpenRPCDocument } from "./get-open-rpc-document";

Expand Down
2 changes: 1 addition & 1 deletion src/get-open-rpc-document.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
export type TGetOpenRPCDocument = (schema: string) => Promise<OpenRPC>;
4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs-extra";
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import { parseOpenRPCDocument } from "./";
import rimraf from "rimraf";
import { promisify } from "util";
Expand Down Expand Up @@ -62,7 +62,7 @@ describe("parseOpenRPCDocument", () => {
});

it("should parseOpenRPCDocument from server", async () => {
const {port} = testServer.address() as AddressInfo;
const { port } = testServer.address() as AddressInfo;
const doc = await parseOpenRPCDocument(`http://localhost:${port}/download/openrpc.json`);
expect(_.isEqual(doc, testDoc)).toBe(true);
});
Expand Down
2 changes: 1 addition & 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 { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import MethodCallParameterValidationError from "./parameter-validation-error";
import MethodCallMethodNotFoundError from "./method-not-found-error";

Expand Down
2 changes: 1 addition & 1 deletion src/method-call-validator/method-call-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Ajv, { ErrorObject, Ajv as IAjv } from "ajv";
import * as _ from "lodash";
import { generateMethodParamId } from "../generate-method-id";
import ParameterValidationError from "./parameter-validation-error";
import { OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema";
import MethodNotFoundError from "./method-not-found-error";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/method-call-validator/method-not-found-error.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MethodNotFoundError from "./method-not-found-error";
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";

const exampleDoc = {
info: {
Expand Down
2 changes: 1 addition & 1 deletion src/method-call-validator/method-not-found-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";

/**
* Provides an error interface for handling when a method is trying to be called but does not exist.
Expand Down
4 changes: 2 additions & 2 deletions src/method-call-validator/parameter-validation-error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ErrorObject } from "ajv";
import { Schema } from "@open-rpc/meta-schema";
import { JSONSchema } from "@open-rpc/meta-schema";

/**
* Provides an error interface for handling when a function call has invalid parameters.
Expand All @@ -17,7 +17,7 @@ export default class ParameterValidationError implements Error {
*/
constructor(
public paramIndex: number,
public expectedSchema: Schema,
public expectedSchema: JSONSchema,
public receievedParam: any,
private errors: ErrorObject[],
) {
Expand Down
41 changes: 39 additions & 2 deletions src/parse-open-rpc-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock("fs-extra", () => ({

import * as _fs from "fs-extra";
import makeParseOpenRPCDocument, { OpenRPCDocumentDereferencingError } from "./parse-open-rpc-document";
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import { OpenRPCDocumentValidationError } from "./validate-open-rpc-document";
import fetchUrlSchema from "./get-open-rpc-document-from-url";
import readSchemaFromFile from "./get-open-rpc-document-from-file";
Expand Down Expand Up @@ -40,7 +40,7 @@ const badRefDocument: OpenRPC = {
},
],
};
const invalidDocument: OpenRPC = {
const invalidDocument: any = {
...workingDocument,
methods: [
{
Expand Down Expand Up @@ -149,6 +149,43 @@ describe("parseOpenRPCDocument", () => {
.toBeInstanceOf(OpenRPCDocumentValidationError);
});

it.only("rejects when the schema becomes invalid after dereffing", () => {
expect.assertions(1);
return expect(parseOpenRPCDocument({
openrpc: "1.2.1",
info: {
version: "1",
title: "test",
},
methods: [
{
name: "foo",
params: [
{ $ref: "#/components/contentDescriptors/LeFoo" },
],
result: {
name: "bar",
schema: { $ref: "#/components/contentDescriptors/LeFoo" },
},
},
],
components: {
schemas: {
LeBar: { title: "LeBar", type: "string" },
},
contentDescriptors: {
LeFoo: {
name: "LeFoo",
required: true,
schema: { $ref: "#/components/schemas/LeBar" },
},
},
},
}))
.rejects
.toBeInstanceOf(OpenRPCDocumentValidationError);
});

it("rejects when the json provided is invalid from file", () => {
expect.assertions(1);
fs.readJson.mockClear();
Expand Down
15 changes: 11 additions & 4 deletions src/parse-open-rpc-document.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import refParser from "json-schema-ref-parser";
import validateOpenRPCDocument, { OpenRPCDocumentValidationError } from "./validate-open-rpc-document";
import isUrl = require("is-url");
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import { TGetOpenRPCDocument } from "./get-open-rpc-document";

/**
Expand Down Expand Up @@ -125,16 +125,23 @@ const makeParseOpenRPCDocument = (fetchUrlSchema: TGetOpenRPCDocument, readSchem
}
}

let finalDocument: OpenRPC = parsedSchema;
let postDeref: OpenRPC = parsedSchema;
if (parseOptions.dereference) {
try {
finalDocument = await refParser.dereference(parsedSchema) as OpenRPC;
postDeref = await refParser.dereference(parsedSchema) as OpenRPC;
} catch (e) {
throw new OpenRPCDocumentDereferencingError(e);
}
}

return finalDocument;
if (parseOptions.validate) {
const isValid = validateOpenRPCDocument(postDeref);
if (isValid instanceof OpenRPCDocumentValidationError) {
throw isValid;
}
}

return postDeref;
};
};

Expand Down
4 changes: 2 additions & 2 deletions src/validate-open-rpc-document.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import validateOpenRPCDocument from "./validate-open-rpc-document";
import { OpenRPC } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";

describe("validateOpenRPCDocument", () => {
it("errors when passed an incorrect schema", () => {
const testSchema: OpenRPC = {
const testSchema: any = {
info: {
afooblared: 123,
title: "foobar",
Expand Down
2 changes: 1 addition & 1 deletion src/validate-open-rpc-document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import metaSchema, { OpenRPC } from "@open-rpc/meta-schema";
import metaSchema, { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import JsonSchemaDraft07 from "../lib/json-schema-draft-07.json";
import Ajv, { ErrorObject } from "ajv";

Expand Down
5 changes: 4 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
],
"rules": {
"ordered-imports": false,
"indent": [true, "spaces", 2]
"object-literal-sort-keys": false,
"no-console": [true, "log"],
"indent": [true, "spaces", 2],
"max-classes-per-file" : false
}
}

0 comments on commit 7fbaf91

Please sign in to comment.