-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32b48ac
commit 0be40b1
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,4 +53,108 @@ describe("ValidateJsonSchema", () => { | |
"schema is required" | ||
); | ||
}); | ||
|
||
describe("formats", () => { | ||
const cases = [ | ||
{ | ||
description: "a valid e-mail address", | ||
input: { data: "[email protected]", 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); | ||
} | ||
}); | ||
}); | ||
}); |