Skip to content

Commit

Permalink
fix: add metaSchema to ajv + upgrade typescript to 4
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed May 8, 2024
1 parent 5f8502e commit 437eb78
Show file tree
Hide file tree
Showing 7 changed files with 1,771 additions and 2,633 deletions.
4,353 changes: 1,737 additions & 2,616 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 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.7",
"@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 @@ -59,7 +59,7 @@
"rimraf": "^3.0.0",
"ts-jest": "^25.0.0",
"typedoc": "^0.19.2",
"typescript": "^3.3.3333",
"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);

Check warning on line 30 in src/dereference-document.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
} catch (err) {
} catch (err: any) {

Check warning on line 31 in src/dereference-document.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
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) {

Check warning on line 67 in src/dereference-document.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
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;

Check warning on line 195 in src/dereference-document.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
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
9 changes: 5 additions & 4 deletions src/parse-open-rpc-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,25 @@ describe("parseOpenRPCDocument", () => {
});

it("should make a reference resolver", ()=> {
const resolver = makeCustomResolver({"file":
async (): Promise<JSONSchema> => {
const resolver = makeCustomResolver({"file":
async (): Promise<JSONSchema> => {
return {}
}
});
expect(resolver).toBeDefined()
});

it("should handle dereference option true", async ()=> {
console.log("workingDocument", workingDocument);
const document = await parseOpenRPCDocument(workingDocument,{
dereference: true,
});
expect(document.methods).toBeDefined();
});

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
21 changes: 18 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,29 @@ 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) {
return new OpenRPCDocumentValidationError([
{
message: (e as unknown as Error).message,
dataPath: "unknown",
schemaPath: "unknown",
params: {},
keyword: "unknown",
data: JSON.parse(JSON.stringify(e)),
},
]);
}

if (ajv.errors) {
return new OpenRPCDocumentValidationError(ajv.errors as ErrorObject[]);
Expand Down

0 comments on commit 437eb78

Please sign in to comment.