Skip to content

Commit ce4a88e

Browse files
authored
added getAllSchema function (#76)
1 parent 5a3810f commit ce4a88e

5 files changed

+32
-1
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ Schema, such as `@hyperjump/json-schema/draft-2020-12`.
201201
* **unregisterSchema**: (uri: string) => void
202202
203203
Remove a schema from the local schema registry.
204+
* **getAllRegisteredSchemaUris**: () => string[]
205+
206+
This function returns the URIs of all registered schemas
204207
* **hasSchema**: (uri: string) => boolean
205208
206209
Check if a schema with the given URI is already registered.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { test, expect, describe } from "vitest";
2+
import { registerSchema, getAllRegisteredSchemaUris } from "../draft-07/index.js";
3+
4+
5+
describe("getAllRegisteredSchemaUris function", () => {
6+
test("should return all registered schema URIs", () => {
7+
const schemaUris = getAllRegisteredSchemaUris();
8+
expect(schemaUris).toEqual([
9+
"http://json-schema.org/draft-07/schema"
10+
]);
11+
});
12+
13+
test("should return all schemas along with custom registered schemas", () => {
14+
registerSchema({
15+
"$id": "http://example.com/custom-schema",
16+
"$schema": "http://json-schema.org/draft-07/schema#"
17+
});
18+
19+
const schemaUris = getAllRegisteredSchemaUris();
20+
expect(schemaUris).toEqual([
21+
"http://json-schema.org/draft-07/schema",
22+
"http://example.com/custom-schema"
23+
]);
24+
});
25+
});

lib/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type SchemaObject = { // eslint-disable-line @typescript-eslint/consisten
99
export const registerSchema: (schema: SchemaObject | boolean, retrievalUri?: string, contextDialectId?: string) => void;
1010
export const unregisterSchema: (retrievalUri: string) => void;
1111
export const hasSchema: (uri: string) => boolean;
12+
export const getAllRegisteredSchemaUris: () => string[];
1213

1314
/**
1415
* @deprecated since 1.7.0. Use registerSchema instead.

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ addKeyword(vocabulary);
130130
addKeyword(writeOnly);
131131

132132
export { addSchema, unregisterSchema, validate, FLAG } from "./core.js";
133-
export { registerSchema, hasSchema } from "./schema.js";
133+
export { registerSchema, hasSchema, getAllRegisteredSchemaUris } from "./schema.js";
134134
export {
135135
getMetaSchemaOutputFormat,
136136
setMetaSchemaOutputFormat,

lib/schema.js

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export const unregisterSchema = (uri) => {
5959
delete schemaRegistry[normalizedUri];
6060
};
6161

62+
export const getAllRegisteredSchemaUris = () => Object.keys(schemaRegistry);
63+
6264
export const hasSchema = (uri) => uri in schemaRegistry;
6365

6466
export const buildSchemaDocument = (schema, id, dialectId, embedded = {}) => {

0 commit comments

Comments
 (0)