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
8 changes: 4 additions & 4 deletions packages/zod/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export function addStringValidation(
for (const attr of attributes) {
switch (attr.name) {
case '@length': {
const min = getArgValue<number>(attr.args?.[0]?.value);
const min = getArgValue<number>(attr.args?.find((a) => a.name === 'min')?.value);
if (min !== undefined) {
result = result.min(min);
}
const max = getArgValue<number>(attr.args?.[1]?.value);
const max = getArgValue<number>(attr.args?.find((a) => a.name === 'max')?.value);
if (max !== undefined) {
result = result.max(max);
}
Expand Down Expand Up @@ -248,11 +248,11 @@ export function addListValidation(
let result = schema;
for (const attr of attributes) {
if (attr.name === '@length') {
const min = getArgValue<number>(attr.args?.[0]?.value);
const min = getArgValue<number>(attr.args?.find((a) => a.name === 'min')?.value);
if (min !== undefined) {
result = result.min(min);
}
const max = getArgValue<number>(attr.args?.[1]?.value);
const max = getArgValue<number>(attr.args?.find((a) => a.name === 'max')?.value);
if (max !== undefined) {
result = result.max(max);
}
Expand Down
43 changes: 43 additions & 0 deletions tests/regression/test/issue-2433.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Comment thread
ymc9 marked this conversation as resolved.
Resolved
});
Loading