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

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.

Source

Thrown at entc/gen/template/builder/update.tmpl:261

					{{ $mutation }}.{{ $f.MutationSet }}(v)
				}
			{{- end }}
		{{- end }}
		{{- if $runtimeRequired }}
			return nil
		{{- end }}
	}
{{ end }}

{{ if $.HasUpdateCheckers }}
	// check runs all checks and user-defined validators on the builder.
	func ({{ $receiver }} *{{ $builder }}) check() error {
		{{- range $f := $.Fields }}
			{{- $isValidator := and ($f.HasGoType) ($f.Type.Validator) }}
			{{- with and (not $f.Immutable) (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 := $.Edges }}
			{{- if and $e.Unique (not $e.Optional) }}
				if {{ $mutation }}.{{ $e.StructField }}Cleared() && len({{ $mutation }}.{{ $e.StructField }}IDs()) > 0 {
					return errors.New(`{{ $pkg }}: clearing a required unique edge "{{ $.Name }}.{{ $e.Name }}"`)
				}
			{{- end }}
		{{- end }}
		return nil
	}
{{ end }}

{{ end }}

View on GitHub (pinned to 69d5d4deb1)

Solutions

  1. Read the wrapped error to identify the failing rule for the named field.
  2. Fix the value passed to Set<Field> so it satisfies the validator/enum.
  3. Call builder.Validate()/check logic before Save to fail early.
  4. Relax or correct the validator in the schema and regenerate if the rule itself is wrong.

Example fix

// before
err := client.User.UpdateOneID(id).SetRole("superadmin").Exec(ctx)
// after
role := "superadmin"
if !slices.Contains(user.ValidRoles, role) { /* handle before save */ }
err := client.User.UpdateOneID(id).SetRole(role).Exec(ctx)
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 FieldValidationError(err error) (field string, verr *ent.ValidationError, ok bool) {
    if errors.As(err, &verr) {
        return verr.Name, verr, true
    }
    return "", nil, false
}

Try / catch

if err := client.User.UpdateOneID(id).SetAge(v).Exec(ctx); err != nil {
    var verr *ent.ValidationError
    if errors.As(err, &verr) {
        return fmt.Errorf("field %s invalid on update: %w", verr.Name, verr)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of ent/ent@69d5d4deb1 (2026-09-03). Data as JSON: /api/errors/64de11c516dcf89d. Report an issue: GitHub.