|
| 1 | +// deno-lint-ignore-file no-explicit-any |
| 2 | +import { type Type } from "./deps.ts"; |
1 | 3 | import { assertEquals } from "../test_deps.ts"; |
2 | | -import { validateRequest } from "./validation.ts"; |
| 4 | +import { |
| 5 | + extractErrorPath, |
| 6 | + type IssueTree, |
| 7 | + validateInput, |
| 8 | + validateRequest, |
| 9 | +} from "./validation.ts"; |
3 | 10 |
|
4 | 11 | const methods = { |
5 | 12 | subtract: (a: number, b: number) => a - b, |
6 | 13 | }; |
7 | 14 |
|
| 15 | +// Mock `Type` class for testing |
| 16 | +class TypeMock implements Type<any> { |
| 17 | + constructor(public parse: (input: any) => any) {} |
| 18 | + nullable: any = () => this; |
| 19 | + toTerminals: any = () => []; |
| 20 | + try: any = (input: unknown) => ({ success: true, value: this.parse(input) }); |
| 21 | + name: any = "TypeMock"; |
| 22 | + func: any = () => this; |
| 23 | + optional: any = () => this; |
| 24 | + default: any = () => this; |
| 25 | + assert: any = () => this; |
| 26 | + map: any = () => this; |
| 27 | + chain: any = () => this; |
| 28 | +} |
| 29 | + |
| 30 | +// Test cases for `validateRequest` |
8 | 31 | Deno.test("validate request object", function (): void { |
9 | 32 | assertEquals( |
10 | 33 | validateRequest( |
@@ -37,3 +60,86 @@ Deno.test("validate request object", function (): void { |
37 | 60 | }, |
38 | 61 | ); |
39 | 62 | }); |
| 63 | + |
| 64 | +// Test cases for `extractErrorPath` |
| 65 | +Deno.test("extractErrorPath - simple IssueTree", () => { |
| 66 | + const issueTree: IssueTree = { |
| 67 | + ok: false, |
| 68 | + code: "error_code", |
| 69 | + expected: ["string"], |
| 70 | + }; |
| 71 | + |
| 72 | + const result = extractErrorPath(issueTree); |
| 73 | + assertEquals(result.path, []); |
| 74 | +}); |
| 75 | + |
| 76 | +Deno.test("extractErrorPath - nested IssueTree", () => { |
| 77 | + const issueTree: IssueTree = { |
| 78 | + ok: false, |
| 79 | + code: "error_code", |
| 80 | + expected: ["string"], |
| 81 | + key: "root", |
| 82 | + tree: { |
| 83 | + ok: false, |
| 84 | + code: "nested_error_code", |
| 85 | + expected: ["number"], |
| 86 | + key: "nested", |
| 87 | + }, |
| 88 | + }; |
| 89 | + |
| 90 | + const result = extractErrorPath(issueTree); |
| 91 | + assertEquals(result.path, ["root", "nested"]); |
| 92 | +}); |
| 93 | + |
| 94 | +Deno.test("extractErrorPath - with path", () => { |
| 95 | + const issueTree: IssueTree = { |
| 96 | + ok: false, |
| 97 | + code: "error_code", |
| 98 | + expected: ["string"], |
| 99 | + key: "root", |
| 100 | + path: ["existing_path"], |
| 101 | + tree: { |
| 102 | + ok: false, |
| 103 | + code: "nested_error_code", |
| 104 | + expected: ["number"], |
| 105 | + key: "nested", |
| 106 | + }, |
| 107 | + }; |
| 108 | + |
| 109 | + const result = extractErrorPath(issueTree); |
| 110 | + assertEquals(result.path, ["existing_path", "root", "nested"]); |
| 111 | +}); |
| 112 | + |
| 113 | +// Test cases for `validateInput` |
| 114 | +Deno.test("validateInput - valid input", () => { |
| 115 | + const validation = new TypeMock((input) => input); |
| 116 | + const input = "valid_input"; |
| 117 | + const validate = validateInput(validation); |
| 118 | + |
| 119 | + const result = validate(input); |
| 120 | + assertEquals(result.kind, "success"); |
| 121 | + assertEquals(result.value, input); |
| 122 | +}); |
| 123 | + |
| 124 | +Deno.test("validateInput - invalid input", () => { |
| 125 | + const validation = new TypeMock(() => { |
| 126 | + throw { |
| 127 | + issueTree: { |
| 128 | + ok: false, |
| 129 | + code: "error_code", |
| 130 | + expected: ["string"], |
| 131 | + key: "input", |
| 132 | + }, |
| 133 | + }; |
| 134 | + }); |
| 135 | + const input = "invalid_input"; |
| 136 | + const validate = validateInput(validation); |
| 137 | + |
| 138 | + const result = validate(input); |
| 139 | + assertEquals(result.kind, "failure"); |
| 140 | + if (result.error) { |
| 141 | + assertEquals(result.error.path, ["input"]); |
| 142 | + } else { |
| 143 | + throw new Error("Expected error to be defined"); |
| 144 | + } |
| 145 | +}); |
0 commit comments