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
6 changes: 4 additions & 2 deletions packages/zod/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ export function addStringValidation(
}
case '@uuid': {
const version = getArgValue<number>(attr.args?.[0]?.value);
if (version === 7) {
if (version === 4) {
result = result.uuidv4();
} else if (version === 7) {
result = result.uuidv7();
} else {
result = result.uuidv4();
result = result.uuid();
}
break;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/e2e/orm/validation/toplevel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('Toplevel field validation tests', () => {
str10 String? @time(-1)
str11 String? @uuid
str12 String? @uuid(7)
str13 String? @uuid(4)
}
`,
);
Expand Down Expand Up @@ -120,11 +121,27 @@ describe('Toplevel field validation tests', () => {
// satisfies @uuid
await expect(_t({ str11: '20ef31c8-a2c6-4dca-b87b-838e364ab4b3' })).toResolveTruthy();

// satisfies @uuid, which is not pinned to a version, with a v7 value
await expect(_t({ str11: '019ff964-2f1d-7668-9a76-8648f2af9146' })).toResolveTruthy();

// violates @uuid(7)
await expect(_t({ str12: 'not-a-uuid' })).toBeRejectedByValidation(['Invalid UUID']);

// violates @uuid(7) with a v4 value
await expect(_t({ str12: '20ef31c8-a2c6-4dca-b87b-838e364ab4b3' })).toBeRejectedByValidation([
'Invalid UUID',
]);

// satisfies @uuid(7)
await expect(_t({ str12: '019ff964-2f1d-7668-9a76-8648f2af9146' })).toResolveTruthy();

// violates @uuid(4) with a v7 value
await expect(_t({ str13: '019ff964-2f1d-7668-9a76-8648f2af9146' })).toBeRejectedByValidation([
'Invalid UUID',
]);

// satisfies @uuid(4)
await expect(_t({ str13: '20ef31c8-a2c6-4dca-b87b-838e364ab4b3' })).toResolveTruthy();
}
});

Expand Down
Loading