{"record":{"id":"4176af5aaa802946","repo":"ent/ent","slug":"pkg-validator-failed-for-field-name","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/create.tmpl","lineNumber":123,"sourceCode":"\t\t\t{{- if $n }}\n\t\t\t\t{{- $partially := ne $n (len $.Config.Storage.Dialects) }}\n\t\t\t\t{{- if $partially }}\n\t\t\t\t\tswitch {{ $receiver }}.driver.Dialect() {\n\t\t\t\t\tcase {{ join $dialects \", \" }}:\n\t\t\t\t{{- end }}\n\t\t\t\t\tif _, ok := {{ $mutation }}.{{ $f.MutationGet }}(); !ok {\n\t\t\t\t\t\treturn &ValidationError{Name: \"{{ $f.Name }}\", err: errors.New(`{{ $pkg }}: missing required field \"{{ $.Name }}.{{ $f.Name }}\"`)}\n\t\t\t\t\t}\n\t\t\t\t{{- if $partially }}\n\t\t\t\t\t}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t\t{{- $isValidator := and ($f.HasGoType) ($f.Type.Validator) }}\n\t\t{{- with or $f.Validators $f.IsEnum $isValidator }}\n\t\t\tif v, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {\n\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\treturn &ValidationError{Name: \"{{ $f.Name }}\", err: fmt.Errorf(`{{ $pkg }}: validator failed for field \"{{ $.Name }}.{{ $f.Name }}\": %w`, err)}\n\t\t\t\t}\n\t\t\t}\n\t\t{{- end }}\n\t{{- end }}\n\t{{- range $e := $.EdgesWithID }}\n\t\t{{- if not $e.Optional }}\n\t\t\tif len({{ $mutation }}.{{ $e.StructField }}IDs()) == 0 {\n\t\t\t\treturn &ValidationError{Name: \"{{ $e.Name }}\", err: errors.New(`{{ $pkg }}: missing required edge \"{{ $.Name }}.{{ $e.Name }}\"`)}\n\t\t\t}\n\t\t{{- end }}\n\t{{- end }}\n\treturn nil\n}\n\n{{ with extend $ \"Receiver\" $receiver \"Builder\" $builder }}\n\t{{ $tmpl := printf \"dialect/%s/create\" $.Storage }}\n\t{{ xtemplate $tmpl . }}\n{{ end }}","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/ent/ent/blob/69d5d4deb19599f129166634e09d33addcf3f2cc/entc/gen/template/builder/create.tmpl#L105-L141","documentation":"This error is generated in the Create builder's validation step. Before saving, ent runs the field's validator functions, enum checks, or a Validate() method on custom Go types. If any fails, it wraps the validator's error in a ValidationError naming the schema type and field. It means the value being inserted violates the schema's field constraints.","triggerScenarios":"Calling client.User.Create().SetAge(200)...Save(ctx) where SetAge's value fails the field's validator (e.g. positive int check), violates an enum constraint, or a custom Go-type field's Validate() method returns an error.","commonSituations":"Inserting values outside enum lists; custom types with Validate() rejecting edge cases (empty strings, negative numbers); validators updated in the schema after data was already produced against older rules.","solutions":["Inspect the wrapped validator error to see which rule failed.","Adjust the value passed to the Set<Field> call so it passes the validator/enum.","If the validator is too strict, update the field validator in the schema and run ent generate.","Use mutation.Validate() or the builder's check before Save to catch it early."],"exampleFix":"// before\nclient.User.Create().SetEmail(\"not-an-email\").Save(ctx)\n// after\nif err := client.User.Create().SetEmail(\"user@example.com\").Save(ctx); err != nil {\n    var verr *ent.ValidationError\n    if errors.As(err, &verr) { /* handle field: verr.Name */ }\n}","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 IsEntValidationError(err error) (field string, ok bool) {\n    var verr *ent.ValidationError\n    if errors.As(err, &verr) {\n        return verr.Name, true\n    }\n    return \"\", false\n}","tryCatchPattern":"if _, err := client.User.Create().SetAge(v).Save(ctx); err != nil {\n    var verr *ent.ValidationError\n    if errors.As(err, &verr) && verr.Name == \"age\" {\n        return fmt.Errorf(\"invalid age: %w\", err)\n    }\n    return err\n}","preventionTips":["Run builder.Validate() before Save in request handlers","Keep field validators and enum lists in sync between schema and application code","Add unit tests covering validator edge cases"],"tags":["ent","validation","go"],"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"}