Categorygithub.com/neo7337/go-struct-validator
repositorypackage
1.0.4
Repository: https://github.com/neo7337/go-struct-validator.git
Documentation: pkg.go.dev

# 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.NameOpsBenchResult
1BenchmarkValidator-85000000849 ns/op
1BenchmarkValidatorParallel-85000000268 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.NameData Type SupportedStatus
4minnumeric
5maxnumeric
6exclusiveMinnumeric
7exclusiveMaxnumeric
8multipleOfnumeric
9max-lengthstring
10min-lengthstring
11patternstring
11notnullstring
12enumall