Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added getAllSchema function #76

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions lib/get-all-registered-schemas-uris.spec.ts
Original file line number Diff line number Diff line change
@@ -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"
]);
});
});
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) => {
Expand Down