ent/ent · error · ValidationError
{{ $pkg }}: validator failed for field "{{ $.Name }}.{{ $f.N
Error message
{{ $pkg }}: validator failed for field "{{ $.Name }}.{{ $f.Name }}": %w What it means
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.
Source
Thrown at entc/gen/template/builder/create.tmpl:123
{{- if $n }}
{{- $partially := ne $n (len $.Config.Storage.Dialects) }}
{{- if $partially }}
switch {{ $receiver }}.driver.Dialect() {
case {{ join $dialects ", " }}:
{{- end }}
if _, ok := {{ $mutation }}.{{ $f.MutationGet }}(); !ok {
return &ValidationError{Name: "{{ $f.Name }}", err: errors.New(`{{ $pkg }}: missing required field "{{ $.Name }}.{{ $f.Name }}"`)}
}
{{- if $partially }}
}
{{- end }}
{{- end }}
{{- end }}
{{- $isValidator := and ($f.HasGoType) ($f.Type.Validator) }}
{{- with or $f.Validators $f.IsEnum $isValidator }}
if v, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
if err := {{ if or $f.Validators $f.IsEnum }}{{ $.Package }}.{{ $f.Validator }}({{ $f.BasicType "v" }}){{ else }}v.Validate(){{ end }}; err != nil {
return &ValidationError{Name: "{{ $f.Name }}", err: fmt.Errorf(`{{ $pkg }}: validator failed for field "{{ $.Name }}.{{ $f.Name }}": %w`, err)}
}
}
{{- end }}
{{- end }}
{{- range $e := $.EdgesWithID }}
{{- if not $e.Optional }}
if len({{ $mutation }}.{{ $e.StructField }}IDs()) == 0 {
return &ValidationError{Name: "{{ $e.Name }}", err: errors.New(`{{ $pkg }}: missing required edge "{{ $.Name }}.{{ $e.Name }}"`)}
}
{{- end }}
{{- end }}
return nil
}
{{ with extend $ "Receiver" $receiver "Builder" $builder }}
{{ $tmpl := printf "dialect/%s/create" $.Storage }}
{{ xtemplate $tmpl . }}
{{ end }}View on GitHub (pinned to 69d5d4deb1)
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.
Example fix
// before
client.User.Create().SetEmail("not-an-email").Save(ctx)
// after
if err := client.User.Create().SetEmail("user@example.com").Save(ctx); err != nil {
var verr *ent.ValidationError
if errors.As(err, &verr) { /* handle field: verr.Name */ }
} Defensive patterns
Strategy: try-catch
Validate before calling
// Validate before Save
if err := builder.Validate(); err != nil {
var verr *ent.ValidationError
if errors.As(err, &verr) { /* inspect verr.Name */ }
} Type guard
func IsEntValidationError(err error) (field string, ok bool) {
var verr *ent.ValidationError
if errors.As(err, &verr) {
return verr.Name, true
}
return "", false
} Try / catch
if _, err := client.User.Create().SetAge(v).Save(ctx); err != nil {
var verr *ent.ValidationError
if errors.As(err, &verr) && verr.Name == "age" {
return fmt.Errorf("invalid age: %w", err)
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- {{ $pkg }}: missing required field "{{ $.Name }}.{{ $f.Name
- {{ $pkg }}: missing required edge "{{ $.Name }}.{{ $e.Name }
- field.String(%q).DefaultFunc expects func but got %s
- field.Bytes(%q).DefaultFunc expects func but got %s
- {{ $pkg }}: validator failed for field "{{ $.Name }}.{{ $f.N
AI-assisted analysis of ent/ent@69d5d4deb1 (2026-09-03).
Data as JSON: /api/errors/4176af5aaa802946.
Report an issue: GitHub.