validator

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package validator provides functions for validating data using the Go Playground Validator. It allows you to validate structs and individual fields, providing error messages in case of validation failures.

The package relies on the go-playground/validator/v10 library for validation and go-playground/universal-translator for translating error messages to human readable text.

Example Usage:

package main

import (
    "fmt"
    "github.com/romankravchuk/nix/validator"
)

type User struct {
    Username string `validate:"required,min=4,max=32"`
    Email    string `validate:"required,email"`
    Age      int    `validate:"gte=18"`
}

func main() {
    user := User{
        Username: "john_doe",
        Email:    "john.doe@example.com",
        Age:      25,
    }
	v := validator.New()

    if isValid := v.Validate(user); !isValid {
        fmt.Println("Validation failed:", v.Errors())
    } else {
        fmt.Println("Validation successful!")
    }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Validator added in v0.0.8

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

func New added in v0.0.8

func New() *Validator

func (*Validator) Errors added in v0.0.8

func (v *Validator) Errors() map[string]string

Errors returns the validation errors as a map of field names to error messages.

func (*Validator) RegisterRules added in v0.0.8

func (v *Validator) RegisterRules(data any, rules map[string]string)

RegisterRules registers the custom validation rules for the given 'data'

func (*Validator) Validate added in v0.0.8

func (v *Validator) Validate(data interface{}) bool

Validate validates the given 'data' using the validator.

Jump to

Keyboard shortcuts

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