Skip to content

Commit

Permalink
Updated test, documentation and keyword.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Azzam1503 authored and jdesrosiers committed Mar 27, 2024
1 parent 48c1f34 commit c62a6a3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ These are available from the `@hyperjump/json-schema/experimental` export.
Remove a dialect. You shouldn't need to use this function. It's called for
you when you call `unregisterSchema`.
* **getDialectIds**
This function retrieves the identifiers of all loaded JSON Schema dialects.
* **Validation**: Keyword
A Keyword object that represents a "validate" operation. You would use this
Expand Down
2 changes: 1 addition & 1 deletion lib/experimental.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const defineVocabulary: (id: string, keywords: { [keyword: string]: strin
export const loadDialect: (dialectId: string, dialect: { [vocabularyId: string]: boolean }, allowUnknownKeywords?: boolean) => void;
export const unloadDialect: (dialectId: string) => void;
export const hasDialect: (dialectId: string) => boolean;
export const loadSchemaDialects: () => string[];
export const getDialectIds: () => string[];

export type Keyword<A> = {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion lib/experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { compile, interpret, BASIC, DETAILED, VERBOSE } from "./core.js";
export {
addKeyword, getKeyword, getKeywordByName, getKeywordName, getKeywordId,
defineVocabulary,
loadDialect, unloadDialect, hasDialect, loadSchemaDialects
loadDialect, unloadDialect, hasDialect, getDialectIds
} from "./keywords.js";
export { getSchema, toSchema, canonicalUri, buildSchemaDocument } from "./schema.js";
export { default as Validation } from "./keywords/validation.js";
Expand Down
68 changes: 68 additions & 0 deletions lib/get-dialect-ids.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { test, expect, describe } from "vitest";
import { getDialectIds, loadDialect } from "./keywords.js";
import "../draft-2020-12";
import "../draft-2019-09";
import "../draft-04";
import "../draft-06";
import "../draft-07";
import "../openapi-3-0";
import "../openapi-3-1";
import "../stable";


describe("getDialectIds function", () => {
test("should return only imported schema in the array if no custom dialects are loaded", () => {
const dialectIds = getDialectIds();
expect(dialectIds).toEqual([
"https://json-schema.org/draft/2020-12/schema",
"https://json-schema.org/draft/2019-09/schema",
"http://json-schema.org/draft-04/schema",
"http://json-schema.org/draft-06/schema",
"http://json-schema.org/draft-07/schema",
"https://spec.openapis.org/oas/3.0/dialect",
"https://spec.openapis.org/oas/3.0/schema",
"https://spec.openapis.org/oas/3.1/dialect/base",
"https://spec.openapis.org/oas/3.1/schema-base",
"https://spec.openapis.org/oas/3.1/schema-base/latest",
"https://spec.openapis.org/oas/3.1/schema-draft-2020-12",
"https://spec.openapis.org/oas/3.1/schema-draft-2019-09",
"https://spec.openapis.org/oas/3.1/schema-draft-07",
"https://spec.openapis.org/oas/3.1/schema-draft-06",
"https://spec.openapis.org/oas/3.1/schema-draft-04",
"https://json-schema.org/validation"
]);
});

test("returns an array of dialect identifiers that are either imported in the file or loaded as custom dialects", () => {
//Load some dialects before each test
loadDialect("http://example.com/dialect1", {
"https://json-schema.org/draft/2020-12/vocab/core": true,
"https://json-schema.org/draft/2020-12/vocab/applicator": true
});
loadDialect("http://example.com/dialect2", {
"https://json-schema.org/draft/2020-12/vocab/core": true,
"https://json-schema.org/draft/2020-12/vocab/applicator": true
});
const dialectIds = getDialectIds();
expect(dialectIds).toEqual([
"https://json-schema.org/draft/2020-12/schema",
"https://json-schema.org/draft/2019-09/schema",
"http://json-schema.org/draft-04/schema",
"http://json-schema.org/draft-06/schema",
"http://json-schema.org/draft-07/schema",
"https://spec.openapis.org/oas/3.0/dialect",
"https://spec.openapis.org/oas/3.0/schema",
"https://spec.openapis.org/oas/3.1/dialect/base",
"https://spec.openapis.org/oas/3.1/schema-base",
"https://spec.openapis.org/oas/3.1/schema-base/latest",
"https://spec.openapis.org/oas/3.1/schema-draft-2020-12",
"https://spec.openapis.org/oas/3.1/schema-draft-2019-09",
"https://spec.openapis.org/oas/3.1/schema-draft-07",
"https://spec.openapis.org/oas/3.1/schema-draft-06",
"https://spec.openapis.org/oas/3.1/schema-draft-04",
"https://json-schema.org/validation",
"http://example.com/dialect1",
"http://example.com/dialect2"
]);
});
});
4 changes: 1 addition & 3 deletions lib/keywords.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,4 @@ export const unloadDialect = (dialectId) => {
delete _dialects[dialectId];
};

export const loadSchemaDialects = () => {
return Object.keys(_dialects).filter((key) => hasDialect(key));
};
export const getDialectIds = () => Object.keys(_dialects);

0 comments on commit c62a6a3

Please sign in to comment.