validation

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 8 Imported by: 0

README

validation

Fluent validation builder and struct tag validation with chainable rules and AppError integration.

Install

go get github.com/kbukum/gokit

Quick Start

package main

import (
    "fmt"
    "github.com/kbukum/gokit/validation"
)

func main() {
    // Fluent validation
    err := validation.New().
        Required("name", "").
        MaxLength("email", "user@example.com", 255).
        OneOf("role", "editor", []string{"admin", "editor", "viewer"}).
        Validate()
    if err != nil {
        fmt.Println(err) // returns *errors.AppError
    }

    // Struct tag validation
    type User struct {
        Name  string `validate:"required"`
        Email string `validate:"required,email"`
    }
    if err := validation.Validate(User{}); err != nil {
        fmt.Println(err)
    }
}

Key Types & Functions

Name Description
Validator Fluent validation builder collecting field errors
FieldError Individual field validation error
New() Create a new Validator
Required() / RequiredUUID() / OptionalUUID() Presence checks
MinLength() / MaxLength() / Range() / Min() / Max() Size/range rules
Pattern() / OneOf() / Custom() Pattern, enum, and custom rules
Validate(s any) Struct tag validation using validate tags
ValidateUUID() Parse and validate UUID string

⬅ Back to main README

Documentation

Overview

Package validation provides input validation utilities for gokit handlers.

It supports both struct tag validation (using the validator library) and programmatic validation with error collection. Struct tag validation is recommended for command/query handlers.

Struct Tag Validation

type CreateUserCmd struct {
    Name  string `validate:"required,min=2"`
    Email string `validate:"required,email"`
}
err := validation.ValidateStruct(cmd)

Programmatic Validation

v := validation.New()
v.Check(name != "", "name", "name is required")
err := v.Error()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Required

func Required(field, value string) error

Required validates a single required field and returns an error if empty.

func Validate

func Validate(s any) error

Validate validates a struct using struct tags. Uses tags like `validate:"required,email,max=255"`.

func ValidateUUID

func ValidateUUID(field, value string) (uuid.UUID, error)

ValidateUUID validates and parses a UUID string.

Types

type FieldError

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

FieldError represents a validation error for a specific field.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator collects validation errors.

func New

func New() *Validator

New creates a new Validator.

func (*Validator) AddError

func (v *Validator) AddError(field, message string)

AddError adds a field error.

func (*Validator) Custom

func (v *Validator) Custom(condition bool, field, message string) *Validator

Custom applies a custom validation condition.

func (*Validator) Errors

func (v *Validator) Errors() []FieldError

Errors returns all validation errors.

func (*Validator) HasErrors

func (v *Validator) HasErrors() bool

HasErrors returns true if there are validation errors.

func (*Validator) Max

func (v *Validator) Max(field string, value, maxVal int) *Validator

Max checks if a number is within max value.

func (*Validator) MaxLength

func (v *Validator) MaxLength(field, value string, maxLen int) *Validator

MaxLength checks if a string is within max length.

func (*Validator) Min

func (v *Validator) Min(field string, value, minVal int) *Validator

Min checks if a number meets minimum value.

func (*Validator) MinLength

func (v *Validator) MinLength(field, value string, minLen int) *Validator

MinLength checks if a string meets minimum length.

func (*Validator) OneOf

func (v *Validator) OneOf(field, value string, allowed []string) *Validator

OneOf checks if a value is one of the allowed values.

func (*Validator) OptionalUUID

func (v *Validator) OptionalUUID(field, value string) *Validator

OptionalUUID checks if a non-empty string is a valid UUID.

func (*Validator) Pattern

func (v *Validator) Pattern(field, value, pattern string) *Validator

Pattern checks if a string matches a regex pattern.

func (*Validator) Range

func (v *Validator) Range(field string, value, minVal, maxVal int) *Validator

Range checks if a number is within a range.

func (*Validator) Required

func (v *Validator) Required(field, value string) *Validator

Required checks if a string is non-empty.

func (*Validator) RequiredUUID

func (v *Validator) RequiredUUID(field, value string) *Validator

RequiredUUID checks if a string is a valid non-nil UUID.

func (*Validator) Validate

func (v *Validator) Validate() *errors.AppError

Validate returns an AppError if there are validation errors, nil otherwise.

Jump to

Keyboard shortcuts

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