validator

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

type Errors []FieldError

Errors is a collection of validation failures.

func (Errors) Error

func (e Errors) Error() string

Error implements the error interface, summarizing the failures.

type FieldError

type FieldError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

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 Field

func Field(name string) *FieldRules

Field starts a rule chain for the 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

func (s Schema) Body() express.Handler

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.

func (Schema) Query

func (s Schema) Query() express.Handler

Query returns express middleware that validates the request's query string.

func (Schema) Validate

func (s Schema) Validate(data map[string]any) Errors

Validate runs the schema against a data map, returning all failures (nil when valid).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL