Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,6 @@
"swagger-documentation",
"zod",
"validation"
]
],
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}
14 changes: 9 additions & 5 deletions src/raw-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <S extends z.ZodRawShape>(extra: S = {} as S) =>
z
.object({ raw: file("buffer") })
.extend(extra)
.brand(ezRawBrand as symbol);
export function raw(): z.ZodBranded<typeof base, symbol>;
export function raw<S extends z.ZodRawShape>(
extra: S,
): z.ZodBranded<ReturnType<typeof base.extend<S>>, symbol>;
export function raw(extra?: z.ZodRawShape) {
return (extra ? base.extend(extra) : base).brand(ezRawBrand);
}

export type RawSchema = ReturnType<typeof raw>;
52 changes: 52 additions & 0 deletions tests/unit/raw-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
});