Float64 Rules
Learn how to validate float64 fields in NLG Form using minimum, maximum, range, equality, and custom validation rules for decimal values.
Float64 rules validate typed decimal number fields defined with form.Float64.
They are useful for validating prices, percentages, ratings, measurements, coordinates, billing values, financial calculations, limits, and other decimal-based application data.
Common Use Cases
Float64 rules are commonly used for:
- product pricing
- percentages and ratios
- latitude and longitude coordinates
- invoice totals
- payment amounts
- tax calculations
- rating systems
- decimal-based API values
- financial validation
- measurement systems
TL;DR
| Rule | Description |
|---|---|
rules.MinFloat64(field, n) | Requires the value to be greater than or equal to n |
rules.MinFloat64WithCode(field, n, code) | Same as MinFloat64 with a custom error code |
rules.MaxFloat64(field, n) | Requires the value to be less than or equal to n |
rules.MaxFloat64WithCode(field, n, code) | Same as MaxFloat64 with a custom error code |
rules.BetweenFloat64(field, min, max) | Requires the value to be within the specified range |
rules.BetweenFloat64WithCode(field, min, max, code) | Same as BetweenFloat64 with a custom error code |
rules.Float64Equals(field, expected) | Requires the value to match the expected float64 |
rules.Float64EqualsWithCode(field, expected, code) | Same as Float64Equals with a custom error code |
rules.IsFloat64(field) | Validates that the field contains a float64 value |
Defining Float64 Fields
Float64 validation starts by defining typed float64 fields.
FloatForm := struct {
Price form.Float64Field[FloatRulesRequest]
Discount form.Float64Field[FloatRulesRequest]
Temperature form.Float64Field[FloatRulesRequest]
}{
Price: form.Float64[FloatRulesRequest]("price", func(r *FloatRulesRequest) float64 {
return r.Price
}),
Discount: form.Float64[FloatRulesRequest]("discount", func(r *FloatRulesRequest) float64 {
return r.Discount
}),
Temperature: form.Float64[FloatRulesRequest]("temperature", func(r *FloatRulesRequest) float64 {
return r.Temperature
}),
}Each field contains:
- the validation response field name
- typed field accessors
- reusable schema references
- strongly typed value access
Applying Float64 Rules
Float64 rules are attached directly to typed field references.
return form.Schema[FloatRulesRequest]{
rules.MinFloat64(FloatForm.Price, 0),
rules.MaxFloat64(FloatForm.Discount, 100),
rules.BetweenFloat64(FloatForm.Temperature, -50, 100),
}This keeps validation logic:
- explicit
- reusable
- type-safe
- composable
- independent from transport layers
Rule Examples
MinFloat64
Requires the float64 value to be greater than or equal to the provided minimum.
rules.MinFloat64(FloatForm.Price, 0)Typical use cases:
- positive prices
- non-negative totals
- minimum percentages
- valid measurements
MinFloat64WithCode
Requires the value to be greater than or equal to the minimum and returns a custom validation code.
const CodePriceTooLow = form.Code("price_too_low")
rules.MinFloat64WithCode(
FloatForm.Price,
0,
CodePriceTooLow,
)Useful for frontend-friendly API error contracts.
MaxFloat64
Requires the float64 value to be less than or equal to the provided maximum.
rules.MaxFloat64(FloatForm.Discount, 100)Typical use cases:
- percentage caps
- maximum limits
- rating boundaries
- financial constraints
MaxFloat64WithCode
Requires the value to be below the provided maximum and returns a custom validation code.
const CodeDiscountTooHigh = form.Code("discount_too_high")
rules.MaxFloat64WithCode(
FloatForm.Discount,
100,
CodeDiscountTooHigh,
)BetweenFloat64
Requires the float64 value to stay within a specific range.
rules.BetweenFloat64(FloatForm.Temperature, -50, 100)Typical use cases:
- geographic coordinates
- normalized scores
- percentage ranges
- sensor measurements
- temperature limits
BetweenFloat64WithCode
Requires the value to remain within the specified range and returns a custom validation code.
const CodeOutOfRange = form.Code("out_of_range")
rules.BetweenFloat64WithCode(
FloatForm.Temperature,
-50,
100,
CodeOutOfRange,
)Float64Equals
Requires the value to exactly match the expected float64 value.
rules.Float64Equals(FloatForm.Rating, 5.0)Useful when APIs require fixed numeric values.
Float64EqualsWithCode
Requires the value to exactly match the expected value and returns a custom validation code.
const CodeInvalidValue = form.Code("invalid_value")
rules.Float64EqualsWithCode(
FloatForm.Rating,
5.0,
CodeInvalidValue,
)IsFloat64
Checks whether the field contains a valid float64 value.
rules.IsFloat64(FloatForm.Price)For typed Go structures this rule is mostly useful for API symmetry because invalid JSON numeric types are usually rejected during request decoding before schema validation begins.
Complete Example
package main
import (
"github.com/netlifeguru/form"
"github.com/netlifeguru/form/rules"
)
type FloatRulesRequest struct {
Price float64 `json:"price"`
Discount float64 `json:"discount"`
Temperature float64 `json:"temperature"`
}
func FloatRulesSchema() form.Schema[FloatRulesRequest] {
FloatForm := struct {
Price form.Float64Field[FloatRulesRequest]
Discount form.Float64Field[FloatRulesRequest]
Temperature form.Float64Field[FloatRulesRequest]
}{
Price: form.Float64[FloatRulesRequest]("price", func(r *FloatRulesRequest) float64 {
return r.Price
}),
Discount: form.Float64[FloatRulesRequest]("discount", func(r *FloatRulesRequest) float64 {
return r.Discount
}),
Temperature: form.Float64[FloatRulesRequest]("temperature", func(r *FloatRulesRequest) float64 {
return r.Temperature
}),
}
return form.Schema[FloatRulesRequest]{
rules.MinFloat64(FloatForm.Price, 0),
rules.MaxFloat64(FloatForm.Discount, 100),
rules.BetweenFloat64(FloatForm.Temperature, -50, 100),
}
}Notes
- Float64 rules operate on typed Go
float64values. - Invalid JSON numeric types are rejected before validation begins.
- Use
BetweenFloat64when validating normalized ranges such as percentages or coordinates. - Use custom error codes for stable frontend-facing API contracts.
- Decimal validation logic remains transport-independent and reusable across services, HTTP handlers, CLI tools, and background workers.
- Float comparisons are exact and do not apply epsilon-based precision matching automatically.