Giter VIP home page Giter VIP logo

validation's Introduction

Coverage Go Reference Go Report Card

Go Validation

This package allows you to annotate your structs with a tag named "valid" to apply field specific validations. This package provides 12 common validation rules like string, email, bool and so on. This package is designed to either fail fast or not. The response will list any error raised by a rule if you choose not to fail fast. It is good to know that the messages are written in English, but you can easily override some or all of them with either SetMessage or SetMessages. See the validation_test file for an example.

Some rules can or will take options. A bool can take an option such as "true", "false", "0" or "1". Min and max rules take options as a comma-separated list of numbers like "0,10". You can find an example for everything in the tests.

Contents

Installation

$ go get -u github.com/adnanbrq/validation/v2

Testing

The whole package, rules and helpers, will be tested to achieve the required minimum coverage of 100%.
There is currently no verifier implemented for the CI, but this step is planned.

Usage

package main

import (
  "fmt"
  "github.com/adnanbrq/validation/v2"
)

type SignUpSchema struct {
  Username string  `valid:"string|min:6|max:32"`           // Needs to be a string
  Password string  `valid:"string|between:6,32"`           // Same as min:6|max:32
  Name     *string `valid:"nullable|pointer|string|min:6"` // 6 characters in length, if present
}

func main() {
  signUpDTO := SignUpSchema{
    Username: "adnanbrq",
    Password: "This is a secret",
  }

  /*
    The result is a map[string][]string that looks like this, for example
    map[string][]string{
      "field name": {"rule message", "rule message"},
      ...
    }
  */
  result, err := validation.
    NewValidator().
    // SetMessage can be used to set a custom message for a Rule using templates
    // You can also override every rule message with SetMessages(map[string]string{})
    // You can use the options {Name, O1, O2}.
    // O1 = Option 1. "1" would be O1 for Rule "between:1,2" or Rule "min:1"
    SetMessage("no-string", "{{.Name}} is not a valid string.").
    Validate(signUpDTO)

  if err != nil {
    panic(err)
  }

  if len(result) != 0 {
    fmt.Println(result)
  } else {
    fmt.Println("Everything fine")
  }
}

Rules

Name Logic Options
required Checks that the value is not nil -
nullable Checks that no rules are run if the value is nil -
bool Checks that the value is 0,1,true or false bool e.g. true,false,0,1
default Does nothing -
email Checks that the value is a string and matches a predefined regex -
json Checks that given value is a map or a regex verified json string -
jwt Checks that the value is a string matching ABC.DEF.GHI -
between Checks that the value's length or size is in given range min,max e.g. 0,32
max Checks length of strings, size of ints, etc. max e.g. 0
min Checks length of strings, size of ints, etc. min e.g. 32
numeric Checks that the value is int,int32,int64,uint32,float32, ... -
int Checks that the value is int,int8,int16,int32,int64. bitsize 32 or 64
uint Checks that the value is uint,uint8,uint16,uint32,uint64. bitsize 32 or 64
float Checks that the value is float32,float64. bitsize 32 or 64
string Checks that the value is a string or points to a string -
pointer Checks that the value is a pointer -
time Checks that the value is time.Time yesterday,today,future,past
date Checks that the value is time.Time and that the specified Date is set D(ay), M(onth), Y(ear) with a specific value e.g. D12 for Day 12

Dependencies

validation's People

Contributors

adnanbrq avatar

Stargazers

 avatar  avatar

Watchers

 avatar

validation's Issues

Validation does not work with custom types

Validating custom types can cause unexpected panic.

Example of a custom type:

type Gender string

const (
      GenderMale Gender= "male"
      GenderFemale = "female"
)

If we then try to validate something of this type with, say, the string rule, we get a panic.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.