From d77c7239941f62cc3ee1b39fb37aad2981f0ae09 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Mon, 2 Mar 2026 10:19:54 -0800 Subject: [PATCH] fix(zod): use named argument lookup for @length validation When @length(max: N) is used without min, positional index [0] picks up the max value and applies it as a minimum constraint. Fix by looking up args by name instead of position in addStringValidation and addListValidation. Fixes #2433 Co-Authored-By: Claude Sonnet 4.6 --- packages/zod/src/utils.ts | 8 ++--- tests/regression/test/issue-2433.test.ts | 43 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 tests/regression/test/issue-2433.test.ts diff --git a/packages/zod/src/utils.ts b/packages/zod/src/utils.ts index 27f3c8579..5874d1664 100644 --- a/packages/zod/src/utils.ts +++ b/packages/zod/src/utils.ts @@ -31,11 +31,11 @@ export function addStringValidation( for (const attr of attributes) { switch (attr.name) { case '@length': { - const min = getArgValue(attr.args?.[0]?.value); + const min = getArgValue(attr.args?.find((a) => a.name === 'min')?.value); if (min !== undefined) { result = result.min(min); } - const max = getArgValue(attr.args?.[1]?.value); + const max = getArgValue(attr.args?.find((a) => a.name === 'max')?.value); if (max !== undefined) { result = result.max(max); } @@ -248,11 +248,11 @@ export function addListValidation( let result = schema; for (const attr of attributes) { if (attr.name === '@length') { - const min = getArgValue(attr.args?.[0]?.value); + const min = getArgValue(attr.args?.find((a) => a.name === 'min')?.value); if (min !== undefined) { result = result.min(min); } - const max = getArgValue(attr.args?.[1]?.value); + const max = getArgValue(attr.args?.find((a) => a.name === 'max')?.value); if (max !== undefined) { result = result.max(max); } diff --git a/tests/regression/test/issue-2433.test.ts b/tests/regression/test/issue-2433.test.ts new file mode 100644 index 000000000..ca0e93a2d --- /dev/null +++ b/tests/regression/test/issue-2433.test.ts @@ -0,0 +1,43 @@ +import { createTestClient } from '@zenstackhq/testtools'; +import { describe, expect, it } from 'vitest'; + +// https://github.com/zenstackhq/zenstack/issues/2433 +describe('Regression for issue 2433', () => { + it('should accept short string when only @length(max: N) is specified', async () => { + const db = await createTestClient( + ` +model Post { + id String @id @default(cuid()) + content String @length(max: 10000) +} + `, + ); + // Should succeed: short content is within max limit + await expect(db.post.create({ data: { content: 'hello' } })).resolves.toMatchObject({ + content: 'hello', + }); + // Should fail validation: content exceeds max + await expect( + db.post.create({ data: { content: 'x'.repeat(10001) } }), + ).toBeRejectedByValidation(); + }); + + it('should reject short string when only @length(min: N) is specified', async () => { + const db = await createTestClient( + ` +model Post { + id String @id @default(cuid()) + content String @length(min: 5) +} + `, + ); + // Should succeed: content meets min length + await expect(db.post.create({ data: { content: 'hello world' } })).resolves.toMatchObject({ + content: 'hello world', + }); + // Should fail validation: content is too short + await expect( + db.post.create({ data: { content: 'hi' } }), + ).toBeRejectedByValidation(); + }); +});