goutils is a set of common utilities for the Go language
This library provides utilities for validation and convertion of types. Seedocumentation
To perform validations on a type you first need a validator
myValidator := validators.String("qwerty")Simple validations can be done using the functions prefixed with IsXXX or HasXXX. These functions returns a bool. This is most useful when you need to do a single validation validation
myValidator.IsHex() // falseValidators can also be chained to do multiple validations in one go. Use the same functions as above, but without the IsXXX or HasXXX prefix and end with the special HasErrors() function
myValidator.Hex().
MaxLen(5).
MinLen(2).
HasErrors() // trueThe check the results of the validations use the helper functions from the validators.Validator type
myValidator.FirstError() // First registered error (Hex)
myValidator.Errors() // All errors [Hex, MinLen]goutils currently has validation implementations for the following types:
- string
- url.URL
Planned validators
- int (8/16/32/64)
- uint (8/16/32/64)
- float (32/64)
More validation functions and types to come.
Convert from string to time.Time:
fmt.Println(converters.String("1502835623824559499").MustTimeFromNsec())
fmt.Println(converters.String("1502835623").MustTimeFromSec())Feature requests, feedback and pull requests welcome!