validate

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package validate provides request-level input validation for the platform. It wraps the go-playground/validator library with custom validation tags and human-readable error formatting that maps directly to the API's error response contract (apierror.APIError with a Param field).

Seven custom validator tags are registered at init time:

  • "password": 8–72 characters, at least one lowercase letter, one uppercase letter, one digit, and one special character.
  • "username": 3–255 characters, alphanumeric (upper and lower), underscores, and hyphens only ([a-zA-Z0-9_-]).
  • "identifier": accepts either a valid email address or a username (3–50 characters, alphanumeric, underscores, and hyphens).
  • "custom_email": stricter email validation than the built-in "email" tag, enforcing RFC length limits, TLD format, and no consecutive dots.
  • "nonzero_decimal": the field, parsed as a decimal string, must not equal zero.
  • "max_days_ahead=N": a time.Time (or field.Optional[time.Time]) no more than N days in the future. Past/zero values pass.
  • "multiple_of=N": a numeric field (or field.Optional[float64]) that is a whole multiple of N (e.g. multiple_of=0.1). Zero/unset values pass.

All custom tags treat empty/zero values as valid — combine with "required" when the field must be present.

The package also provides a lightweight Validator helper for imperative checks that can't be expressed with struct tags (e.g. cross-field constraints).

Index

Constants

View Source
const PasswordMaxLength int = 72

validatePassword implements the "password" struct tag. A valid password is 8–72 bytes long and contains at least one lowercase letter, one uppercase letter, one ASCII digit, and one special character (from the hasSpecialChar set). Empty strings pass (combine with "required" to enforce presence). The 72-byte upper bound matches bcrypt's maximum input length.

Variables

This section is empty.

Functions

func ApplySlicePresenceFlags

func ApplySlicePresenceFlags(body []byte, v any)

ApplySlicePresenceFlags sets boolean "Has" companion fields to true when the corresponding slice field's JSON key is present in the raw body. This lets downstream code distinguish "field absent" (Has=false) from "field explicitly sent" (Has=true), including empty arrays to clear the collection.

Convention: a slice field `FooIDs []string` with json tag "foo_ids" has a companion `HasFooIDs bool` with json:"-". When "foo_ids" appears in the JSON body, HasFooIDs is set to true.

func RejectEmptyPatchBody

func RejectEmptyPatchBody(body []byte, v any) *apierror.APIError

RejectEmptyPatchBody returns a validation error when the JSON body does not contain at least one field that maps to a body-bound struct field. Fields bound from non-body sources (path, query, header tags) are excluded.

This prevents PATCH requests with empty bodies ({}) from silently succeeding as no-op updates.

func RejectExplicitJSONNulls

func RejectExplicitJSONNulls(body []byte, v any) *apierror.APIError

RejectExplicitJSONNulls returns an invalid_format API error when the JSON body contains an explicit null or a blank string for optional pointer fields (json omitempty) or a blank string for field.Optional fields. Absent keys are allowed (PATCH semantics).

field.Clearable values accept null (clear) and are not checked here. field.Optional values reject explicit null at unmarshal time; this pass only rejects a present-but-blank string for them. Response-style pointers without omitempty are not checked here.

func Validate

func Validate(v any) *apierror.APIError

Validate runs all struct-tag validations on v and returns a user-facing apierror.APIError on failure (nil on success). When a single field fails, the error's Param is set to that field's JSON/form/query name so the client can highlight the offending input. When multiple fields fail, the error message lists all violations and Param is set to the first failing field.

Types

type Validator

type Validator struct {
	// Errors maps field names to their first error message. Only the first error per field is stored to keep messages concise.
	Errors map[string]string
}

Validator is a lightweight imperative validation helper for checks that cannot be expressed with struct tags (e.g. cross-field constraints, conditional logic). It collects named errors via AddError or Check and reports validity via Valid.

v := validate.New()
v.Check(req.EndDate.After(req.StartDate), "ends_at", "must be after starts_at")
if !v.Valid() { ... }

Jump to

Keyboard shortcuts

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