validator

package module
v0.0.0-...-1a4fc35 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2025 License: MIT Imports: 4 Imported by: 0

README

Go Reference Report card

validator

A simple validation utility for collecting and managing validation errors. Supports boolean and error-based validation with formatted error messages.

Installation

go get github.com/LightArrayCapital/go-utilities/validator

Forked from moonrhythm/validator.

Usage

The validator package provides a Validator type that collects validation errors and allows you to check if all validations passed.

Basic Example
package main

import (
	"fmt"
	"github.com/LightArrayCapital/go-utilities/validator"
)

func validateUser(name string, age int) error {
	v := validator.New()

	// Validate name is not empty
	v.Must(name != "", "name is required")

	// Validate age is positive
	v.Must(age > 0, "age must be positive")

	// Validate age is reasonable
	v.Mustf(age < 150, "age %d is too large", age)

	// Return combined error if any validation failed
	return v.Error()
}

func main() {
	if err := validateUser("", -5); err != nil {
		fmt.Println("Validation failed:", err)
	}
}
Using with Errors
func validateData(data string) error {
	v := validator.New()

	// Check if a function returns an error
	if err := someValidation(data); err != nil {
		v.Must(err, "error during someValidation") // Add the error directly
	}

	// Or use Must with the error directly
	v.Must(anotherValidation(data), "")

	return v.Error()
}
Checking Validation Status
v := validator.New()
v.Must(len(username) >= 3, "username must be at least 3 characters")
v.Must(email != "", "email is required")

if !v.Valid() {
	// Handle validation errors
	return v.Error()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsError

func IsError(err error) bool

IsError returns true if given error is validate error

Types

type Error

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

Error is the validate error

func (*Error) Clone

func (err *Error) Clone() *Error

func (*Error) Error

func (err *Error) Error() string

Error implements error interface

func (*Error) Errors

func (err *Error) Errors() []error

Errors returns errors

func (*Error) String

func (err *Error) String() string

String implements fmt.Stringer interface

func (*Error) Strings

func (err *Error) Strings() []string

Strings returns errors in strings

type Validator

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

Validator type

func New

func New() *Validator

New creates new validator

func (*Validator) Add

func (v *Validator) Add(err ...error)

Add adds errors

func (*Validator) Error

func (v *Validator) Error() error

Error returns error if has error

func (*Validator) Must

func (v *Validator) Must(input any, msg string) bool

Must checks input must not be an error or must be true if it is bool. If input is error, the error is added to the error list, with optional wrapping with msg. If input is bool, a new error with msg is added to the error list.

func (*Validator) Mustf

func (v *Validator) Mustf(x any, format string, a ...any) bool

Mustf calls fmt.Sprintf(format, a...) and pass to v.Must

func (*Validator) String

func (v *Validator) String() string

String implements fmt.Stringer

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid returns true if no error

Jump to

Keyboard shortcuts

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