Integer Rules
Learn how to validate integer fields in NLG Form using minimum, maximum, range, equality, positivity, negativity, and custom validation rules.
Integer rules validate typed integer fields defined with form.Int.
They are useful for validating ages, quantities, counters, limits, identifiers, priorities, inventory values, pagination parameters, and other integer-based application data.
Common Use Cases
Integer rules are commonly used for:
- user age validation
- pagination limits
- inventory quantities
- order counts
- retry limits
- API rate limits
- priorities and weights
- billing quantities
- numeric identifiers
- integer configuration values
TL;DR
| Rule | Description |
|---|---|
rules.Min(field, n) | Requires the integer value to be greater than or equal to n |
rules.MinWithCode(field, n, code) | Same as Min with a custom error code |
rules.Max(field, n) | Requires the integer value to be less than or equal to n |
rules.MaxWithCode(field, n, code) | Same as Max with a custom error code |
rules.Between(field, min, max) | Requires the integer value to stay within the specified range |
rules.BetweenWithCode(field, min, max, code) | Same as Between with a custom error code |
rules.IntEquals(field, expected) | Requires the integer value to exactly match the expected value |
rules.IntEqualsWithCode(field, expected, code) | Same as IntEquals with a custom error code |
rules.Positive(field) | Requires the integer value to be greater than zero |
rules.PositiveWithCode(field, code) | Same as Positive with a custom error code |
rules.Negative(field) | Requires the integer value to be less than zero |
rules.NegativeWithCode(field, code) | Same as Negative with a custom error code |
rules.IsInt(field) | Validates that the field contains an integer value |
Defining Integer Fields
Integer validation starts by defining typed integer fields.
IntForm := struct {
Age form.IntField[IntRulesRequest]
Quantity form.IntField[IntRulesRequest]
Priority form.IntField[IntRulesRequest]
}{
Age: form.Int[IntRulesRequest]("age", func(r *IntRulesRequest) int {
return r.Age
}),
Quantity: form.Int[IntRulesRequest]("quantity", func(r *IntRulesRequest) int {
return r.Quantity
}),
Priority: form.Int[IntRulesRequest]("priority", func(r *IntRulesRequest) int {
return r.Priority
}),
}Each field contains:
- the validation response field name
- typed accessors
- reusable schema references
- strongly typed integer access
Applying Integer Rules
Integer rules are attached directly to typed field references.
return form.Schema[IntRulesRequest]{
rules.Min(IntForm.Age, 18),
rules.Max(IntForm.Quantity, 100),
rules.Positive(IntForm.Priority),
}This keeps validation logic:
- explicit
- composable
- reusable
- transport-independent
- type-safe
Rule Examples
Min
Requires the integer value to be greater than or equal to the provided minimum.
rules.Min(IntForm.Age, 18)Typical use cases:
- minimum age requirements
- minimum quantities
- pagination constraints
- API limits
MinWithCode
Requires the value to be greater than or equal to the minimum and returns a custom validation code.
const CodeTooYoung = form.Code("too_young")
rules.MinWithCode(
IntForm.Age,
18,
CodeTooYoung,
)Useful for frontend-friendly API validation contracts.
Max
Requires the integer value to be less than or equal to the provided maximum.
rules.Max(IntForm.Quantity, 100)Typical use cases:
- quantity limits
- page size restrictions
- retry caps
- maximum priorities
MaxWithCode
Requires the value to be below the maximum and returns a custom validation code.
const CodeTooLarge = form.Code("too_large")
rules.MaxWithCode(
IntForm.Quantity,
100,
CodeTooLarge,
)Between
Requires the integer value to stay within a specified range.
rules.Between(IntForm.Priority, 1, 10)Typical use cases:
- rating systems
- bounded priorities
- normalized integer values
- application configuration limits
BetweenWithCode
Requires the value to remain within the specified range and returns a custom validation code.
const CodeOutOfRange = form.Code("out_of_range")
rules.BetweenWithCode(
IntForm.Priority,
1,
10,
CodeOutOfRange,
)IntEquals
Requires the integer value to exactly match the expected value.
rules.IntEquals(IntForm.Quantity, 5)Useful for fixed numeric requirements and strict API contracts.
IntEqualsWithCode
Requires the value to exactly match the expected integer value and returns a custom validation code.
const CodeInvalidQuantity = form.Code("invalid_quantity")
rules.IntEqualsWithCode(
IntForm.Quantity,
5,
CodeInvalidQuantity,
)Positive
Requires the integer value to be greater than zero.
rules.Positive(IntForm.Quantity)Typical use cases:
- order quantities
- positive identifiers
- retry counts
- payment units
PositiveWithCode
Requires the integer value to be positive and returns a custom validation code.
const CodeMustBePositive = form.Code("must_be_positive")
rules.PositiveWithCode(
IntForm.Quantity,
CodeMustBePositive,
)Negative
Requires the integer value to be less than zero.
rules.Negative(IntForm.Priority)Typical use cases:
- negative offsets
- reverse scoring systems
- signed internal values
NegativeWithCode
Requires the integer value to be negative and returns a custom validation code.
const CodeMustBeNegative = form.Code("must_be_negative")
rules.NegativeWithCode(
IntForm.Priority,
CodeMustBeNegative,
)IsInt
Checks whether the field contains a valid integer value.
rules.IsInt(IntForm.Quantity)For typed Go structures this rule is mainly useful for API symmetry because invalid JSON integer types are typically rejected during request decoding before schema validation begins.
Complete Example
package main
import (
"github.com/netlifeguru/form"
"github.com/netlifeguru/form/rules"
)
type IntRulesRequest struct {
Age int `json:"age"`
Quantity int `json:"quantity"`
Priority int `json:"priority"`
}
func IntRulesSchema() form.Schema[IntRulesRequest] {
IntForm := struct {
Age form.IntField[IntRulesRequest]
Quantity form.IntField[IntRulesRequest]
Priority form.IntField[IntRulesRequest]
}{
Age: form.Int[IntRulesRequest]("age", func(r *IntRulesRequest) int {
return r.Age
}),
Quantity: form.Int[IntRulesRequest]("quantity", func(r *IntRulesRequest) int {
return r.Quantity
}),
Priority: form.Int[IntRulesRequest]("priority", func(r *IntRulesRequest) int {
return r.Priority
}),
}
return form.Schema[IntRulesRequest]{
rules.Min(IntForm.Age, 18),
rules.Max(IntForm.Quantity, 100),
rules.Between(IntForm.Priority, 1, 10),
}
}Notes
- Integer rules operate on typed Go
intvalues. - Invalid JSON numeric types are rejected before validation begins.
- Use
Betweenwhen values must stay within bounded ranges. - Use
PositiveandNegativefor intent-based validation readability. - Custom error codes help stabilize frontend-facing validation contracts.
- Validation schemas remain reusable across APIs, CLI tools, background workers, and internal services.
- Integer validation remains explicit and independent from transport layers.
Float64 Rules
Learn how to validate float64 fields in NLG Form using minimum, maximum, range, equality, and custom validation rules for decimal values.
String Rules
Learn how to validate string fields in NLG Form using required checks, length validation, prefixes, suffixes, substrings, equality rules, and custom validation codes.