Opinionated go to validation library

🙅 valeed

Your opinionated go-to validation library. Struct tag-based.

Validate here, validate there, validate everywhere. Sleek and simple validation to the rescue. Opinionated wrapper on famous https://github.com/go-playground/validator.

Usage

func main() {
	type UserForm struct {
		Name     string    `validate:"required,gte=7"`
		Email    string    `validate:"email"`
		Age      int       `validate:"required,numeric,min=1,max=99"`
		CreateAt int       `validate:"gte=1"`
		Safe     int       `validate:"-"`
		UpdateAt time.Time `validate:"required"`
		Code     string    `validate:"required"`
	}

	err := valeed.Validate(UserForm{})
	fmt.Println(err.Error()) // UserForm.Name must be required; UserForm.Email must be email; UserForm.Age must be required, actual value is 0; UserForm.CreateAt must be gte{1}, actual value is 0; UserForm.UpdateAt must be required, actual value is 0001-01-01 00:00:00 +0000 UTC; UserForm.Code must be required

	err = valeed.ValidateWithOpts(UserForm{}, valeed.ValidateOptions{Mode: valeed.ModeVerbose})
	fmt.Println(err.Error()) // invalid fields at home/projects/myproject/main.go:24: UserForm.Name must be required; UserForm.Email must be email; UserForm.Age must be required, actual value is 0; UserForm.CreateAt must be gte{1}, actual value is 0; UserForm.UpdateAt must be required, actual value is 0001-01-01 00:00:00 +0000 UTC; UserForm.Code must be required

	err = valeed.ValidateWithOpts(UserForm{}, valeed.ValidateOptions{Mode: valeed.ModeCompact})
	fmt.Println(err.Error()) // invalid fields: Name, Email, Age, CreateAt, UpdateAt, Code
}

Supported Struct Tags

This lib is basically a wrapper over github.com/go-playground/validator (v10). Which means all supported validation structs would follow what originally defined by the lib (SPOILER: there are tons).

Check here for available tags or how to add your custom validation tag:

FAQ

Why not just use go-playground/validator?

Because the default error message format from go-playground/validator is imo overly verbose and includes characters like \n like this:

Key: 'UserForm.Name' Error:Field validation for 'Name' failed on the 'required' tag\nKey: 'UserForm.Email' Error:Field validation for 'Email' failed on the 'email' tag\nKey: 'UserForm.Age' Error:Field validation for 'Age' failed on the 'required' tag\nKey: 'UserForm.CreateAt' Error:Field validation for 'CreateAt' failed on the 'gte' tag\nKey: 'UserForm.UpdateAt' Error:Field validation for 'UpdateAt' failed on the 'required' tag\nKey: 'UserForm.Code' Error:Field validation for 'Code' failed on the 'required' tag\n

Using valeed, will result to this message:

UserForm.Name must be required; UserForm.Email must be email; UserForm.Age must be required, actual value is 0; UserForm.CreateAt must be gte{1}, actual value is 0; UserForm.UpdateAt must be required, actual value is 0001-01-01 00:00:00 +0000 UTC; UserForm.Code must be required

How this could help?

This is very simple but enough to help on many cases like, config validation, dependency validation, input validations.

I want to add custom validator!

You can inject your own validator/v10 instance with your custom validator tags. To use it globally you can set using valeed.SetGlobal().

I want to add another sophisticated custom validator!

If it's too sophisticated, honestly just use another library you think suitable. This one meant for simple quick usecases only.

But I want to add custom validator!

Make a merge request. Everyone can make a change.

Owner
Avré Barra
NodeJS, Ruby, Golang Software Engineer. A fan of codebases, poetry, and codebases that's also a poetry.
Avré Barra
Similar Resources

Gin Middleware to extract json tag value from playground validator's errors validation

Json Tag Extractor for Go-Playground Validator This is Gin Middleware that aim to extract json tag and than store it to FieldError.Field() object. Ins

Jan 14, 2022

Validator - Replace the validation framework used by gin

validator Replace the validation framework used by gin replace mod:replace githu

Jan 18, 2022

This project is a Library to validate a pull secret

This project is a Library to validate a pull secret

lib-ps-validator Description This repository and its contents are completely UNSUPPORTED in any way and are not part of official documentation. Lib to

Jul 21, 2021

The Hyperscale InputFilter library provides a simple inputfilter chaining mechanism by which multiple filters and validator may be applied to a single datum in a user-defined order.

Hyperscale InputFilter Branch Status Coverage master The Hyperscale InputFilter library provides a simple inputfilter chaining mechanism by which mult

Oct 20, 2021

A library to remove special characters from a string.

spechar Is a small library for removing special characters from strings. Install First you have to install the package: go get github.com/gowizzard/sp

Dec 13, 2021

A tool/library to run custom validations on Kubernetes resources in parallel

