From 0be40b12055cf8ba3e837e35f276ebbd6b5d189b Mon Sep 17 00:00:00 2001 From: Sanni Date: Thu, 21 Dec 2023 23:08:52 +0100 Subject: [PATCH] test: enables formats --- .../__tests__/ValidateJsonSchema.test.ts | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/src/activities/__tests__/ValidateJsonSchema.test.ts b/src/activities/__tests__/ValidateJsonSchema.test.ts index 4757961..3fdc3ea 100644 --- a/src/activities/__tests__/ValidateJsonSchema.test.ts +++ b/src/activities/__tests__/ValidateJsonSchema.test.ts @@ -53,4 +53,108 @@ describe("ValidateJsonSchema", () => { "schema is required" ); }); + + describe("formats", () => { + const cases = [ + { + description: "a valid e-mail address", + input: { data: "joe.bloggs@example.com", schema: { type: "string", format: "email" } }, + expected: true + }, + { + description: "an invalid e-mail address", + input: { data: "2962", schema: { type: "string", format: "email" } }, + expected: false + }, + + { + description: "valid regex", + input: { data: "[0-9]", schema: { type: "string", format: "regex" } }, + expected: true + }, + { + description: "invalid regex", + input: { data: "[9-0]", schema: { type: "string", format: "regex" } }, + expected: false + }, + { + description: "not string is valid", + input: { data: 123, schema: { type: "number", format: "regex" } }, + expected: true + }, + + { + description: "valid uri", + input: { data: "urn:isbn:978-3-531-18621-4", schema: { type: "string", format: "uri" } }, + expected: true + }, + { + description: "invalid relative uri-reference", + input: { data: "/abc", schema: { type: "string", format: "uri" } }, + expected: false + }, + + { + description: "valid uri-template", + input: { + data: "http://example.com/dictionary/{term:1}/{term}", + schema: { type: "string", format: "uri-template" } + }, + expected: true + }, + { + description: "invalid uri-template", + input: { + data: "http://example.com/dictionary/{term:1}/{term", + schema: { type: "string", format: "uri-template" } + }, + expected: false + }, + + { + description: "valid hostname", + input: { data: "123.example.com", schema: { type: "string", format: "hostname" } }, + expected: true + }, + + { + description: "a valid date string", + input: { data: "1963-06-19", schema: { type: "string", format: "date" } }, + expected: true + }, + { + description: "an invalid date string", + input: { data: "06/19/1963", schema: { type: "string", format: "date" } }, + expected: false + }, + + { + description: "a valid uuid", + input: { data: "f81d4fae-7dec-11d0-a765-00a0c91e6bf6", schema: { type: "string", format: "uuid" } }, + expected: true + }, + { + description: "not valid uuid", + input: { data: "f81d4fae7dec11d0a76500a0c91e6bf6", schema: { type: "string", format: "uuid" } }, + expected: false + } + ]; + + + it.each(cases)("$description", ({ input, expected }) => { + const activity: ValidateJsonSchema = new ValidateJsonSchema(); + const { isValid, errors } = activity.execute(input); + + expect(typeof isValid).toBe("boolean"); + + if (expected) { + expect(isValid).toBe(true); + expect(errors).toBeUndefined(); + } else { + expect(isValid).toBe(false); + expect(errors).toBeDefined(); + expect(Array.isArray(errors)).toBe(true); + } + }); + }); });