diff --git a/README.md b/README.md index ff9a5f1..f29f32f 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,9 @@ Schema, such as `@hyperjump/json-schema/draft-2020-12`. * **unregisterSchema**: (uri: string) => void Remove a schema from the local schema registry. +* **getAllRegisteredSchemaUris**: () => string[] + + This function returns the URIs of all registered schemas * **hasSchema**: (uri: string) => boolean Check if a schema with the given URI is already registered. diff --git a/lib/get-all-registered-schemas-uris.spec.ts b/lib/get-all-registered-schemas-uris.spec.ts new file mode 100644 index 0000000..1a786d7 --- /dev/null +++ b/lib/get-all-registered-schemas-uris.spec.ts @@ -0,0 +1,25 @@ +import { test, expect, describe } from "vitest"; +import { registerSchema, getAllRegisteredSchemaUris } from "../draft-07/index.js"; + + +describe("getAllRegisteredSchemaUris function", () => { + test("should return all registered schema URIs", () => { + const schemaUris = getAllRegisteredSchemaUris(); + expect(schemaUris).toEqual([ + "http://json-schema.org/draft-07/schema" + ]); + }); + + test("should return all schemas along with custom registered schemas", () => { + registerSchema({ + "$id": "http://example.com/custom-schema", + "$schema": "http://json-schema.org/draft-07/schema#" + }); + + const schemaUris = getAllRegisteredSchemaUris(); + expect(schemaUris).toEqual([ + "http://json-schema.org/draft-07/schema", + "http://example.com/custom-schema" + ]); + }); +}); diff --git a/lib/index.d.ts b/lib/index.d.ts index 9832fa6..a88d7c6 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -9,6 +9,7 @@ export type SchemaObject = { // eslint-disable-line @typescript-eslint/consisten export const registerSchema: (schema: SchemaObject | boolean, retrievalUri?: string, contextDialectId?: string) => void; export const unregisterSchema: (retrievalUri: string) => void; export const hasSchema: (uri: string) => boolean; +export const getAllRegisteredSchemaUris: () => string[]; /** * @deprecated since 1.7.0. Use registerSchema instead. diff --git a/lib/index.js b/lib/index.js index 4cd5bd6..1c1afe1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -130,7 +130,7 @@ addKeyword(vocabulary); addKeyword(writeOnly); export { addSchema, unregisterSchema, validate, FLAG } from "./core.js"; -export { registerSchema, hasSchema } from "./schema.js"; +export { registerSchema, hasSchema, getAllRegisteredSchemaUris } from "./schema.js"; export { getMetaSchemaOutputFormat, setMetaSchemaOutputFormat, diff --git a/lib/schema.js b/lib/schema.js index 5e73622..b701a0f 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -59,6 +59,8 @@ export const unregisterSchema = (uri) => { delete schemaRegistry[normalizedUri]; }; +export const getAllRegisteredSchemaUris = () => Object.keys(schemaRegistry); + export const hasSchema = (uri) => uri in schemaRegistry; export const buildSchemaDocument = (schema, id, dialectId, embedded = {}) => {