Documentation ¶
Overview ¶
Package valid validates strings of text.
The flag Checker which is set in function NewSchema allows easily to check and/or to modify the input for some types.
Index ¶
- Variables
- func Base32(s *Schema, str string) (value string, err error)
- func Base64(s *Schema, str string) (value string, err error)
- func Bool(s *Schema, str string) (value bool, err error)
- func CheckDomain(flag Checker, domain string, forEmail bool) (err error)
- func ContainsFloat64(s *Schema, array []float64, str string) (found bool, err error)
- func ContainsInt(s *Schema, array []int, str string) (found bool, err error)
- func ContainsString(s *Schema, array []string, str string) (found bool, err error)
- func ContainsUint(s *Schema, array []uint, str string) (found bool, err error)
- func Email(s *Schema, str string) (value string, err error)
- func Float32(s *Schema, str string) (value float32, err error)
- func Float64(s *Schema, str string) (value float64, err error)
- func Float64Slice(s *Schema, array []string) (values []float64, err error)
- func Hexadecimal(s *Schema, str string) (value int64, err error)
- func IP(s *Schema, str string) (value string, err error)
- func Int(s *Schema, str string) (value int, err error)
- func Int16(s *Schema, str string) (value int16, err error)
- func Int32(s *Schema, str string) (value int32, err error)
- func Int64(s *Schema, str string) (value int64, err error)
- func Int8(s *Schema, str string) (value int8, err error)
- func IntSlice(s *Schema, array []string) (values []int, err error)
- func LuhnChecksum(s *Schema, str string) (value string, err error)
- func MAC(s *Schema, str string) (value string, err error)
- func Pattern(s *Schema, expr, str string) (value string, err error)
- func SetBoolStrings(m map[string]bool)
- func String(s *Schema, str string) (value string, err error)
- func StringSlice(s *Schema, array []string) (values []string, err error)
- func Time(s *Schema, str string) (value time.Time, err error)
- func URL(s *Schema, str string) (value string, err error)
- func Uint(s *Schema, str string) (value uint, err error)
- func Uint16(s *Schema, str string) (value uint16, err error)
- func Uint32(s *Schema, str string) (value uint32, err error)
- func Uint64(s *Schema, str string) (value uint64, err error)
- func Uint8(s *Schema, str string) (value uint8, err error)
- func UintSlice(s *Schema, array []string) (values []uint, err error)
- type Checker
- type ICANNError
- type PatternError
- type Schema
- func (s *Schema) DataType() datautil.Type
- func (s *Schema) PostCheck(value interface{}) error
- func (s *Schema) PreCheck(str string, typ datautil.Type) (string, error)
- func (s *Schema) SetChecker(flag Checker) *Schema
- func (s *Schema) SetMax(n interface{}) *Schema
- func (s *Schema) SetMin(n interface{}) *Schema
- func (s *Schema) SetPatternFmt(fmt string) *Schema
- func (s *Schema) SetRange(min, max interface{}) *Schema
- func (s *Schema) SetTimeFmt(fmt string) *Schema
- type URLSchemeError
- type ValidError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrIPDomain = errors.New("IP address in domain") ErrNoIP = errors.New("no IP address") ErrNoScheme = errors.New(`no protocol scheme (like "http://")`) ErrHTTP_FTP = errors.New("no protocol schemes for http,https,ftp") )
var ( ErrAlpha = errors.New("only letters are allowed (a-zA-Z)") ErrAlphaNum = errors.New("only letters and numbers are allowed (a-zA-Z0-9)") ErrASCII = errors.New("only ASCII characters are allowed") ErrString = errors.New("the value is not a string") )
var ( ErrEmpty = errors.New("empty string") ErrRequired = errors.New("required string") )
var ErrBool = errors.New("not boolean value")
var ErrLuhnChecksum = errors.New("invalid checksum")
Functions ¶
func Bool ¶
Bool checks if the string is a boolean value.
It accepts "1", "t", "true", "y", "yes" for true value, and "0", "f", "false", "n", "no" for false value, in both lower, upper and title cases.
Also, checks extra values set with "SetBoolStrings()".
func CheckDomain ¶
CheckDomain uses the flag C_TLD to checking if the domain is ICANN's and the flag C_DNS to checking if it is mapped to an address.
func ContainsFloat64 ¶
ContainsFloat64 checks if the string str as float64 is within array.
func ContainsInt ¶
ContainsInt checks if the string str as int is within array.
func ContainsString ¶
ContainsString checks if the string str is within array.
func ContainsUint ¶
ContainsUint checks if the string str as uint is within array.
func Email ¶
Email checks if the string is an email address correctly parsed and returns the address part. Uses the flag C_TLD to checking the domain and C_DNS for the DNS MX records.
func Float64Slice ¶
Float64Slice checks if each array element is a float64.
func Hexadecimal ¶
Hexadecimal checks if the string is an hexadecimal value and returns the corresponding value.
func IP ¶
IP checks if the string is an IP address. Uses the flag C_TLD to checking the domain by a reverse lookup.
func Int ¶
Int checks if the string is an int.
Example ¶
package main import ( "fmt" "github.com/kless/datautil/valid" ) var schema = valid.NewSchema(valid.C_Required | valid.M_TrimSpace) func main() { dst, err := valid.Int(schema, "666") fmt.Printf("%d : %v\n", dst, err) schema.SetRange(6, 66) dst, err = valid.Int(schema, "5") fmt.Printf("%d : %v\n", dst, err) dst, err = valid.Int(schema, "67") fmt.Printf("%d : %v\n", dst, err) }
Output: 666 : <nil> 5 : minimum length: 6 67 : maximum length: 66
func LuhnChecksum ¶
LuhnChecksum validates a variety of identification numbers, such as credit card numbers and IMEI numbers.
func Pattern ¶
Pattern checks if the string matches a regular expression.
Internally, uses a cache to store every new compiled regular expression.
Example ¶
package main import ( "fmt" "github.com/kless/datautil/valid" ) var schema = valid.NewSchema(valid.C_Required | valid.M_TrimSpace) func main() { schema.SetChecker(0) re, input := `[a-z]+`, "FOO" dst, err := valid.Pattern(schema, re, input) fmt.Printf("%q : %v\n", dst, err) schema.SetPatternFmt("letters in lowercase") dst, err = valid.Pattern(schema, re, input) fmt.Printf("%q : %v\n", dst, err) }
Output: "" : valid.Pattern: parsing "FOO": does not matches with expression: `[a-z]+` "" : valid.Pattern: parsing "FOO": does not matches with format: letters in lowercase
func SetBoolStrings ¶
SetBoolStrings sets the extra strings for parsing boolean values in strings. For every key, it is also added in upper and title case.
func String ¶
String checks if the string is not a numeric value. Returns an error if it is not a string.
Uses the flags C_StrictString, C_ASCII, C_Alpha and C_Alphanumeric.
Example ¶
package main import ( "fmt" "github.com/kless/datautil/valid" ) var schema = valid.NewSchema(valid.C_Required | valid.M_TrimSpace) func main() { dst, err := valid.String(schema, " foo ") fmt.Printf("%q : %v\n", dst, err) dst, err = valid.String(schema, "") fmt.Printf("%q : %v\n", dst, err) schema.Bydefault = "bar" dst, err = valid.String(schema, "") fmt.Printf("%q : %v\n", dst, err) schema.SetChecker(valid.M_ToUppercase) dst, err = valid.String(schema, " ja") fmt.Printf("%q : %v\n", dst, err) }
Output: "foo" : <nil> "" : required string "bar" : <nil> " JA" : <nil>
func StringSlice ¶
StringSlice checks if each array element is a string.
func URL ¶
URL checks if the string is an URL and returns a valid URL string. Uses the flags C_HTTP_FTP to checking the protocol schemes for HTTP and FTP, C_TLD for the domain, and C_DNS for the DNS records.
Types ¶
type Checker ¶
type Checker uint16
Checker represents the check-ups and modifications for a string.
const ( C_Required Checker = 1 << iota // Required; can not be empty M_TrimSpace // Remove all leading and trailing space M_ToLowercase // Convert to lower case M_ToUppercase // Convert to upper case // For type String C_StrictString // Strict checking; no numbers in strings C_Alpha // Check if the string contains only letters (a-zA-Z) C_Alphanumeric // Check if the string contains only letters and numbers C_ASCII // Check if the string contains only ASCII characters C_HTTP_FTP // Check protocol scheme in type URL C_DNS // Check DNS C_TLD // Check Top Level Domain )
These flags define the check-ups and modification for the input string.
type ICANNError ¶
type ICANNError string
func (ICANNError) Error ¶
func (e ICANNError) Error() string
type PatternError ¶
type PatternError struct {
// contains filtered or unexported fields
}
func (*PatternError) Error ¶
func (e *PatternError) Error() string
type Schema ¶
type Schema struct { Bydefault string // Value used by default in empty strings IsSlice bool // contains filtered or unexported fields }
A Schema identifies a validation schema.
func NewSchema ¶
NewSchema returns a new schema with the given string Checkers and sets "time.RFC3339" for the time format.
func (*Schema) PostCheck ¶
PostCheck checks the length according to the flags c_MinLen and c_MaxLen.
func (*Schema) PreCheck ¶
PreCheck checks and modifies the string according to the flags C_Required, M_TrimSpace, M_ToLowercase and M_ToUppercase.
func (*Schema) SetChecker ¶
SetChecker sets the Checker flags.
func (*Schema) SetMax ¶
SetMax sets the checking for the maximum length of a string, or the maximum value of a numeric type. The valid types for the aregument are: int, float64.
func (*Schema) SetMin ¶
SetMin sets the checking for the minimum length of a string, or the minimum value of a numeric type. The valid types for the aregument are: int, float64.
func (*Schema) SetPatternFmt ¶
SetPatternFmt sets the message about regular expression to show when the pattern is not matched.
func (*Schema) SetRange ¶
SetRange sets the checking for the minimum and maximum lengths of a string, or the minimum and maximum values of a numeric type. The valid types for the areguments are: int, float64.
func (*Schema) SetTimeFmt ¶
SetTimeFmt sets the time format.
type URLSchemeError ¶
type URLSchemeError []string
func (URLSchemeError) Error ¶
func (e URLSchemeError) Error() string
type ValidError ¶
type ValidError struct { Func string // the failing function Str string // the input Err error // the reason the validation failed }
A ValidError records a failed validation.
func (*ValidError) Error ¶
func (e *ValidError) Error() string