Documentation
¶
Overview ¶
Package validator provides lightweight, fluent input validation for express handlers, in the spirit of Node's express-validator. Build a Schema of field rules and either validate a map directly or mount it as middleware that rejects invalid requests with a 400 JSON response.
schema := validator.Schema{
validator.Field("email").Required().Email(),
validator.Field("age").Optional().IsInt().Min(0).Max(120),
validator.Field("name").Required().MinLen(2).MaxLen(50),
}
app.Post("/users", schema.Body(), createUser)
Index ¶
- type Errors
- type FieldError
- type FieldRules
- func (f *FieldRules) Custom(fn func(value any) string) *FieldRules
- func (f *FieldRules) Email() *FieldRules
- func (f *FieldRules) In(allowed ...string) *FieldRules
- func (f *FieldRules) IsInt() *FieldRules
- func (f *FieldRules) IsNumber() *FieldRules
- func (f *FieldRules) Matches(pattern string) *FieldRules
- func (f *FieldRules) Max(n float64) *FieldRules
- func (f *FieldRules) MaxLen(n int) *FieldRules
- func (f *FieldRules) Min(n float64) *FieldRules
- func (f *FieldRules) MinLen(n int) *FieldRules
- func (f *FieldRules) Optional() *FieldRules
- func (f *FieldRules) Required() *FieldRules
- type Schema
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FieldError ¶
FieldError is a single validation failure.
type FieldRules ¶
type FieldRules struct {
// contains filtered or unexported fields
}
FieldRules accumulates the rules applied to a single named field.
func (*FieldRules) Custom ¶
func (f *FieldRules) Custom(fn func(value any) string) *FieldRules
Custom applies a user-supplied validation function. Returning a non-empty string records that message as an error.
func (*FieldRules) Email ¶
func (f *FieldRules) Email() *FieldRules
Email validates that the value is a syntactically valid email address.
func (*FieldRules) In ¶
func (f *FieldRules) In(allowed ...string) *FieldRules
In requires the value to be one of the allowed strings.
func (*FieldRules) IsInt ¶
func (f *FieldRules) IsInt() *FieldRules
IsInt requires the value to be an integer.
func (*FieldRules) IsNumber ¶
func (f *FieldRules) IsNumber() *FieldRules
IsNumber requires the value to be numeric.
func (*FieldRules) Matches ¶
func (f *FieldRules) Matches(pattern string) *FieldRules
Matches requires the string value to match the given regular expression.
func (*FieldRules) Max ¶
func (f *FieldRules) Max(n float64) *FieldRules
Max requires a numeric value <= n.
func (*FieldRules) MaxLen ¶
func (f *FieldRules) MaxLen(n int) *FieldRules
MaxLen requires the string value to be at most n characters.
func (*FieldRules) Min ¶
func (f *FieldRules) Min(n float64) *FieldRules
Min requires a numeric value >= n.
func (*FieldRules) MinLen ¶
func (f *FieldRules) MinLen(n int) *FieldRules
MinLen requires the string value to be at least n characters.
func (*FieldRules) Optional ¶
func (f *FieldRules) Optional() *FieldRules
Optional marks the field as optional: when absent, remaining rules are skipped instead of failing.
func (*FieldRules) Required ¶
func (f *FieldRules) Required() *FieldRules
Required fails when the field is missing or an empty string.
type Schema ¶
type Schema []*FieldRules
Schema is an ordered set of field rules.
func (Schema) Body ¶
Body returns express middleware that validates the parsed request body. It expects a preceding body-parser (express.JSON or express.URLEncoded). On failure it responds 400 with {"errors": [...]}; on success it calls next.