Struct Validator

The codec/validator package provides struct validation inspired by the OpenAPI Specification (OAS). It uses constraints struct tags to declaratively validate field values.

Installation

go get oss.nandlabs.io/golly/codec/validator

Quick Start

Add constraints tags to your struct fields and use the validator to check values:

package main

import (
    "fmt"
    validator "oss.nandlabs.io/golly/codec/validator"
)

type TestStruct struct {
    Name        string  `json:"name" constraints:"min-length=5"`
    Age         int     `json:"age" constraints:"min=21"`
    Description string  `json:"description" constraints:"max-length=50"`
    Cost        float64 `json:"cost" constraints:"exclusiveMin=200"`
    ItemCount   int     `json:"itemCount" constraints:"multipleOf=5"`
}

func main() {
    sv := validator.NewStructValidator()
    msg := TestStruct{
        Name:        "Test",
        Age:         25,
        Description: "this is bench testing",
        Cost:        299.9,
        ItemCount:   2000,
    }

    if err := sv.Validate(msg); err != nil {
        fmt.Println(err)
    }
}

Fields without constraints tags are skipped during validation.

Supported Validations

NameData TypeDescriptionStatus
minnumericMinimum value (inclusive)โœ…
maxnumericMaximum value (inclusive)โœ…
exclusiveMinnumericMinimum value (exclusive)โœ…
exclusiveMaxnumericMaximum value (exclusive)โœ…
multipleOfnumericValue must be a multiple ofโœ…
max-lengthstringMaximum string lengthโœ…
min-lengthstringMinimum string lengthโœ…
patternstringRegex pattern matchโœ…
notnullstringMust not be emptyโœ…
enumallValue must be one of listed valuesโœ…

Usage

Multiple Constraints

Combine multiple constraints on a single field:

type Config struct {
    Port        int    `constraints:"min=1,max=65535"`
    Host        string `constraints:"min-length=1,pattern=^[a-z0-9.-]+$"`
    Environment string `constraints:"enum=dev|staging|prod"`
}

Skipping Validation

Fields without constraints tags are not validated:

type User struct {
    Name   string `constraints:"min-length=1"`  // validated
    Email  string `constraints:"pattern=.+@.+"` // validated
    Notes  string                                // skipped
}