diff --git a/.changeset/fix-sys-user-unique-validation.md b/.changeset/fix-sys-user-unique-validation.md new file mode 100644 index 000000000..610b7ce0b --- /dev/null +++ b/.changeset/fix-sys-user-unique-validation.md @@ -0,0 +1,17 @@ +--- +'@objectstack/platform-objects': patch +--- + +Fix `sys_user` load failure after the validation-rule type trim (#1485) + +#1485 trimmed the unenforceable validation-rule types (`unique`, `async`, +`custom`) from the `ValidationRuleSchema` discriminated union, but `sys_user` +still declared an `email_unique` rule with `type: 'unique'`. Loading the object +then threw a `ZodError` ("Invalid discriminator value … at validations[0].type"), +failing `platform-objects.test.ts` and turning `main` red. + +The rule was redundant: `sys_user` already declares a unique index on `email` +(`indexes: [{ fields: ['email'], unique: true }]`), and the user table is +managed by better-auth which enforces email uniqueness at the source. Removed +the unenforceable validation rule; uniqueness remains enforced by the index. +No other object uses a trimmed validation type. diff --git a/packages/platform-objects/src/identity/sys-user.object.ts b/packages/platform-objects/src/identity/sys-user.object.ts index 380318876..1cdb327f3 100644 --- a/packages/platform-objects/src/identity/sys-user.object.ts +++ b/packages/platform-objects/src/identity/sys-user.object.ts @@ -439,14 +439,8 @@ export const SysUser = ObjectSchema.create({ mru: true, }, - validations: [ - { - name: 'email_unique', - type: 'unique', - severity: 'error', - message: 'Email must be unique', - fields: ['email'], - caseSensitive: false, - }, - ], + // Email uniqueness is enforced by the unique index above (and better-auth's + // managed user table). A declarative `unique` validation rule is intentionally + // not used — uniqueness needs a DB lookup, not a synchronous validation, so it + // is not one of the declarable validation-rule types. });