Skip to content
go-cnfgPublic

About

go-playground/validator as a parser for cnfg

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Go Reference main codecov

validator

go-playground/validator as a source for cnfg, so the rules of a config field live in its struct tag and are checked once every source has been read.

go get github.com/go-cnfg/validator
import (
    "time"

    "github.com/go-cnfg/cnfg"
    "github.com/go-cnfg/validator"
)

type Config struct {
    Addr    string        `validate:"required,hostname_port"`
    Workers int           `validate:"gte=1,lte=100"`
    Level   string        `validate:"oneof=debug info warn"`
    Timeout time.Duration `validate:"min=1s"`
}

cfg, err := cnfg.Parse(Config{
    Addr:    ":8080",
    Workers: 4,
    Level:   "info",
    Timeout: 30 * time.Second,
},
    cnfg.Env("APP"),
    cnfg.Flags(),
    validator.Validate(),
)

A field that does not pass stops the parse:

Key: 'Config.Workers' Error:Field validation for 'Workers' failed on the 'gte' tag

The error is go-playground's ValidationErrors, so errors.As gets you every field that failed and the tag it failed on, for a message of your own:

import v10 "github.com/go-playground/validator/v10"

var fields v10.ValidationErrors
if errors.As(err, &fields) {
    for _, f := range fields {
        log.Printf("%s: %s", f.Field(), f.Tag())
    }
}

Validate uses a shared validator with the default settings. With takes one you set up yourself, for your own rules, a different tag name or translations:

import v10 "github.com/go-playground/validator/v10"

v := v10.New()
v.SetTagName("check")

cfg, err := cnfg.Parse(Config{Addr: ":8080"}, cnfg.Flags(), validator.With(v))

Put the check last so every source has been read. Parse returns the zero value of your config together with the error when a source fails, so a config that does not pass never reaches your program.

About

go-playground/validator as a parser for cnfg

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages