{"record":{"id":"64de11c516dcf89d","repo":"ent/ent","slug":"pkg-validator-failed-for-field-name-64de11","errorCode":null,"errorMessage":"{{ $pkg }}: validator failed for field \"{{ $.Name }}.{{ $f.Name }}\": %w","messagePattern":"(.+?)\\}: validator failed for field \"(.+?)\\}\\.(.+?)\\}\": %w","errorType":"validation","errorClass":"ValidationError","httpStatus":null,"severity":"error","filePath":"entc/gen/template/builder/update.tmpl","lineNumber":261,"sourceCode":"\t\t\t\t\t{{ $mutation }}.{{ $f.MutationSet }}(v)\n\t\t\t\t}\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t\t{{- if $runtimeRequired }}\n\t\t\treturn nil\n\t\t{{- end }}\n\t}\n{{ end }}\n\n{{ if $.HasUpdateCheckers }}\n\t// check runs all checks and user-defined validators on the builder.\n\tfunc ({{ $receiver }} *{{ $builder }}) check() error {\n\t\t{{- range $f := $.Fields }}\n\t\t\t{{- $isValidator := and ($f.HasGoType) ($f.Type.Validator) }}\n\t\t\t{{- with and (not $f.Immutable) (or $f.Validators $f.IsEnum $isValidator) }}\n\t\t\t\tif v, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {\n\t\t\t\t\tif err := {{ if or $f.Validators $f.IsEnum }}{{ $.Package }}.{{ $f.Validator }}({{ $f.BasicType \"v\" }}){{ else }}v.Validate(){{ end }}; err != nil {\n\t\t\t\t\t\treturn &ValidationError{Name: \"{{ $f.Name }}\", err: fmt.Errorf(`{{ $pkg }}: validator failed for field \"{{ $.Name }}.{{ $f.Name }}\": %w`, err)}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t\t{{- range $e := $.Edges }}\n\t\t\t{{- if and $e.Unique (not $e.Optional) }}\n\t\t\t\tif {{ $mutation }}.{{ $e.StructField }}Cleared() && len({{ $mutation }}.{{ $e.StructField }}IDs()) > 0 {\n\t\t\t\t\treturn errors.New(`{{ $pkg }}: clearing a required unique edge \"{{ $.Name }}.{{ $e.Name }}\"`)\n\t\t\t\t}\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t\treturn nil\n\t}\n{{ end }}\n\n{{ end }}\n","sourceCodeStart":243,"sourceCodeEnd":278,"githubUrl":"https://github.com/ent/ent/blob/69d5d4deb19599f129166634e09d33addcf3f2cc/entc/gen/template/builder/update.tmpl#L243-L278","documentation":"The generated Update builder's check() method runs validators for each mutated field — validator funcs, enum checks, or Validate() on custom Go types — skipping immutable fields. On failure it wraps the validator error in a ValidationError naming the type and field. It means a value passed to Set<Field> on an update violates the schema's constraints.","triggerScenarios":"Calling client.User.UpdateOneID(id).SetAge(-1)...Save(ctx) where the new value fails the field's validator, enum check, or custom type Validate(); updating a field whose validator rules were tightened after existing data was written.","commonSituations":"Enum mismatch after adding new allowed values to code but not the schema (or vice versa); custom Go-type fields whose Validate() rejects the update; validators disagreeing between create and update paths.","solutions":["Read the wrapped error to identify the failing rule for the named field.","Fix the value passed to Set<Field> so it satisfies the validator/enum.","Call builder.Validate()/check logic before Save to fail early.","Relax or correct the validator in the schema and regenerate if the rule itself is wrong."],"exampleFix":"// before\nerr := client.User.UpdateOneID(id).SetRole(\"superadmin\").Exec(ctx)\n// after\nrole := \"superadmin\"\nif !slices.Contains(user.ValidRoles, role) { /* handle before save */ }\nerr := client.User.UpdateOneID(id).SetRole(role).Exec(ctx)","handlingStrategy":"try-catch","validationCode":"// Validate before Save\nif err := builder.Validate(); err != nil {\n    var verr *ent.ValidationError\n    if errors.As(err, &verr) { /* inspect verr.Name */ }\n}","typeGuard":"func FieldValidationError(err error) (field string, verr *ent.ValidationError, ok bool) {\n    if errors.As(err, &verr) {\n        return verr.Name, verr, true\n    }\n    return \"\", nil, false\n}","tryCatchPattern":"if err := client.User.UpdateOneID(id).SetAge(v).Exec(ctx); err != nil {\n    var verr *ent.ValidationError\n    if errors.As(err, &verr) {\n        return fmt.Errorf(\"field %s invalid on update: %w\", verr.Name, verr)\n    }\n    return err\n}","preventionTips":["Validate update payloads against the same rules as create payloads","Keep enum lists and validators synchronized across schema versions","Test updates with boundary values"],"tags":["ent","validation","go","update"],"backgroundTag":"field-validator-failed","analyzedSha":"69d5d4deb19599f129166634e09d33addcf3f2cc","analyzedAt":"2026-09-03T16:51:39.799Z","contentChangedAt":"2026-09-03T16:51:39.799Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}