|
| 1 | +// @ts-ignore TS6133 |
| 2 | +import { expect } from "https://deno.land/x/[email protected]/mod.ts"; |
| 3 | +const test = Deno.test; |
| 4 | +import { util } from "../helpers/util.ts"; |
| 5 | + |
| 6 | +import * as z from "../index.ts"; |
| 7 | + |
| 8 | +import type { StandardSchemaV1 } from "@standard-schema/spec"; |
| 9 | + |
| 10 | +test("assignability", () => { |
| 11 | + const _s1: StandardSchemaV1 = z.string(); |
| 12 | + const _s2: StandardSchemaV1<string> = z.string(); |
| 13 | + const _s3: StandardSchemaV1<string, string> = z.string(); |
| 14 | + const _s4: StandardSchemaV1<unknown, string> = z.string(); |
| 15 | + [_s1, _s2, _s3, _s4]; |
| 16 | +}); |
| 17 | + |
| 18 | +test("type inference", () => { |
| 19 | + const stringToNumber = z.string().transform((x) => x.length); |
| 20 | + type input = StandardSchemaV1.InferInput<typeof stringToNumber>; |
| 21 | + util.assertEqual<input, string>(true); |
| 22 | + type output = StandardSchemaV1.InferOutput<typeof stringToNumber>; |
| 23 | + util.assertEqual<output, number>(true); |
| 24 | +}); |
| 25 | + |
| 26 | +test("valid parse", () => { |
| 27 | + const schema = z.string(); |
| 28 | + const result = schema["~standard"]["validate"]("hello"); |
| 29 | + if (result instanceof Promise) { |
| 30 | + throw new Error("Expected sync result"); |
| 31 | + } |
| 32 | + expect(result.issues).toEqual(undefined); |
| 33 | + if (result.issues) { |
| 34 | + throw new Error("Expected no issues"); |
| 35 | + } else { |
| 36 | + expect(result.value).toEqual("hello"); |
| 37 | + } |
| 38 | +}); |
| 39 | + |
| 40 | +test("invalid parse", () => { |
| 41 | + const schema = z.string(); |
| 42 | + const result = schema["~standard"]["validate"](1234); |
| 43 | + if (result instanceof Promise) { |
| 44 | + throw new Error("Expected sync result"); |
| 45 | + } |
| 46 | + expect(result.issues).toBeDefined(); |
| 47 | + if (!result.issues) { |
| 48 | + throw new Error("Expected issues"); |
| 49 | + } |
| 50 | + expect(result.issues.length).toEqual(1); |
| 51 | + expect(result.issues[0].path).toEqual([]); |
| 52 | +}); |
| 53 | + |
| 54 | +test("valid parse async", async () => { |
| 55 | + const schema = z.string().refine(async () => true); |
| 56 | + const _result = schema["~standard"]["validate"]("hello"); |
| 57 | + if (_result instanceof Promise) { |
| 58 | + const result = await _result; |
| 59 | + expect(result.issues).toEqual(undefined); |
| 60 | + if (result.issues) { |
| 61 | + throw new Error("Expected no issues"); |
| 62 | + } else { |
| 63 | + expect(result.value).toEqual("hello"); |
| 64 | + } |
| 65 | + } else { |
| 66 | + throw new Error("Expected async result"); |
| 67 | + } |
| 68 | +}); |
| 69 | + |
| 70 | +test("invalid parse async", async () => { |
| 71 | + const schema = z.string().refine(async () => false); |
| 72 | + const _result = schema["~standard"]["validate"]("hello"); |
| 73 | + if (_result instanceof Promise) { |
| 74 | + const result = await _result; |
| 75 | + expect(result.issues).toBeDefined(); |
| 76 | + if (!result.issues) { |
| 77 | + throw new Error("Expected issues"); |
| 78 | + } |
| 79 | + expect(result.issues.length).toEqual(1); |
| 80 | + expect(result.issues[0].path).toEqual([]); |
| 81 | + } else { |
| 82 | + throw new Error("Expected async result"); |
| 83 | + } |
| 84 | +}); |
0 commit comments