Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed freezing async ZodReadonly results #3457

Merged
merged 3 commits into from
May 2, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2681,7 +2681,7 @@ Note that branded types do not affect the runtime result of `.parse`. It is a st
This method returns a `ZodReadonly` schema instance that parses the input using the base schema, then calls `Object.freeze()` on the result. The inferred type is also marked as `readonly`.

```ts
const schema = z.object({ name: string }).readonly();
const schema = z.object({ name: z.string() }).readonly();
type schema = z.infer<typeof schema>;
// Readonly<{name: string}>

Expand Down
50 changes: 50 additions & 0 deletions src/__tests__/readonly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,53 @@ test("object freezing", () => {
)
).toBe(true);
});

test("async object freezing", async () => {
expect(
Object.isFrozen(await z.array(z.string()).readonly().parseAsync(["a"]))
).toBe(true);
expect(
Object.isFrozen(
await z.tuple([z.string(), z.number()]).readonly().parseAsync(["a", 1])
)
).toBe(true);
expect(
Object.isFrozen(
await z
.map(z.string(), z.date())
.readonly()
.parseAsync(new Map([["a", new Date()]]))
)
).toBe(true);
expect(
Object.isFrozen(
await z
.set(z.promise(z.string()))
.readonly()
.parseAsync(new Set([Promise.resolve("a")]))
)
).toBe(true);
expect(
Object.isFrozen(
await z.record(z.string()).readonly().parseAsync({ a: "b" })
)
).toBe(true);
expect(
Object.isFrozen(
await z.record(z.string(), z.number()).readonly().parseAsync({ a: 1 })
)
).toBe(true);
expect(
Object.isFrozen(
await z
.object({ a: z.string(), 1: z.number() })
.readonly()
.parseAsync({ a: "b", 1: 2 })
)
).toBe(true);
expect(
Object.isFrozen(
await z.promise(z.string()).readonly().parseAsync(Promise.resolve("a"))
)
).toBe(true);
});
13 changes: 9 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5041,10 +5041,15 @@ export class ZodReadonly<T extends ZodTypeAny> extends ZodType<
> {
_parse(input: ParseInput): ParseReturnType<this["_output"]> {
const result = this._def.innerType._parse(input);
if (isValid(result)) {
result.value = Object.freeze(result.value);
}
return result;
const freeze = (data: ParseReturnType<this["_output"]>) => {
if (isValid(data)) {
data.value = Object.freeze(data.value);
}
return data;
};
return isAsync(result)
? result.then((data) => freeze(data))
: freeze(result);
}

static create = <T extends ZodTypeAny>(
Expand Down