Skip to content

Commit 49171d2

Browse files
authored
Backport ez.raw() fix for v20 (#2529)
Cherry-picking e2b155d for v20.
1 parent e971142 commit 49171d2

4 files changed

Lines changed: 65 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Version 20
44

5+
### v20.22.3
6+
7+
- Fixed: the output type of the `ez.raw()` schema (without an argument) was missing the `raw` property (since v19.0.0).
8+
59
### v20.22.2
610

711
- Fixed a bug that could lead to duplicate properties in generated client types:

coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

src/raw-schema.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import { file } from "./file-schema";
33

44
export const ezRawBrand = Symbol("Raw");
55

6+
const base = z.object({ raw: file("buffer") });
7+
68
/** Shorthand for z.object({ raw: ez.file("buffer") }) */
7-
export const raw = <S extends z.ZodRawShape>(extra: S = {} as S) =>
8-
z
9-
.object({ raw: file("buffer") })
10-
.extend(extra)
11-
.brand(ezRawBrand as symbol);
9+
export function raw(): z.ZodBranded<typeof base, symbol>;
10+
export function raw<S extends z.ZodRawShape>(
11+
extra: S,
12+
): z.ZodBranded<ReturnType<typeof base.extend<S>>, symbol>;
13+
export function raw(extra?: z.ZodRawShape) {
14+
return (extra ? base.extend(extra) : base).brand(ezRawBrand);
15+
}
1216

1317
export type RawSchema = ReturnType<typeof raw>;

tests/unit/raw-schema.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)