Skip to main content

Built-in rules

Built-in rules are available through validate and validateElem tags without registration.

RuleField typeExampleValid when
min(N)Stringvalidate:"min(3)"Byte length is at least N.
max(N)Stringvalidate:"max(50)"Byte length is at most N.
oneof(v1,v2,...)Stringvalidate:"oneof(local,remote)"Value exactly matches one listed string.
emailStringvalidate:"email"Value passes a lightweight email shape check.
uuidStringvalidate:"uuid"Value has canonical 8-4-4-4-12 hexadecimal form.
semverStringvalidate:"semver"Value is a semantic version such as 1.2.3 or 1.2.3-beta.1.
min(V)Numericvalidate:"min(1)"Value is at least V.
max(V)Numericvalidate:"max(65535)"Value is at most V.
nonzeroNumericvalidate:"nonzero"Value is not zero.
oneof(v1,v2,...)Numericvalidate:"oneof(1,2,3)"Value equals one listed number.
min(duration)Durationvalidate:"min(1s)"Duration is at least the parsed duration.
max(duration)Durationvalidate:"max(30s)"Duration is at most the parsed duration.

String includes string and named string types. String length rules count bytes; min(N) with N < 1 and max(N) with N < 0 are no-ops. The email rule checks basic shape, not full RFC compliance.

Numeric includes int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, and float64. Named types whose underlying type is int use the int rules. Numeric parameters use decimal notation.

Duration means time.Duration or github.com/ygrebnov/model/pkg/types.Duration; parameters use Go duration syntax such as 500ms or 2h. Pointer fields use the corresponding built-in rule; a nil pointer is evaluated as the zero value unless omitempty skips it.

A missing or invalid parameter produces a validation issue. To override a built-in for one exact field type, register a custom rule with the same name through model.WithRules.