Skip to content

Commit 5aad757

Browse files
committed
feat: add support for proper schema segmentation
This officially formally supports being able to validate specific versions for of an open-rpc document. Previously we had a sort of catchall for all documents, now we have a formal system for validating specific versions. This means we have a pathway forward for larger changes to occur in the ecosystem while maintaining reliability.
1 parent 8de1fc3 commit 5aad757

14 files changed

+892
-542
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ build
44
docs
55
.DS_Store
66
dist
7-
.vscode
7+
.vscode
8+
LLMLOG.MD

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ module.exports = {
44
resetMocks: true,
55
restoreMocks: true,
66
rootDir: "./src",
7-
preset: "ts-jest",
7+
transformIgnorePatterns: ["node_modules/(?!((@open-rpc/spec-types|@open-rpc/spec))/)"],
8+
transform: {
9+
"^.+\\.[tj]sx?$": ["ts-jest", { tsconfig: { allowJs: true } }],
10+
},
811
};

package-lock.json

Lines changed: 811 additions & 519 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@json-schema-tools/dereferencer": "^1.6.3",
3535
"@json-schema-tools/meta-schema": "^1.7.5",
3636
"@json-schema-tools/reference-resolver": "^1.2.6",
37-
"@open-rpc/meta-schema": "^1.14.9",
37+
"@open-rpc/spec-types": "^0.0.1",
3838
"@open-rpc/specification-extension-spec": "^1.0.2",
3939
"ajv": "^6.10.0",
4040
"detect-node": "^2.0.4",
@@ -44,6 +44,7 @@
4444
"isomorphic-fetch": "^3.0.0"
4545
},
4646
"devDependencies": {
47+
"@babel/core": "^7.29.0",
4748
"@types/detect-node": "^2.0.0",
4849
"@types/fs-extra": "^9.0.1",
4950
"@types/is-url": "^1.2.28",
@@ -61,7 +62,7 @@
6162
"json-schema": "^0.4.0",
6263
"prettier": "^2.0.0",
6364
"rimraf": "^3.0.0",
64-
"ts-jest": "^29.1.2",
65+
"ts-jest": "29.4.6",
6566
"typedoc": "^0.25.13",
6667
"typescript": "^4.9.5",
6768
"webpack": "^5.1.3",

src/apply-extension-spec.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import getExtendedMetaSchema from "./get-extended-metaschema";
55

66
describe("applyExtensionSpec", () => {
77
it("should successfully apply extension spec to meta schema", () => {
8-
const result = applyExtensionSpec(goodSchema as OpenrpcDocument, getExtendedMetaSchema());
8+
const result = applyExtensionSpec(goodSchema as OpenrpcDocument, getExtendedMetaSchema("1.3"));
99

1010
// Check if extension was applied to methodObject
1111
const methodObjectDef = result.definitions.methodObject;
@@ -47,7 +47,7 @@ describe("applyExtensionSpec", () => {
4747
"x-extensions": [],
4848
};
4949

50-
const schema = getExtendedMetaSchema();
50+
const schema = getExtendedMetaSchema("1.3");
5151
const result = applyExtensionSpec(emptyExtensionsDoc as OpenrpcDocument, schema);
5252
expect(result).toEqual(schema);
5353
});
@@ -64,7 +64,7 @@ describe("applyExtensionSpec", () => {
6464
};
6565

6666
expect(() => {
67-
applyExtensionSpec(badDoc as OpenrpcDocument, getExtendedMetaSchema());
67+
applyExtensionSpec(badDoc as OpenrpcDocument, getExtendedMetaSchema("1.3"));
6868
}).toThrow("nonExistentDefinition does not exist, cannot apply extension x-notification");
6969
});
7070

@@ -96,7 +96,7 @@ describe("applyExtensionSpec", () => {
9696
],
9797
};
9898

99-
const result = applyExtensionSpec(doc as OpenrpcDocument, getExtendedMetaSchema());
99+
const result = applyExtensionSpec(doc as OpenrpcDocument, getExtendedMetaSchema("1.3"));
100100
expect(result.definitions["x-test-potate"]).toBeDefined();
101101
expect(result.definitions["x-test-potate"].properties["x-next"]).toBeDefined();
102102
expect(result.definitions["x-test-potate"].properties["x-next"].type).toBe("boolean");
@@ -119,7 +119,7 @@ describe("applyExtensionSpec", () => {
119119
});
120120

121121
it("should throw error when extension property already exists", () => {
122-
const schema = getExtendedMetaSchema();
122+
const schema = getExtendedMetaSchema("1.3");
123123
const modifiedSchema = {
124124
...schema,
125125
definitions: {

src/dereference-document.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import dereferenceDocument, { OpenRPCDocumentDereferencingError } from "./dereference-document";
3-
import {
4-
OpenrpcDocument,
5-
ContentDescriptorObject,
6-
JSONSchema,
7-
MethodObject,
8-
} from "./types";
3+
import { OpenrpcDocument, ContentDescriptorObject, JSONSchema, MethodObject } from "./types";
94
import { JSONSchemaObject } from "@json-schema-tools/meta-schema";
105

116
const workingDocument: OpenrpcDocument = {

src/dereference-document.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Dereferencer from "@json-schema-tools/dereferencer";
2-
import metaSchema from "@open-rpc/meta-schema";
2+
import getMetaSchemaForVersion from "./get-meta-schema-for-version";
33
import {
44
OpenrpcDocument as OpenRPC,
55
ReferenceObject,
@@ -476,6 +476,7 @@ export default async function dereferenceDocument(
476476
derefDoc = await handleSchemaComponents(derefDoc);
477477
derefDoc = await handleSchemasInsideContentDescriptorComponents(derefDoc);
478478

479+
const metaSchema = getMetaSchemaForVersion(openrpcDocument.openrpc);
479480
const definitionsMap = createDefinitionsMap(metaSchema);
480481
// eslint-disable-next-line @typescript-eslint/no-explicit-any
481482
const extensions = [] as any;

src/get-document-extended-metaschema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import getExtendedMetaSchema from "./get-extended-metaschema";
44

55
// eslint-disable-next-line @typescript-eslint/no-explicit-any
66
export default function getDocumentExtendedMetaSchema(document: OpenrpcDocument): any {
7-
const extendedMetaSchema = getExtendedMetaSchema();
7+
const extendedMetaSchema = getExtendedMetaSchema(document.openrpc);
88
const extendedDocument = applyExtensionSpec(document, extendedMetaSchema);
99
return extendedDocument;
1010
}

src/get-extended-metaschema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import metaSchema from "@open-rpc/meta-schema";
1+
import getMetaSchemaForVersion from "./get-meta-schema-for-version";
22
import { metaSchema as extensionMetaSchema } from "@open-rpc/specification-extension-spec";
33

44
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5-
function getExtendedMetaSchema(): any {
5+
function getExtendedMetaSchema(version = "1.3"): any {
66
// NOTE: this is to make sure we don't mutate the original meta schema
7-
const metaSchemaCopy = JSON.parse(JSON.stringify(metaSchema));
7+
const metaSchemaCopy = JSON.parse(JSON.stringify(getMetaSchemaForVersion(version)));
88
// eslint-disable-next-line @typescript-eslint/no-explicit-any
99
const extensionMetaSchemaCopy = { ...extensionMetaSchema } as any;
1010

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import getMetaSchemaForVersion from "./get-meta-schema-for-version";
2+
3+
describe("getMetaSchemaForVersion", () => {
4+
it("should successfully apply extension spec to meta schema", () => {
5+
const result = getMetaSchemaForVersion("1.3");
6+
expect(result).toBeDefined();
7+
if ("enum" in result.properties["openrpc"]) {
8+
expect(result.properties["openrpc"].enum).toContain("1.3.2");
9+
} else {
10+
throw new Error("OpenRPC schema version 1.3 is not supported");
11+
}
12+
13+
const result1_4 = getMetaSchemaForVersion("1.4");
14+
expect(result1_4).toBeDefined();
15+
if ("regex" in result1_4.properties["openrpc"]) {
16+
expect(result1_4.properties["openrpc"].regex).toContain("4");
17+
} else {
18+
throw new Error("OpenRPC schema version 1.4 is not supported");
19+
}
20+
});
21+
22+
it("should throw an error for an unsupported version", () => {
23+
expect(() => getMetaSchemaForVersion("99.9")).toThrow(
24+
"Unsupported OpenRPC schema version: 99.9"
25+
);
26+
});
27+
});

0 commit comments

Comments
 (0)