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

fix: [#1693] Tuple with empty items #1712

Merged
merged 5 commits into from
Dec 24, 2022
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
1 change: 1 addition & 0 deletions deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`zodix`](https://github.com/rileytomasek/zodix): Zod utilities for FormData and URLSearchParams in Remix loaders and actions.
- [`formik-validator-zod`](https://github.com/glazy/formik-validator-zod): Formik-compliant validator library that simplifies using Zod with Formik.
- [`zod-i18n-map`](https://github.com/aiji42/zod-i18n): Useful for translating Zod error messages.
- [`@modular-forms/solid`](https://github.com/fabian-hiller/modular-forms): Modular form library for SolidJS that supports Zod for validation.

#### Zod to X

Expand Down
6 changes: 6 additions & 0 deletions deno/lib/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ test("continue parsing despite array size error", () => {
expect(result.error.issues.length).toEqual(2);
}
});

test("parse should fail given sparse array", () => {
const schema = z.array(z.string()).nonempty().min(1).max(3);

expect(() => schema.parse(new Array(3))).toThrow();
});
4 changes: 4 additions & 0 deletions deno/lib/__tests__/tuple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ test("tuple with rest schema", () => {
util.assertEqual<t1, [string, number, ...boolean[]]>(true);
});

test("parse should fail given sparse array as tuple", () => {
expect(() => testTuple.parse(new Array(3))).toThrow();
});

// test('tuple with optional elements', () => {
// const result = z
// .tuple([z.string(), z.number().optional()])
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2827,7 +2827,7 @@ export class ZodTuple<
status.dirty();
}

const items = (ctx.data as any[])
const items = ([...ctx.data] as any[])
.map((item, itemIndex) => {
const schema = this._def.items[itemIndex] || this._def.rest;
if (!schema) return null as any as SyncParseReturnType<any>;
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ test("continue parsing despite array size error", () => {
expect(result.error.issues.length).toEqual(2);
}
});

test("parse should fail given sparse array", () => {
const schema = z.array(z.string()).nonempty().min(1).max(3);

expect(() => schema.parse(new Array(3))).toThrow();
});
4 changes: 4 additions & 0 deletions src/__tests__/tuple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ test("tuple with rest schema", () => {
util.assertEqual<t1, [string, number, ...boolean[]]>(true);
});

test("parse should fail given sparse array as tuple", () => {
expect(() => testTuple.parse(new Array(3))).toThrow();
});

// test('tuple with optional elements', () => {
// const result = z
// .tuple([z.string(), z.number().optional()])
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@ export class ZodArray<

if (ctx.common.async) {
return Promise.all(
(ctx.data as any[]).map((item, i) => {
([...ctx.data] as any[]).map((item, i) => {
return def.type._parseAsync(
new ParseInputLazyPath(ctx, item, ctx.path, i)
);
Expand All @@ -1699,7 +1699,7 @@ export class ZodArray<
});
}

const result = (ctx.data as any[]).map((item, i) => {
const result = ([...ctx.data] as any[]).map((item, i) => {
return def.type._parseSync(
new ParseInputLazyPath(ctx, item, ctx.path, i)
);
Expand Down Expand Up @@ -2827,7 +2827,7 @@ export class ZodTuple<
status.dirty();
}

const items = (ctx.data as any[])
const items = ([...ctx.data] as any[])
.map((item, itemIndex) => {
const schema = this._def.items[itemIndex] || this._def.rest;
if (!schema) return null as any as SyncParseReturnType<any>;
Expand Down