validate

package
v0.295.2 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

Validate

Package validate contains validation helpers.

struct field tag based validation helpers

Tag Description/Constraints Supported Types Syntax Examples Notes
min Value must be greater than or equal to the minimum value specified in the tag. Numerical, comparable types (e.g., int, float) min:"10" Works with numerical and comparable types like integers and floats.
max Value must be less than or equal to the maximum value specified in the tag. Numerical, comparable types max:"25" Upper-bound check for compatible types (same as min).
range Value must fall within any of the provided ranges (OR logic between comma-separated values). String, int/uint/float, rune range:"..10"
range:"a-z,A-Z"
range:"5..10"
- Open-ended formats: ..max → ≤ max; min.. → ≥ min.
- For strings: Lexical (range:"a..bb") or character ranges (range:"a-z,A-Z").
- Ranges are comma-separated and in OR logic.
char Every character in the string must fall within specified character ranges (e.g., letters, symbols). String, rune char:"a-z,A-Z"
char:"0-9"
Supports open-ended ranges like "A-" (characters from A to end of alphabet).
length Constrain the length of the value (len(value) must match the condition). string, slice, map, channel length:"<=10"
length:"5..10"
length"5"
len:"5"
Alias: Use len:"..." for the same effect. Works with types that have a defined len().
enum Limits valid values to a predefined list of options. Any type (registered or representable) enum:"foo bar baz"
enum:"foo,bar,baz"
- Uses either enum.Register for registration or direct representation in the tag.
- Supports various types when registered.

Documentation

Overview

Example (RangeInt)
package main

import (
	"go.llib.dev/frameless/pkg/validate"
)

func main() {
	type T struct {
		V int `range:"0..100"`
	}

	validate.Value(T{V: 42})  // no error
	validate.Value(T{V: -1})  // validate.Error
	validate.Value(T{V: 101}) // validate.Error
}
Example (RangeIntMulti)
package main

import (
	"go.llib.dev/frameless/pkg/validate"
)

func main() {
	type T struct {
		Num1 int `range:"0..100"`
		Num2 int `range:"0..25,30..50"`
	}

	_ = validate.Value(T{})
}

Index

Examples

Constants

View Source
const ImplementationError errorkit.Error = "ImplementationError"
View Source
const InsideValidateFunc copt = 1

InsideValidateFunc option indicates that the function is being used inside a Validate() error method.

When this option is set, the Validate function call is skipped to prevent an infinite loop caused by a circular Validate call.

Variables

This section is empty.

Functions

func Struct

func Struct(v any, opts ...Option) error

func StructField

func StructField(field reflect.StructField, value reflect.Value, opts ...Option) error

func Value

func Value[T any](v T, opts ...Option) error

Types

type Error added in v0.288.0

type Error struct{ Cause error }

Error is a validation error, that represents an incorrect content.

func (Error) Error added in v0.288.0

func (err Error) Error() string

func (Error) Unwrap added in v0.288.0

func (err Error) Unwrap() error

type Option added in v0.287.0

type Option option.Option[config]

type ValidationError deprecated

type ValidationError = Error

Deprecated: use validate.Error instead

type Validator added in v0.287.0

type Validator interface {
	Validate() error
}

Jump to

Keyboard shortcuts

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