cluster-validator cluster-validator is a tool/library for performing resource validations in parallel on a Kubernetes cluster. For example, validating

Mar 2, 2022

An idiomatic Go (golang) validation package. Supports configurable and extensible validation rules (validators) using normal language constructs instead of error-prone struct tags.

ozzo-validation Description ozzo-validation is a Go package that provides configurable and extensible data validation capabilities. It has the followi

Jan 7, 2023

Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.

Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.

Checker 中文版本 Checker is a parameter validation package, can be use in struct/non-struct validation, including cross field validation in struct, elemen

Dec 16, 2022

Courier Golang client library provides an opinionated wrapper over paho MQTT library to add features on top of it

Courier Golang Client Library Introduction Courier Golang client library provides an opinionated wrapper over paho MQTT library to add features on top

Nov 19, 2022

Govalid is a data validation library that can validate most data types supported by golang

Govalid is a data validation library that can validate most data types supported by golang. Custom validators can be used where the supplied ones are not enough.

Apr 22, 2022

:key: Idiotproof golang password validation library inspired by Python's passlib

passlib for go Python's passlib is quite an amazing library. I'm not sure there's a password library in existence with more thought put into it, or wi

Dec 30, 2022

Library providing opanapi3 and Go types for store/validation and transfer of ISO-4217, ISO-3166, and other types.

go-types This library has been created with the purpose to facilitate the store, validation, and transfer of Go ISO-3166/ISO-4217/timezones/emails/URL

Nov 9, 2022

:key: Idiotproof golang password validation library inspired by Python's passlib

passlib for go 100% modules-free. Python's passlib is quite an amazing library. I'm not sure there's a password library in existence with more thought

Dec 19, 2022

testcase is an opinionated behavior-driven-testing library

Table of Contents testcase Guide Official API Documentation Getting Started / Example Modules Summary DRY Modularization Stability Case Study About te

Nov 10, 2022

Go package for dealing with EU VAT. Does VAT number validation & rates retrieval.

Package vat Package for validating VAT numbers & retrieving VAT rates in Go. Installation Use go get. go get github.com/dannyvankooten/vat Then impor

Dec 6, 2022

Struct validation using tags

Govalid Use Govalid to validate structs. Documentation For full documentation see pkg.go.dev. Example package main import ( "fmt" "log" "strings"

Dec 6, 2022

Validate Golang request data with simple rules. Highly inspired by Laravel's request validation.

Validate Golang request data with simple rules. Highly inspired by Laravel's request validation.

Validate golang request data with simple rules. Highly inspired by Laravel's request validation. Installation Install the package using $ go get githu

Dec 29, 2022

⚔ Go package for data validation and filtering. support Map, Struct, Form data. Go通用的数据验证与过滤库,使用简单,内置大部分常用验证、过滤器,支持自定义验证器、自定义消息、字段翻译。

Validate The package is a generic Go data validate and filter tool library. Support quick validate Map, Struct, Request(Form, JSON, url.Values, Upload

Dec 28, 2022

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving

Package validator Package validator implements value validations for structs and individual fields based on tags. It has the following unique features

Jan 1, 2023
Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.
Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.

Checker 中文版本 Checker is a parameter validation package, can be use in struct/non-struct validation, including cross field validation in struct, elemen

Dec 16, 2022
Library providing opanapi3 and Go types for store/validation and transfer of ISO-4217, ISO-3166, and other types.

go-types This library has been created with the purpose to facilitate the store, validation, and transfer of Go ISO-3166/ISO-4217/timezones/emails/URL

Nov 9, 2022
Struct validation using tags

Govalid Use Govalid to validate structs. Documentation For full documentation see pkg.go.dev. Example package main import ( "fmt" "log" "strings"

Dec 6, 2022
Validate Golang request data with simple rules. Highly inspired by Laravel's request validation.
Validate Golang request data with simple rules. Highly inspired by Laravel's request validation.

Validate golang request data with simple rules. Highly inspired by Laravel's request validation. Installation Install the package using $ go get githu

Dec 29, 2022
Dec 28, 2022
:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving

Package validator Package validator implements value validations for structs and individual fields based on tags. It has the following unique features

Jan 1, 2023
An interesting go struct tag expression syntax for field validation, etc.

go-tagexpr An interesting go struct tag expression syntax for field validation, etc. Usage Validator: A powerful validator that supports struct tag ex

Jan 9, 2023
Swagger builder and input validation for Go servers
Swagger builder and input validation for Go servers

crud A Swagger/OpenAPI builder and validation library for building HTTP/REST APIs. Heavily inspired by hapi and the hapi-swagger projects. No addition

Jan 5, 2023
Go package containing implementations of efficient encoding, decoding, and validation APIs.

encoding Go package containing implementations of encoders and decoders for various data formats. Motivation At Segment, we do a lot of marshaling and

Dec 25, 2022
Simple module for validation inn control number

simple module for validation inn control number

Sep 4, 2022