z

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package z (zog) allows data validation in the form of composable schemas inspired by zod https://zod.dev/, using reflect https://pkg.go.dev/reflect.

schema := z.Struct(
	z.StructSchema{
		"ID": z.Int().Min(0),
		"Title": z.String(func(title string) error {
			if strings.HasPrefix(title, " ") {
				return errors.New("title should not begin with empty spaces")
			}
			return nil
		}).Min(10).Max(200),
		"Authors": z.Slice[string]().Min(1),
		"Appendix": z.Struct(z.StructSchema{
			"Body": z.String().Min(1),
			"Annex": z.Struct(z.StructSchema{
				"AnnexBody": z.String().Max(100),
			}).Optional(),
		}),
	},
)

errors := z.Validate3(&book, schema)

Using Gin https://gin-gonic.com/

	func (service *Service) testEndpoint(c *gin.Context) error {
		request := dtos.testEndpoint{}

		if err := c.ShouldBind(&request); err != nil {
			return err
		}

		errors := z.Validate(&request, testEndpointDto)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FloatValidation

type FloatValidation struct {
	Validation
}

func Float

func Float(fns ...(func(float64) error)) *FloatValidation

type IntValidation

type IntValidation struct {
	Validation
}

func Int

func Int(fns ...(func(int) error)) *IntValidation

func (IntValidation) Max

func (intValidation IntValidation) Max(maxValue int) *IntValidation

func (IntValidation) Min

func (intValidation IntValidation) Min(minValue int) *IntValidation

type SliceValidation

type SliceValidation[T any] struct {
	Validation
}

func Slice

func Slice[T any](fns ...(func([]T) error)) *SliceValidation[T]

func (SliceValidation[T]) ForEach

func (sliceValidation SliceValidation[T]) ForEach(subSchemas ...ValidationI) *SliceValidation[T]

func (SliceValidation[T]) Max

func (sliceValidation SliceValidation[T]) Max(maxLen int) *SliceValidation[T]

func (SliceValidation[T]) Min

func (sliceValidation SliceValidation[T]) Min(minLen int) *SliceValidation[T]

type StringValidation

type StringValidation struct {
	Validation
}

func String

func String(fns ...(func(string) error)) *StringValidation

String builds a string validation schema, accepts a list of validation functions as arguments

func (StringValidation) Max

func (stringValidation StringValidation) Max(stringLen int) *StringValidation

Max string length

func (StringValidation) Min

func (stringValidation StringValidation) Min(stringLen int) *StringValidation

Min string length

func (StringValidation) Regex

func (stringValidation StringValidation) Regex(regex *regexp.Regexp) *StringValidation

Regex validation

type StructSchema

type StructSchema = map[string]ValidationI

StructSchema is the type for key value pairs used to validate structs

type StructValidation

type StructValidation struct {
	Validation
}

func Struct

func Struct(schemaMap StructSchema) *StructValidation

Struct builds a struc validation schema, accepts a map (StructSchema) with key validation pairs.

func (StructValidation) SubSchema

func (structValidation StructValidation) SubSchema(subSchemas ...ValidationI) *StructValidation

SubSchema allows to compose sub struct schemas

type Validation

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

func From

func From[T any](reflectKind reflect.Kind, fns ...(func(T) error)) *Validation

From allows for user defined validations, using reflect for type validation, keep in mind that a mismatch between reflect and actual type conversion will result in runtime panic

type CustomStringValidation struct {
	z.Validation
}

func CustomString() *CustomStringValidation {
	fns := [](func(string) error){func(s string) error {
		if s == "ilegal_value" {
			return errors.New("invalid string")
		}
		return nil
	}}

	return &CustomStringValidation{
		Validation: *z.From(reflect.String, fns...),
	}
}

func (Validation) Optional

func (validation Validation) Optional() *Validation

Optional allows for nil pointers to be a valid schema, this allows for optional values to be validated if they are present or skipped if they are a nil pointer

func (Validation) Required

func (validation Validation) Required() *Validation

Required fails the validation on nil pointers (default), see Optional

type ValidationErrorItem

type ValidationErrorItem struct {
	Error string              `json:"error"`
	Path  []string            `json:"path"`
	Kind  ValidationErrorKind `json:"kind"`
}

ValidationErrorItem gives information about validation errors, it implements json marshall (json tags)

func Validate

func Validate[T any](value *T, schema ValidationI) []ValidationErrorItem

Validate takes a struct pointer and a schema, returns the list of validation errors found, an empty list means successful validation

func (ValidationErrorItem) String

func (validationErrorItem ValidationErrorItem) String() string

type ValidationErrorKind

type ValidationErrorKind int
const (
	TypeError ValidationErrorKind = iota
	ValidationError
	FieldError
	NilError
)

func (ValidationErrorKind) String

func (validationErrorKind ValidationErrorKind) String() string

type ValidationI

type ValidationI interface {
	// contains filtered or unexported methods
}

ValidationI is the interface which all sub validators should implement

Jump to

Keyboard shortcuts

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