diff --git a/src/parse.test.ts b/src/parse.test.ts index 75d6b2d1..3b204065 100644 --- a/src/parse.test.ts +++ b/src/parse.test.ts @@ -1,4 +1,5 @@ jest.mock("fs-extra", () => ({ + pathExists: jest.fn(), readJson: jest.fn(), })); @@ -46,6 +47,11 @@ describe("get-schema", () => { expect(schema.methods).toBeDefined(); }); + it("handles being passed an open rpc object", async () => { + const schema: any = await parse(workingSchema); + expect(schema.methods).toBeDefined(); + }); + describe("errors", () => { it("rejects when unable to find file via default", () => { expect.assertions(1); diff --git a/src/parse.ts b/src/parse.ts index 6a219ec7..758fe505 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,8 +1,9 @@ -import { readJson } from "fs-extra"; +import { readJson, pathExists } from "fs-extra"; import isUrl = require("is-url"); import refParser from "json-schema-ref-parser"; import fetch from "node-fetch"; import { getValidationErrors } from "./get-validation-errors"; +import { types } from "@open-rpc/meta-schema"; const cwd = process.cwd(); @@ -31,19 +32,22 @@ const readSchemaFromFile = async (schema: string) => { } }; -export async function parse(schema?: string) { - let parsedSchema; +export async function parse(schema?: string | types.OpenRPC) { + let parsedSchema: types.OpenRPC; if (schema === undefined) { schema = `${cwd}/openrpc.json`; } - if (isJson(schema)) { - parsedSchema = JSON.parse(schema); - } else if (isUrl(schema)) { - parsedSchema = await fetchUrlSchemaFile(schema); + if (typeof schema !== "string") { + parsedSchema = schema; + } else if (isJson(schema as string)) { + parsedSchema = JSON.parse(schema as string); + } else if (isUrl(schema as string)) { + parsedSchema = await fetchUrlSchemaFile(schema as string); } else { - parsedSchema = await readSchemaFromFile(schema); + const isCorrectPath = await pathExists(schema as string); + parsedSchema = await readSchemaFromFile(schema as string); } const errors = getValidationErrors(parsedSchema);