|
| 1 | +import { z } from "zod"; |
| 2 | +import { ez } from "../../src"; |
| 3 | +import { metaSymbol } from "../../src/metadata"; |
| 4 | +import { ezRawBrand } from "../../src/raw-schema"; |
| 5 | + |
| 6 | +describe("ez.raw()", () => { |
| 7 | + describe("creation", () => { |
| 8 | + test("should be an instance of branded object", () => { |
| 9 | + const schema = ez.raw(); |
| 10 | + expect(schema).toBeInstanceOf(z.ZodBranded); |
| 11 | + expect(schema._def[metaSymbol]?.brand).toBe(ezRawBrand); |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + describe("types", () => { |
| 16 | + test("without extension", () => { |
| 17 | + const schema = ez.raw(); |
| 18 | + expectTypeOf(schema._output).toMatchTypeOf<{ raw: Buffer }>(); |
| 19 | + }); |
| 20 | + |
| 21 | + test("with empty extension", () => { |
| 22 | + const schema = ez.raw({}); |
| 23 | + expectTypeOf(schema._output).toMatchTypeOf<{ raw: Buffer }>(); |
| 24 | + }); |
| 25 | + |
| 26 | + test("with populated extension", () => { |
| 27 | + const schema = ez.raw({ extra: z.number() }); |
| 28 | + expectTypeOf(schema._output).toMatchTypeOf<{ |
| 29 | + raw: Buffer; |
| 30 | + extra: number; |
| 31 | + }>(); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe("parsing", () => { |
| 36 | + test("should accept buffer as the raw prop", () => { |
| 37 | + const schema = ez.raw(); |
| 38 | + expect(schema.parse({ raw: Buffer.from("test"), extra: 123 })).toEqual({ |
| 39 | + raw: expect.any(Buffer), |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + test("should allow extension", () => { |
| 44 | + const schema = ez.raw({ extra: z.number() }); |
| 45 | + expect(schema.parse({ raw: Buffer.from("test"), extra: 123 })).toEqual({ |
| 46 | + raw: expect.any(Buffer), |
| 47 | + extra: 123, |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | +}); |
0 commit comments