validator

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2022 License: MIT Imports: 8 Imported by: 0

README

go-struct-validator

Build Status

Neo's Go Struct Validator is a simple validator built on the core of OAS Specification. The validator is heavily inspired by the OAS Specification approach leading to the creation of structs in a generic manner.

The validator covers the specifications, and its respective validations according to OAS.



Installation
go get github.com/neo7337/go-struct-validator
Benchmarking
go test -run=Bench -bench=. -benchtime 5000000x
S.No. Name Ops BenchResult
1 BenchmarkValidator-8 5000000 849 ns/op
1 BenchmarkValidatorParallel-8 5000000 268 ns/op
Quick Start Guide

It comes with a simple usage as explained below, just import the package, and you are good to go.

To add check for validations, add the constraints tag in the struct fields.


    package main
    
    import (
        "fmt"
        "github.com/neo7337/go-struct-validator"
    )
    
    var sv = validator.NewStructValidator()
    
    type TestStruct struct {
        Name        string  `json:"name" constraints:"required=true;nillable=true;min-length=5"`
        Age         int     `json:"age" constraints:"required=true;nillable=true;min=21"`
        Description string  `json:"description" constraints:"required=true;nillable=true;max-length=50"`
        Cost        float64 `json:"cost" constraints:"required=true;nillable=true;exclusiveMin=200"`
        ItemCount   int     `json:"itemCount" constraints:"required=true;nillable=true;multipleOf=5"`
    }
    
    func main() {
        msg := TestStruct{
            Name:        "Test",
            Age:         25,
            Description: "this is bench testing",
            Cost:        299.9,
            ItemCount:   2000,
        }
		
        if err := sv.Validate(msg); err != nil {
            fmt.Errorf(err)
        }
    }
Features
Validations Supported
S.No. Name Data Type Supported Status
4 min numeric
5 max numeric
6 exclusiveMin numeric
7 exclusiveMax numeric
8 multipleOf numeric
9 max-length string
10 min-length string
11 pattern string
11 notnull string
12 enum all

Documentation

Index

Constants

View Source
const (
	MaxInt  = 1<<(intSize-1) - 1
	MinInt  = -1 << (intSize - 1)
	MaxUint = 1<<intSize - 1
)

Variables

View Source
var (
	ErrNotNull = "notnull validation failed for field %s"

	ErrMin = "min value validation failed for field %s"

	ErrMax = "max value validation failed for field %s"

	ErrExclusiveMin = "exclusive min validation failed for field %s"

	ErrExclusiveMax = "exclusive max validation failed for field %s"

	ErrMultipleOf = "multipleOf validation failed for field %s"

	ErrMinLength = "min-length validation failed for field %s"

	ErrMaxLength = "max-length validation failed for field %s"

	ErrPattern = "pattern validation failed for field %s"

	ErrEnums = "enum validation failed for field %s"

	ErrBadConstraint = "invalid constraint %s with value '%s' for field %s"

	ErrInvalidValidationForField = "invalid validation applied to the field %s"

	ErrNotSupported = errors.New("unsupported constraint on type")

	ErrConversionFailed = errors.New("conversion failed")
)

Functions

This section is empty.

Types

type StructValidator

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

func NewStructValidator

func NewStructValidator() *StructValidator

func NewStructValidatorWithCache

func NewStructValidatorWithCache() *StructValidator

func (*StructValidator) Validate

func (sv *StructValidator) Validate(v interface{}) error

type StructValidatorFunc

type StructValidatorFunc func(field field, param string) error

Jump to

Keyboard shortcuts

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