This package provides a framework for writing validations for Go applications.

github.com/gobuffalo/validate

Build Status GoDoc

This package provides a framework for writing validations for Go applications. It does provide you with few validators, but if you need others you can easly build them.

Installation

$ go get github.com/gobuffalo/validate

Usage

Using validate is pretty easy, just define some Validator objects and away you go.

Here is a pretty simple example:

package main

import (
	"log"

	v "github.com/gobuffalo/validate"
)

type User struct {
	Name  string
	Email string
}

func (u *User) IsValid(errors *v.Errors) {
	if u.Name == "" {
		errors.Add("name", "Name must not be blank!")
	}
	if u.Email == "" {
		errors.Add("email", "Email must not be blank!")
	}
}

func main() {
	u := User{Name: "", Email: ""}
	errors := v.Validate(&u)
	log.Println(errors.Errors)
  // map[name:[Name must not be blank!] email:[Email must not be blank!]]
}

In the previous example I wrote a single Validator for the User struct. To really get the benefit of using go-validator, as well as the Go language, I would recommend creating distinct validators for each thing you want to validate, that way they can be run concurrently.

package main

import (
	"fmt"
	"log"
	"strings"

	v "github.com/gobuffalo/validate"
)

type User struct {
	Name  string
	Email string
}

type PresenceValidator struct {
	Field string
	Value string
}

func (v *PresenceValidator) IsValid(errors *v.Errors) {
	if v.Value == "" {
		errors.Add(strings.ToLower(v.Field), fmt.Sprintf("%s must not be blank!", v.Field))
	}
}

func main() {
	u := User{Name: "", Email: ""}
	errors := v.Validate(&PresenceValidator{"Email", u.Email}, &PresenceValidator{"Name", u.Name})
	log.Println(errors.Errors)
        // map[name:[Name must not be blank!] email:[Email must not be blank!]]
}

That's really it. Pretty simple and straight-forward Just a nice clean framework for writing your own validators. Use in good health.

Built-in Validators

To make it even simpler, this package has a children package with some nice built-in validators.

package main

import (
	"log"

	"github.com/gobuffalo/validate"
	"github.com/gobuffalo/validate/validators"
)

type User struct {
	Name  string
	Email string
}


func main() {
	u := User{Name: "", Email: ""}
	errors := validate.Validate(
		&validators.EmailIsPresent{Name: "Email", Field: u.Email, Message: "Mail is not in the right format."},
		&validators.StringIsPresent{Field: u.Name, Name: "Name"},
	)
	log.Println(errors.Errors)
	// map[name:[Name can not be blank.] email:[Mail is not in the right format.]]
}

All fields are required for each validators, except Message (every validator has a default error message).

Available Validators

A full list of available validators can be found at https://godoc.org/github.com/gobuffalo/validate/validators.

