diff --git a/CHANGELOG.md b/CHANGELOG.md index 24194f7f59..23ab7b1c72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Version 19 +### v19.3.1 + +- Fixed: the output type of the `ez.raw()` schema (without an argument) was missing the `raw` property (since v19.0.0). + ### v19.3.0 - Feat: Supporting `vitest` version 2: diff --git a/coverage.svg b/coverage.svg index 5bb55be2e2..1b3eb315d9 100644 --- a/coverage.svg +++ b/coverage.svg @@ -1 +1 @@ -Coverage: 100%Coverage100% \ No newline at end of file +Coverage: 100%Coverage100% \ No newline at end of file diff --git a/package.json b/package.json index 7c3270502c..c1911a4600 100644 --- a/package.json +++ b/package.json @@ -185,5 +185,6 @@ "swagger-documentation", "zod", "validation" - ] + ], + "packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610" } diff --git a/src/raw-schema.ts b/src/raw-schema.ts index 9cc492c7c8..4cf6522560 100644 --- a/src/raw-schema.ts +++ b/src/raw-schema.ts @@ -3,11 +3,15 @@ import { file } from "./file-schema"; export const ezRawBrand = Symbol("Raw"); +const base = z.object({ raw: file("buffer") }); + /** Shorthand for z.object({ raw: ez.file("buffer") }) */ -export const raw = (extra: S = {} as S) => - z - .object({ raw: file("buffer") }) - .extend(extra) - .brand(ezRawBrand as symbol); +export function raw(): z.ZodBranded; +export function raw( + extra: S, +): z.ZodBranded>, symbol>; +export function raw(extra?: z.ZodRawShape) { + return (extra ? base.extend(extra) : base).brand(ezRawBrand); +} export type RawSchema = ReturnType; diff --git a/tests/unit/raw-schema.spec.ts b/tests/unit/raw-schema.spec.ts new file mode 100644 index 0000000000..e8f3f9c7ff --- /dev/null +++ b/tests/unit/raw-schema.spec.ts @@ -0,0 +1,52 @@ +import { z } from "zod"; +import { ez } from "../../src"; +import { metaSymbol } from "../../src/metadata"; +import { ezRawBrand } from "../../src/raw-schema"; +import { describe, expect, expectTypeOf, test } from "vitest"; + +describe("ez.raw()", () => { + describe("creation", () => { + test("should be an instance of branded object", () => { + const schema = ez.raw(); + expect(schema).toBeInstanceOf(z.ZodBranded); + expect(schema._def[metaSymbol]?.brand).toBe(ezRawBrand); + }); + }); + + describe("types", () => { + test("without extension", () => { + const schema = ez.raw(); + expectTypeOf(schema._output).toMatchTypeOf<{ raw: Buffer }>(); + }); + + test("with empty extension", () => { + const schema = ez.raw({}); + expectTypeOf(schema._output).toMatchTypeOf<{ raw: Buffer }>(); + }); + + test("with populated extension", () => { + const schema = ez.raw({ extra: z.number() }); + expectTypeOf(schema._output).toMatchTypeOf<{ + raw: Buffer; + extra: number; + }>(); + }); + }); + + describe("parsing", () => { + test("should accept buffer as the raw prop", () => { + const schema = ez.raw(); + expect(schema.parse({ raw: Buffer.from("test"), extra: 123 })).toEqual({ + raw: expect.any(Buffer), + }); + }); + + test("should allow extension", () => { + const schema = ez.raw({ extra: z.number() }); + expect(schema.parse({ raw: Buffer.from("test"), extra: 123 })).toEqual({ + raw: expect.any(Buffer), + extra: 123, + }); + }); + }); +});