From fb65508eb479aea6b72043f7fb58d7fbf59b31d8 Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers <7943856+bschoenmaeckers@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:01:27 +0100 Subject: [PATCH] feat(samples): add base64url support for contentEncoding keyword (#9246) This change is JSON Schema 2020-12 specific. --- .../fn/class/EncoderRegistry.js | 2 ++ .../json-schema-2020-12-samples/fn/encoders/base64url.js | 6 ++++++ test/unit/core/plugins/json-schema-2020-12-samples/fn.js | 9 +++++++++ 3 files changed, 17 insertions(+) create mode 100644 src/core/plugins/json-schema-2020-12-samples/fn/encoders/base64url.js diff --git a/src/core/plugins/json-schema-2020-12-samples/fn/class/EncoderRegistry.js b/src/core/plugins/json-schema-2020-12-samples/fn/class/EncoderRegistry.js index f9c575adf9a..ba2213a7731 100644 --- a/src/core/plugins/json-schema-2020-12-samples/fn/class/EncoderRegistry.js +++ b/src/core/plugins/json-schema-2020-12-samples/fn/class/EncoderRegistry.js @@ -9,6 +9,7 @@ import encodeQuotedPrintable from "../encoders/quoted-printable" import encodeBase16 from "../encoders/base16" import encodeBase32 from "../encoders/base32" import encodeBase64 from "../encoders/base64" +import encodeBase64Url from "../encoders/base64url" class EncoderRegistry extends Registry { #defaults = { @@ -19,6 +20,7 @@ class EncoderRegistry extends Registry { base16: encodeBase16, base32: encodeBase32, base64: encodeBase64, + base64url: encodeBase64Url, } data = { ...this.#defaults } diff --git a/src/core/plugins/json-schema-2020-12-samples/fn/encoders/base64url.js b/src/core/plugins/json-schema-2020-12-samples/fn/encoders/base64url.js new file mode 100644 index 00000000000..90f81b2a556 --- /dev/null +++ b/src/core/plugins/json-schema-2020-12-samples/fn/encoders/base64url.js @@ -0,0 +1,6 @@ +/** + * @prettier + */ +const encodeBase64Url = (content) => Buffer.from(content).toString("base64url") + +export default encodeBase64Url diff --git a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js index 5d49a06196e..4d45ad21c4e 100644 --- a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js +++ b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js @@ -138,6 +138,15 @@ describe("sampleFromSchema", () => { ).toStrictEqual( "aHR0cHM6Ly9leGFtcGxlLmNvbS9kaWN0aW9uYXJ5L3t0ZXJtOjF9L3t0ZXJtfQ==" ) + expect( + sample({ + type: "string", + format: "uri-template", + contentEncoding: "base64url", + }) + ).toStrictEqual( + "aHR0cHM6Ly9leGFtcGxlLmNvbS9kaWN0aW9uYXJ5L3t0ZXJtOjF9L3t0ZXJtfQ" + ) expect( sample({ type: "string",