Skip to content

Commit

Permalink
Merge pull request #870 from open-rpc/fix/update-openrpc-meta-schema
Browse files Browse the repository at this point in the history
fix: update @open-rpc/meta-schema to latest
  • Loading branch information
BelfordZ authored May 9, 2024
2 parents 1ca064e + 1434c99 commit 1422e57
Show file tree
Hide file tree
Showing 10 changed files with 1,752 additions and 5,648 deletions.
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ module.exports = {
resetMocks: true,
restoreMocks: true,
rootDir: './src',
testEnvironment: 'jsdom',
preset: 'ts-jest'
};
7,288 changes: 1,699 additions & 5,589 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
},
"homepage": "https://github.com/open-rpc/schema-utils-js#readme",
"dependencies": {
"@json-schema-tools/dereferencer": "1.6.2",
"@json-schema-tools/meta-schema": "1.7.5",
"@json-schema-tools/reference-resolver": "1.2.5",
"@open-rpc/meta-schema": "1.14.2",
"@json-schema-tools/dereferencer": "^1.6.3",
"@json-schema-tools/meta-schema": "^1.7.5",
"@json-schema-tools/reference-resolver": "^1.2.6",
"@open-rpc/meta-schema": "^1.14.9",
"ajv": "^6.10.0",
"detect-node": "^2.0.4",
"fast-safe-stringify": "^2.0.7",
Expand All @@ -46,20 +46,20 @@
"@types/fs-extra": "^9.0.1",
"@types/is-url": "^1.2.28",
"@types/isomorphic-fetch": "0.0.36",
"@types/jest": "^26.0.5",
"@types/jest": "^29.5.12",
"@types/json-schema": "^7.0.3",
"@types/node-fetch": "^2.1.6",
"@types/rimraf": "^3.0.0",
"@types/webpack-env": "^1.13.9",
"@typescript-eslint/eslint-plugin": "^4.11.1",
"@typescript-eslint/parser": "^4.11.1",
"eslint": "^7.17.0",
"jest": "^25.1.0",
"jest": "^29.7.0",
"json-schema": "^0.4.0",
"rimraf": "^3.0.0",
"ts-jest": "^25.0.0",
"typedoc": "^0.19.2",
"typescript": "^3.3.3333",
"ts-jest": "^29.1.2",
"typedoc": "^0.25.13",
"typescript": "^4.9.5",
"webpack": "^5.1.3",
"webpack-cli": "^5.1.4"
}
Expand Down
6 changes: 3 additions & 3 deletions src/dereference-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const derefItem = async (item: ReferenceObject, doc: OpenRPC, resolver: Referenc
try {
// returns resolved value of the reference
return (await resolver.resolve($ref, doc) as any);
} catch (err) {
} catch (err: any) {
throw new OpenRPCDocumentDereferencingError([
`unable to eval pointer against OpenRPC Document.`,
`error type: ${err.name}`,
Expand Down Expand Up @@ -64,7 +64,7 @@ const handleSchemaWithSchemaComponents = async (s: JSONSchema, schemaComponents:
delete dereffed.components;
}
return dereffed;
} catch (e) {
} catch (e: any) {
throw new OpenRPCDocumentDereferencingError([
"Unable to parse reference inside of JSONSchema",
s.title ? `Schema Title: ${s.title}` : "",
Expand Down Expand Up @@ -189,7 +189,7 @@ const handleMethod = async (methodOrRef: MethodOrReference, doc: OpenrpcDocument
*/
export default async function dereferenceDocument(openrpcDocument: OpenRPC, resolver: ReferenceResolver): Promise<OpenRPC> {
let derefDoc = { ...openrpcDocument };

derefDoc = await handleSchemaComponents(derefDoc);
derefDoc = await handleSchemasInsideContentDescriptorComponents(derefDoc);
const methods = [] as any;
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
Expand Up @@ -5,7 +5,7 @@ import { TGetOpenRPCDocument } from "./get-open-rpc-document";
const readSchemaFromFile: TGetOpenRPCDocument = async (filePath: string) => {
try {
return await readJson(filePath) as OpenRPC;
} catch (e) {
} catch (e: any) {
if (e.message.includes("SyntaxError")) {
throw new Error(`Failed to parse json in file ${filePath}`);
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/parse-open-rpc-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ describe("parseOpenRPCDocument", () => {
});

it("should make a reference resolver", ()=> {
const resolver = makeCustomResolver({"file":
async (): Promise<JSONSchema> => {
const resolver = makeCustomResolver({"file":
async (): Promise<JSONSchema> => {
return {}
}
});
Expand All @@ -266,8 +266,8 @@ describe("parseOpenRPCDocument", () => {
});

it("should handle custom resolver option", async ()=> {
const resolver = makeCustomResolver({"handler":
async (uri: string): Promise<JSONSchema> => {
const resolver = makeCustomResolver({"handler":
async (uri: string): Promise<JSONSchema> => {
return {}
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/parse-open-rpc-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface ParseOpenRPCDocumentOptions {
/*
* Enable custom reference resolver. This will allow people to resolve 3rd party custom reference values like for ipfs.
*
* @default defaultReferenceResolver
* @default defaultReferenceResolver
*
*/
resolver?: ReferenceResolver;
Expand Down Expand Up @@ -105,6 +105,7 @@ const makeParseOpenRPCDocument = (fetchUrlSchema: TGetOpenRPCDocument, readSchem
parsedSchema = await readSchemaFromFile(schema as string);
}


if (parseOptions.validate) {
const isValid = validateOpenRPCDocument(parsedSchema);
if (isValid instanceof OpenRPCDocumentValidationError) {
Expand Down
28 changes: 15 additions & 13 deletions src/validate-open-rpc-document.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import validateOpenRPCDocument, { OpenRPCDocumentValidationError } from "./validate-open-rpc-document";
import validateOpenRPCDocument, {
OpenRPCDocumentValidationError,
} from "./validate-open-rpc-document";
import { OpenrpcDocument } from "@open-rpc/meta-schema";

describe("validateOpenRPCDocument", () => {
Expand All @@ -15,7 +17,7 @@ describe("validateOpenRPCDocument", () => {
const result = validateOpenRPCDocument(testSchema as OpenrpcDocument);

expect(result).not.toBe(null);
expect(result).toBeInstanceOf(OpenRPCDocumentValidationError)
expect(result).toBeInstanceOf(OpenRPCDocumentValidationError);
});

it("errors when passed an incorrect doc that is deep", () => {
Expand All @@ -32,16 +34,16 @@ describe("validateOpenRPCDocument", () => {
result: {
name: "foobar",
schema: {
type: "not real"
}
}
}
type: "not real",
},
},
},
],
openrpc: "1.0.0-rc1",
};
const result = validateOpenRPCDocument(testSchema as OpenrpcDocument);
expect(result).not.toBe(null);
expect(result).toBeInstanceOf(OpenRPCDocumentValidationError)
expect(result).toBeInstanceOf(OpenRPCDocumentValidationError);
});

it("works fine whn there are file refs", () => {
Expand All @@ -58,16 +60,16 @@ describe("validateOpenRPCDocument", () => {
result: {
name: "foobar",
schema: {
$ref: `${__dirname}/good-schema.json`
}
}
}
$ref: `${__dirname}/good-schema.json`,
},
},
},
],
openrpc: "1.0.0-rc1",
};

const result = validateOpenRPCDocument(testSchema as OpenrpcDocument);
expect(result).toBe(true);
expect(result).not.toBeInstanceOf(OpenRPCDocumentValidationError)
expect(result).not.toBeInstanceOf(OpenRPCDocumentValidationError);
});

});
18 changes: 15 additions & 3 deletions src/validate-open-rpc-document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import metaSchema, { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import Ajv, { ErrorObject } from "ajv";
import JsonSchemaMetaSchema from "@json-schema-tools/meta-schema";

/**
* @ignore
Expand Down Expand Up @@ -37,7 +38,7 @@ export class OpenRPCDocumentValidationError implements Error {
*
* import { validateOpenRPCDocument } from "@open-rpc/schema-utils-js";
* const badOpenRPCDocument = {} as any;
*
*
* const result = validateOpenRPCDocument(badOpenRPCDocument);
* if (result !== true) {
* console.error(result);
Expand All @@ -46,15 +47,26 @@ export class OpenRPCDocumentValidationError implements Error {
*
*/
export default function validateOpenRPCDocument(
document: OpenRPC,
document: OpenRPC
): OpenRPCDocumentValidationError | true {
const ajv = new Ajv();
ajv.addSchema(JsonSchemaMetaSchema, "https://meta.json-schema.tools");
const metaSchemaCopy = { ...metaSchema } as any;
delete metaSchemaCopy.definitions.JSONSchema.$id;
delete metaSchemaCopy.definitions.JSONSchema.$schema;
delete metaSchemaCopy.$schema;
delete metaSchemaCopy.$id;
ajv.validate(metaSchemaCopy, document);
try {
ajv.validate(metaSchemaCopy, document);
} catch (e) {
throw new Error([
'schema-utils-js: Internal Error',
'-----',
e,
'-----',
'If you see this report it: https://github.com/open-rpc/schema-utils-js/issues',
].join('\n'));
}

if (ajv.errors) {
return new OpenRPCDocumentValidationError(ajv.errors as ErrorObject[]);
Expand Down
28 changes: 4 additions & 24 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,18 @@
},
"typedocOptions": {
"name": "@open-rpc/schema-utils-js",
"mode": "file",
"src": [ "./src/index.ts" ],
"entryPoints": [
"./src/index.ts"
],
"out": "docs",
"excludeExternals": true,
"includeDeclarations": false,
"excludeNotExported": true,
"excludePrivate": true,
"excludeProtected": true,
"hideGenerator": true,
"listInvalidSymbolLinks": true,
"readme": "./README.md",
"exclude": [
"node_modules",
"lib",
"docs",
"build",
"jest.config.js",
"src/*.test.ts",
"src/**/*.test.ts"
],
"toc": [
"parseOpenRPCDocument",
"makeDereferenceDocument",
"generateMethodParamId",
"generateMethodResultId",
"validateOpenRPCDocument",
"MethodCallValidator",
"MethodTypings",
"ParameterValidationError",
"OpenRPCDocumentValidationError",
"OpenRPCDocumentDereferencingError",
"ContentDescriptorNotFoundInMethodError",
"dereferenceDocument"
]
}
}

0 comments on commit 1422e57

Please sign in to comment.