Owner
Buffalo - The Go Web Eco-System
Buffalo - The Go Web Eco-System
Comments
  • Allow custom message for validation errors

    Allow custom message for validation errors

    I think this is the first step to allow localization of error messages. Actually, from now on, a developer can use the translator instance to translate the custom message error.

  • Dynamic translation of validation errors

    Dynamic translation of validation errors

    Hi everybody !

    I am facing a little issue with the translations of the validation errors. I see it is possible to add a static translation by adding a Message field in the validator, like this: &validators.EmailIsPresent{Name: "Email", Field: u.Email, Message: "Mail is not in the right format."}

    This works great for a static translation, but what about dynamic translations (based on the user language) ?

    In actions, I know I can use this method: T.Translate(c, "users.login-success") but in models (where I don't have access to the context), I can't use that.

    Is there any solution to solve this issue ?

    Thanks a lot !

  • What is the context behind adding uuid/v3?

    What is the context behind adding uuid/v3?

    Adding this code to master is breaking installation of pop:

    package github.com/gofrs/uuid/v3: cannot find package "github.com/gofrs/uuid/v3" in any of:
    	/usr/local/go/src/github.com/gofrs/uuid/v3 (from $GOROOT)
    	/go/src/github.com/gofrs/uuid/v3 (from $GOPATH)
    

    And indeed, the v3 path is explicitly unsupported now in uuid: https://github.com/gofrs/uuid#go-111-modules

    Specifically:

    As a result, v3.2.0 also drops support for the github.com/gofrs/uuid/v3 import path. ... With the v3.2.0 release, all gofrs/uuid consumers should use the github.com/gofrs/uuid import path.

    This is also an issue in gobuffalo/nulls, both from code submitted by @paganotoni.

  • Release `IntsAreEqual` and `IntsAreNotEqual` validators

    Release `IntsAreEqual` and `IntsAreNotEqual` validators

    I would like to use IntsAreNotEqual that was added in 2018, but there have been no releases since before that. Is there something holding this back from being released?

  • Question: how to use validators.CustomKeys?

    Question: how to use validators.CustomKeys?

    Hello! Thank you for a great library, it makes writing validation a pleasure.

    I want to get error messages with dot for deep objects field names. For example, I want to specify key name like this:

    &validators.StringIsPresent{
    	Name:    "object.title",
    	Field:   rootObj.object.Title,
    	Message: "Object title must be specified",
    }
    

    But as a result I have a field name like this: objecttitle, because of transformation name to underscore here.

    I know I can bypass it by writing custom validator, which won't have such transformation. But I see, that validators.GenerateKey function first checks name in map validators.CustomKeys, which is empty by default.

    How can I add my custom key names to this map?

  • Wondering how to implement a Unique validator

    Wondering how to implement a Unique validator

    I am working on a project in Buffalo that requires a model have a Unique but user configurable string field. Is there any particular way I should go about passing the tx into the validator?

    Doing this seems to work:

    type FieldIsUnique struct {
    	Table string
    	Name  string
    	Field string
    	tx *pop.Connection
    }
    
    func (v *FieldIsUnique) IsValid(errors *validate.Errors) {
    	exists, err := v.tx.Where(fmt.Sprintf("%s = ?", v.Name), v.Field).Exists(v.Table)
    	if exists || err != nil {
    		errors.Add(strings.ToLower(v.Name), fmt.Sprintf("%s is already in use", v.Field))
    	}
    }
    
    func (m *Model) Validate(tx *pop.Connection) (*validate.Errors, error) {
    	return validate.Validate(
    		&FieldIsUnique{Table: "models", Field: m.WannaBeUnique, Name: "WannaBeUnique", tx: tx},
    	}, nil
    }
    

    But I am wondering if there is a better pattern I should follow...

  • License?

    License?

    Could you please clarify what license (if any) this is released under by adding a LICENSE file?

    This is a dependency of gobuffalo/pop which you released under the MIT license so I'm hoping you'll say MIT, but really just looking for clarification.

    Thanks!

  • Add more validators

    Add more validators

    For int:

    • int_is_greater_than_or_equal_to.go
    • int_is_less_than_or_equal_to.go

    For float:

    • float_array_is_present.go
    • float_is_greater_than.go
    • float_is_greater_than_or_equal_to.go
    • float_is_less_than.go
    • float_is_less_than_or_equal_to.go
    • float_is_present.go
    • floats_are_equal.go
    • floats_are_not_equal.go
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
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
Validator - Replace the validation framework used by gin

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

Jan 18, 2022
[Go] Package of validators and sanitizers for strings, numerics, slices and structs

govalidator A package of validators and sanitizers for strings, structs and collections. Based on validator.js. Installation Make sure that Go is inst

Jan 6, 2023
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
Dec 28, 2022
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
Just a playground with some interesting concepts like pipelines aka middleware, handleFuncs, request validations etc. Check it out.

Pipeline a.k.a middleware in Go Just a playground with some interesting concepts like pipelines aka middleware, handleFuncs, request validations etc.

Dec 9, 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
Golanger Web Framework is a lightweight framework for writing web applications in Go.

/* Copyright 2013 Golanger.com. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except

Nov 14, 2022
Golanger Web Framework is a lightweight framework for writing web applications in Go.

/* Copyright 2013 Golanger.com. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except

Nov 14, 2022
Boilerplate for writing Go applications without framework using hexagonal application development approach
Boilerplate for writing Go applications without framework using hexagonal application development approach

Boilerplate for writing Go applications without framework using hexagonal application development approach

Dec 2, 2022
Monkey programming language project from 'Writing An Interpreter In Go'and 'Writing A Compiler In Go' Books
Monkey programming language project from 'Writing An Interpreter In Go'and 'Writing A Compiler In Go' Books

Monkey Monkey programming language ?? project from "Writing An Interpreter In Go

Dec 16, 2021
Package kml provides convenince methods for creating and writing KML documents.

go-kml Package kml provides convenience methods for creating and writing KML documents. Key Features Simple API for building arbitrarily complex KML d

Jul 29, 2022
A library to simplify writing applications using TCP sockets to stream protobuff messages

BuffStreams Streaming Protocol Buffers messages over TCP in Golang What is BuffStreams? BuffStreams is a set of abstraction over TCPConns for streamin

Dec 13, 2022
Source code of a YouTube tutorial about writing terminal applications with Golang

Bubble Tea Demo 00 Source code of a YouTube tutorial about writing terminal applications with Golang by using Bubble Tea. Contains a simple counter ap

Nov 10, 2022
Package goth provides a simple, clean, and idiomatic way to write authentication packages for Go web applications.

Goth: Multi-Provider Authentication for Go Package goth provides a simple, clean, and idiomatic way to write authentication packages for Go web applic

Dec 29, 2022
Package i18n provides internationalization and localization for your Go applications.

i18n Package i18n provides internationalization and localization for your Go applications. Installation The minimum requirement of Go is 1.16. go get

Nov 9, 2022
The android-go project provides a platform for writing native Android apps in Go programming language.
The android-go project provides a platform for writing native Android apps in Go programming language.

android-go The android-go project aims to provide a platform (namely an SDK) for writing native Android apps in Go programming language. All things he

Jan 5, 2023
Flamingo Framework and Core Library. Flamingo is a go based framework for pluggable web projects. It is used to build scalable and maintainable (web)applications.
Flamingo Framework and Core Library. Flamingo is a go based framework for pluggable web projects. It is used to build scalable and maintainable (web)applications.

Flamingo Framework Flamingo is a web framework based on Go. It is designed to build pluggable and maintainable web projects. It is production ready, f

Jan 5, 2023