Skip to content

Commit

Permalink
feat: WIP organize extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
zcstarr committed Mar 15, 2024
1 parent 9fee294 commit 0e9176a
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/append-extension-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import metaSchema, { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";

Check warning on line 1 in src/append-extension-schema.ts

View workflow job for this annotation

GitHub Actions / lint

'OpenRPC' is defined but never used
import jsonSchema from "@json-schema-tools/meta-schema"
import extensionSchema from "./open-rpc-extensions-schema.json"

const getMetaSchemaExtended = () => {

Check warning on line 5 in src/append-extension-schema.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
const extensionMetaSchemaCopy = { ...extensionSchema } as any;

Check warning on line 6 in src/append-extension-schema.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const metaSchemaCopy = { ...metaSchema } as any;

Check warning on line 7 in src/append-extension-schema.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
delete extensionMetaSchemaCopy.$schema;
delete extensionMetaSchemaCopy.$id;
const jsonSchemaCopy = {...jsonSchema} as any;

Check warning on line 10 in src/append-extension-schema.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
delete jsonSchemaCopy.$id;
delete jsonSchemaCopy.$schema;
extensionMetaSchemaCopy.properties['x-extensions'].items.properties.schema = jsonSchemaCopy // .schema = jsonSchemaCopy;
metaSchemaCopy.properties['x-extensions'] ={"$ref":"#/definitions/x-extensions"}
metaSchemaCopy.definitions['x-extensions']=extensionMetaSchemaCopy.properties['x-extensions'];
return metaSchemaCopy;
};

export default getMetaSchemaExtended;
65 changes: 65 additions & 0 deletions src/extension-good-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"openrpc": "1.2.6",
"info": {
"title": "Minimal OpenRPC Example",
"version": "1.0.0"
},
"methods": [
{
"name": "getExampleData",
"summary": "Retrieves example data from the API.",
"x-notification": "fff",
"params": [
{
"name": "dataId",
"schema": {
"type": "integer",
"description": "The ID of the data to retrieve."
},
"required": true
}
],
"result": {
"name": "exampleData",
"schema": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"description": "The ID of the data."
},
"data": {
"type": "string",
"description": "The content of the data."
}
},
"required": [
"id",
"data"
]
},
"description": "The result object containing the requested data."
}
}
],
"x-extensions": [
{
"openrpcExtension": "0.0.0-development",
"name": "x-notification",
"version": "0.0.1",
"description": "Describe a notification for OpenRPC methods",
"summary": "OpenRPC Notification",
"externalDocumentation": {
"description": "github",
"url": "https://github.com/open-rpc/specification-extensions-spec/examples/x-notification-openrpc-ext.json"
},
"restricted": [
"methodObject"
],
"schema": {
"type": "boolean",
"description": "wether or not this method is a notification or not"
}
}
]
}
89 changes: 89 additions & 0 deletions src/extensions-meta-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"$schema": "https://meta.json-schema.tools/",
"title": "OpenRPC Specification Extension",
"description": "A standard way to define OpenRPC Specification Extensions. This makes it easier to provide support for Specification Extensions in other tools.",
"type": "object",
"required": [
"name",
"schema",
"openrpcExtension",
"version"
],
"properties": {
"openrpcExtension": {
"title": "openrpcExtensionVersion",
"description": "**REQUIRED**. This string MUST be the semantic version number of the Specification that the document uses.",
"type": "string",
"enum": [
"0.0.0-development"
]
},
"name": {
"title": "specificationExtensionName",
"description": "**REQUIRED** Name of the Specification Extension. MUST start with `x-`",
"type": "string",
"pattern": "^x-",
"examples": [
"x-foobarbaz"
]
},
"version": {
"title": "specificationExtensionVersion",
"description": "**REQUIRED**. The version of the Extension (which is distinct from the `openrpcExtension` version and implementation version).",
"type": "string",
"examples": [
"0.0.1"
]
},
"required": {
"title": "specificationExtensionRequired",
"type": "boolean",
"description": "wether or not this specification extension is required or not"
},
"restricted": {
"title": "restrictedObjects",
"description": "A list of object names to restrict the usage of the specification extension to.",
"type": "array",
"items": {
"title": "restrictedObject",
"description": "restricted object",
"type": "string"
}
},
"description": {
"title": "specificationExtensionDescription",
"type": "string",
"description": "Markdown description describing the specification extension."
},
"summary": {
"title": "specificationExtensionSummary",
"type": "string",
"description": "A short summary of what the specification extension is."
},
"schema": {
"$ref": "https://meta.json-schema.tools"
},
"externalDocumentation": {
"title": "specificationExtensionExternalDocumentationObject",
"type": "object",
"additionalProperties": false,
"description": "Information about specification extension external documentation.",
"required": [
"url"
],
"properties": {
"description": {
"title": "specificationExtensionexternalDocumentationObjectDescription",
"description": "external documentation description.",
"type": "string"
},
"url": {
"title": "specificationExtensionexternalDocumentationObjectUrl",
"description": "external documentation description.",
"type": "string",
"format": "uri"
}
}
}
}
}
98 changes: 98 additions & 0 deletions src/open-rpc-extensions-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"$id": "https://extensions.meta.open-rpc.org/",
"title": "OpenRPC Reserved Extension Property",
"type": "object",
"properties": {
"x-extensions": {
"type": "array",
"items": {
"title": "OpenRPC Specification Extension",
"description": "A standard way to define OpenRPC Specification Extensions. This makes it easier to provide support for Specification Extensions in other tools.",
"type": "object",
"required": [
"name",
"schema",
"openrpcExtension",
"version"
],
"properties": {
"openrpcExtension": {
"title": "openrpcExtensionVersion",
"description": "**REQUIRED**. This string MUST be the semantic version number of the Specification that the document uses.",
"type": "string",
"enum": [
"0.0.0-development"
]
},
"name": {
"title": "specificationExtensionName",
"description": "**REQUIRED** Name of the Specification Extension. MUST start with `x-`",
"type": "string",
"pattern": "^x-",
"examples": [
"x-foobarbaz"
]
},
"version": {
"title": "specificationExtensionVersion",
"description": "**REQUIRED**. The version of the Extension (which is distinct from the `openrpcExtension` version and implementation version).",
"type": "string",
"examples": [
"0.0.1"
]
},
"required": {
"title": "specificationExtensionRequired",
"type": "boolean",
"description": "wether or not this specification extension is required or not"
},
"restricted": {
"title": "restrictedObjects",
"description": "A list of object names to restrict the usage of the specification extension to.",
"type": "array",
"items": {
"title": "restrictedObject",
"description": "restricted object",
"type": "string"
}
},
"description": {
"title": "specificationExtensionDescription",
"type": "string",
"description": "Markdown description describing the specification extension."
},
"summary": {
"title": "specificationExtensionSummary",
"type": "string",
"description": "A short summary of what the specification extension is."
},
"schema": {
"$ref": "https://meta.json-schema.tools"
},
"externalDocumentation": {
"title": "specificationExtensionExternalDocumentationObject",
"type": "object",
"additionalProperties": false,
"description": "Information about specification extension external documentation.",
"required": [
"url"
],
"properties": {
"description": {
"title": "specificationExtensionexternalDocumentationObjectDescription",
"description": "external documentation description.",
"type": "string"
},
"url": {
"title": "specificationExtensionexternalDocumentationObjectUrl",
"description": "external documentation description.",
"type": "string",
"format": "uri"
}
}
}
}
}
}
}
}
7 changes: 4 additions & 3 deletions src/validate-open-rpc-document.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import validateOpenRPCDocument, { OpenRPCDocumentValidationError } from "./validate-open-rpc-document";
import { OpenrpcDocument } from "@open-rpc/meta-schema";
import dummyDoc from "./extension-good-schema.json"

describe("validateOpenRPCDocument", () => {
it("errors when passed an incorrect document", () => {
it.only("errors when passed an incorrect document", () => {
const testSchema = {
info: {
afooblared: 123,
Expand All @@ -12,10 +13,10 @@ describe("validateOpenRPCDocument", () => {
methods: [],
openrpc: "1.0.0-rc1",
};
const result = validateOpenRPCDocument(testSchema as OpenrpcDocument);
const result = validateOpenRPCDocument(dummyDoc 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 Down
7 changes: 6 additions & 1 deletion 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 getMetaSchemaExtended from "./append-extension-schema";

/**
* @ignore
Expand Down Expand Up @@ -49,14 +50,18 @@ export default function validateOpenRPCDocument(
document: OpenRPC,
): OpenRPCDocumentValidationError | true {
const ajv = new Ajv();
const metaSchemaCopy = { ...metaSchema } as any;
const metaSchemaCopy = getMetaSchemaExtended() as any;
// 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);

if (ajv.errors) {
console.log(document)
console.info(ajv.errors)
return new OpenRPCDocumentValidationError(ajv.errors as ErrorObject[]);
} else {
return true;
Expand Down

0 comments on commit 0e9176a

Please sign in to comment.