Skip to content

Commit 96e1526

Browse files
fix(zod): treat @uuid without a version as any UUID version
`addStringValidation` in packages/zod/src/utils.ts mapped the `@uuid` attribute with `if (version === 7) uuidv7() else uuidv4()`, so the no-version form fell into the v4 branch and rejected every other UUID version. stdlib.zmodel declares the version argument as optional and documents the attribute as "Validates a string field value is a valid UUID", and the `isUuid()` branch in the same file already passes an undefined version to `z.uuid()`, which accepts any version. Writing a v7 or v1 UUID into a `String @uuid` field therefore failed with `Validation error: Invalid UUID`. v7 is the shape ZenStack's own `uuid(7)` generator produces, at packages/orm/src/client/crud/operations/base.ts:1119. Only the no-version branch changes, to `result.uuid()`. `@uuid(4)` and `@uuid(7)` stay pinned, and the `@uuid` check in attribute-application-validator.ts already rejects any other version literal.
1 parent a4a25c4 commit 96e1526

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎packages/zod/src/utils.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ export function addStringValidation(
8282
}
8383
case '@uuid': {
8484
const version = getArgValue<number>(attr.args?.[0]?.value);
85-
if (version === 7) {
85+
if (version === 4) {
86+
result = result.uuidv4();
87+
} else if (version === 7) {
8688
result = result.uuidv7();
8789
} else {
88-
result = result.uuidv4();
90+
result = result.uuid();
8991
}
9092
break;
9193
}

‎tests/e2e/orm/validation/toplevel.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('Toplevel field validation tests', () => {
2020
str10 String? @time(-1)
2121
str11 String? @uuid
2222
str12 String? @uuid(7)
23+
str13 String? @uuid(4)
2324
}
2425
`,
2526
);
@@ -120,11 +121,27 @@ describe('Toplevel field validation tests', () => {
120121
// satisfies @uuid
121122
await expect(_t({ str11: '20ef31c8-a2c6-4dca-b87b-838e364ab4b3' })).toResolveTruthy();
122123

124+
// satisfies @uuid, which is not pinned to a version, with a v7 value
125+
await expect(_t({ str11: '019ff964-2f1d-7668-9a76-8648f2af9146' })).toResolveTruthy();
126+
123127
// violates @uuid(7)
124128
await expect(_t({ str12: 'not-a-uuid' })).toBeRejectedByValidation(['Invalid UUID']);
125129

130+
// violates @uuid(7) with a v4 value
131+
await expect(_t({ str12: '20ef31c8-a2c6-4dca-b87b-838e364ab4b3' })).toBeRejectedByValidation([
132+
'Invalid UUID',
133+
]);
134+
126135
// satisfies @uuid(7)
127136
await expect(_t({ str12: '019ff964-2f1d-7668-9a76-8648f2af9146' })).toResolveTruthy();
137+
138+
// violates @uuid(4) with a v7 value
139+
await expect(_t({ str13: '019ff964-2f1d-7668-9a76-8648f2af9146' })).toBeRejectedByValidation([
140+
'Invalid UUID',
141+
]);
142+
143+
// satisfies @uuid(4)
144+
await expect(_t({ str13: '20ef31c8-a2c6-4dca-b87b-838e364ab4b3' })).toResolveTruthy();
128145
}
129146
});
130147

0 commit comments

Comments
 (0)