assert

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2024 License: MIT Imports: 3 Imported by: 0

README

assert gopher

Dead simple 0 dependency "typed" golang validator

Motivation

Most go validators use either struct tags or a string based representation of the validation rules, and while there's nothing wrong with that, sometimes you just want to quickly validate an input and perhaps cast it to a different type from which you received it.

With assert you may also rest assured you did not typo any of the validation rules as all rules are functions, hence a bit more "typed" than something like validate:"required,min=4,max=5"

Installing
go get github.com/sindreslungaard/assert
Examples

Simple example of validating the character length of an interface and returning it as a string

package main

import "github.com/sindreslungaard/assert"

func main() {
    // This can be any value, any type.
    src := "some-value"

    str, err := assert.Is(src).MinLen(5).MaxLen(10).String()

    if err != nil {
        // handle error
    }

    // str == "some-value"
}

Every assertion formats the input to a type it prefers to test on before executing, so you can do number assertions on string values and so on

num, err := assert.Is("55").MinNum(50).Int()
num, err := assert.Is(55).MinNum(50).Int()
// Both of these are OK

Example validation for a http form POST request

func mySignupHandler(c *gin.Context) {

    username, err := assert.Is(c.PostForm("username")).MinLen(3).MaxLen(20).AlphaNumeric().String()

    if err != nil {
        // let the user know
    }

    password, err := assert.Is(c.PostForm("password")).MinLen(8)

    if err != nil {
        // let the user know
    }

}

We can recude the number of error checks with assert.First

func mySignupHandler(c *gin.Context) {

    username, err1 := assert.Is(c.PostForm("username")).MinLen(3).MaxLen(20).AlphaNumeric().String()
    email,    err2 := assert.Is(c.PostForm("email")).Email().String()
    password, err3 := assert.Is(c.PostForm("password")).MinLen(8).String()

    err := assert.First(err1, err2, err3)

    if err != nil {
        // let the user know
    }

}
Available validations
Validation Argument type Description
NotEmpty Checks if the input (formatted to string) is equal to ""
MinLen int Checks if the length of input (formatted to string) is greater than or equal to the argument
MaxLen int Checks if the length of input (formatted to string) is less than or equal to the argument
MinNum int Checks if the value of input (formatted to int) is greater than or equal to the argument
MaxNum int Checks if the value of input (formatted to int) is less than or equal to the argument
Regex string Checks if the input (formatted to string) matches the provided regex pattern
Email Checks if the input (formatted to string) is a valid email
Alpha Checks if the input (formatted to string) only contains a-z and A-Z
AlphaNumeric Checks if the input (formatted to string) only contains a-z, A-Z and 0-9
Available type cast assertions

Type casts should always be last in your assertion chain, they are what executes all assertions in the chain and returns any errors or the input converted to the new type.

Type Description
String Formats the input as a string using fmt.Sprintf
Int Formats the input as an int using fmt.Sprintf -> strconv.Atoi
Float64 Formats the input as a float64 using fmt.Sprintf -> strconv.ParseFloat with 64 bit precision

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func First

func First(e ...error) error

Types

type AssertFunc

type AssertFunc func(interface{}) error

type Assertion

type Assertion struct {
	// contains filtered or unexported fields
}

func Is

func Is(src interface{}) *Assertion

func (*Assertion) Alpha

func (a *Assertion) Alpha() *Assertion

Alpha asserts that the source (formatted as string) only containes letters a-z and A-Z

func (*Assertion) AlphaNumeric

func (a *Assertion) AlphaNumeric() *Assertion

AlphaNumeric asserts that the source (formatted as string) only containes characters a-z, A-Z and 0-9

func (*Assertion) Email

func (a *Assertion) Email() *Assertion

Email asserts that the source (formatted as string) matches an email pattern

func (*Assertion) Float64

func (a *Assertion) Float64() (float64, error)

Float runs all assertions and returns the source as a float64 and errors if any

func (*Assertion) Int

func (a *Assertion) Int() (int, error)

Int runs all assertions and returns the source as an int and errors if any

func (*Assertion) MaxLen

func (a *Assertion) MaxLen(val int) *Assertion

MaxLen asserts that the character length of the source (formatted as string) is less than or equal to the value

func (*Assertion) MaxNum

func (a *Assertion) MaxNum(val int) *Assertion

MaxNum asserts that the value of the source (formatted as int) is greater than or equal to the value

func (*Assertion) MinLen

func (a *Assertion) MinLen(val int) *Assertion

MinLen asserts that the character length of the source (formatted as string) is greater than or equal to the value

func (*Assertion) MinNum

func (a *Assertion) MinNum(val int) *Assertion

MinNum asserts that the value of the source (formatted as int) is less than or equal to the value

func (*Assertion) NotEmpty

func (a *Assertion) NotEmpty() *Assertion

NotEmpty asserts that the string representation of the source is not equal to ""

func (*Assertion) Regex

func (a *Assertion) Regex(val string) *Assertion

Regex asserts that the source (formatted as string) matches the regex pattern specified

func (*Assertion) String

func (a *Assertion) String() (string, error)

String runs all assertions and returns the source as a string and errors if any

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL