From c62a6a3d3e500168c301588d4ee1647d5d1a0a8e Mon Sep 17 00:00:00 2001 From: AZZAM UDDIN Date: Wed, 13 Mar 2024 19:22:07 +0530 Subject: [PATCH] Updated test, documentation and keyword.js --- README.md | 3 ++ lib/experimental.d.ts | 2 +- lib/experimental.js | 2 +- lib/get-dialect-ids.spec.ts | 68 +++++++++++++++++++++++++++++++++++++ lib/keywords.js | 4 +-- 5 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 lib/get-dialect-ids.spec.ts diff --git a/README.md b/README.md index 2d2ca4a3..432725db 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/experimental.d.ts b/lib/experimental.d.ts index 537fc51a..20193c42 100644 --- a/lib/experimental.d.ts +++ b/lib/experimental.d.ts @@ -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 = { id: string; diff --git a/lib/experimental.js b/lib/experimental.js index d213b7d2..38c00cf7 100644 --- a/lib/experimental.js +++ b/lib/experimental.js @@ -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"; diff --git a/lib/get-dialect-ids.spec.ts b/lib/get-dialect-ids.spec.ts new file mode 100644 index 00000000..4f77a5f3 --- /dev/null +++ b/lib/get-dialect-ids.spec.ts @@ -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" + ]); + }); +}); diff --git a/lib/keywords.js b/lib/keywords.js index 35e51a95..e0a3530e 100644 --- a/lib/keywords.js +++ b/lib/keywords.js @@ -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);