valid

package
v0.0.0-...-58e9c6f Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2014 License: MIT Imports: 14 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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")
)
View Source
var (
	ErrEmpty    = errors.New("empty string")
	ErrRequired = errors.New("required string")
)
View Source
var ErrBool = errors.New("not boolean value")
View Source
var ErrLuhnChecksum = errors.New("invalid checksum")

Functions

func Base32

func Base32(s *Schema, str string) (value string, err error)

Base32 checks if the string is encoded in base32 and returns the value decoded.

func Base64

func Base64(s *Schema, str string) (value string, err error)

Base64 checks if the string is encoded in base64 and returns the value decoded.

func Bool

func Bool(s *Schema, str string) (value bool, err error)

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

func CheckDomain(flag Checker, domain string, forEmail bool) (err error)

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

func ContainsFloat64(s *Schema, array []float64, str string) (found bool, err error)

ContainsFloat64 checks if the string str as float64 is within array.

func ContainsInt

func ContainsInt(s *Schema, array []int, str string) (found bool, err error)

ContainsInt checks if the string str as int is within array.

func ContainsString

func ContainsString(s *Schema, array []string, str string) (found bool, err error)

ContainsString checks if the string str is within array.

func ContainsUint

func ContainsUint(s *Schema, array []uint, str string) (found bool, err error)

ContainsUint checks if the string str as uint is within array.

func Email

func Email(s *Schema, str string) (value string, err error)

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 Float32

func Float32(s *Schema, str string) (value float32, err error)

Float32 checks if the string is a float32.

func Float64

func Float64(s *Schema, str string) (value float64, err error)

Float64 checks if the string is a float64.

func Float64Slice

func Float64Slice(s *Schema, array []string) (values []float64, err error)

Float64Slice checks if each array element is a float64.

func Hexadecimal

func Hexadecimal(s *Schema, str string) (value int64, err error)

Hexadecimal checks if the string is an hexadecimal value and returns the corresponding value.

func IP

func IP(s *Schema, str string) (value string, err error)

IP checks if the string is an IP address. Uses the flag C_TLD to checking the domain by a reverse lookup.

func Int

func Int(s *Schema, str string) (value int, err error)

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 Int16

func Int16(s *Schema, str string) (value int16, err error)

Int16 checks if the string is an int16.

func Int32

func Int32(s *Schema, str string) (value int32, err error)

Int32 checks if the string is an int32.

func Int64

func Int64(s *Schema, str string) (value int64, err error)

Int64 checks if the string is an int64.

func Int8

func Int8(s *Schema, str string) (value int8, err error)

Int8 checks if the string is an int8.

func IntSlice

func IntSlice(s *Schema, array []string) (values []int, err error)

IntSlice checks if each array element is an int.

func LuhnChecksum

func LuhnChecksum(s *Schema, str string) (value string, err error)

LuhnChecksum validates a variety of identification numbers, such as credit card numbers and IMEI numbers.

https://en.wikipedia.org/wiki/Luhn_algorithm

func MAC

func MAC(s *Schema, str string) (value string, err error)

MAC checks if the string is a hardware address.

func Pattern

func Pattern(s *Schema, expr, str string) (value string, err error)

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

func SetBoolStrings(m map[string]bool)

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

func String(s *Schema, str string) (value string, err error)

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

func StringSlice(s *Schema, array []string) (values []string, err error)

StringSlice checks if each array element is a string.

func Time

func Time(s *Schema, str string) (value time.Time, err error)

Time checks if the string is a time.

func URL

func URL(s *Schema, str string) (value string, err error)

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.

func Uint

func Uint(s *Schema, str string) (value uint, err error)

Uint checks if the string is an uint.

func Uint16

func Uint16(s *Schema, str string) (value uint16, err error)

Uint16 checks if the string is an uint16.

func Uint32

func Uint32(s *Schema, str string) (value uint32, err error)

Uint32 checks if the string is an uint32.

func Uint64

func Uint64(s *Schema, str string) (value uint64, err error)

Uint64 checks if the string is an uint64.

func Uint8

func Uint8(s *Schema, str string) (value uint8, err error)

Uint8 checks if the string is an uint8.

func UintSlice

func UintSlice(s *Schema, array []string) (values []uint, err error)

UintSlice checks if each array element is an uint.

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

func NewSchema(flag Checker) *Schema

NewSchema returns a new schema with the given string Checkers and sets "time.RFC3339" for the time format.

func (*Schema) DataType

func (s *Schema) DataType() datautil.Type

DataType returns the data type.

func (*Schema) PostCheck

func (s *Schema) PostCheck(value interface{}) error

PostCheck checks the length according to the flags c_MinLen and c_MaxLen.

func (*Schema) PreCheck

func (s *Schema) PreCheck(str string, typ datautil.Type) (string, error)

PreCheck checks and modifies the string according to the flags C_Required, M_TrimSpace, M_ToLowercase and M_ToUppercase.

func (*Schema) SetChecker

func (s *Schema) SetChecker(flag Checker) *Schema

SetChecker sets the Checker flags.

func (*Schema) SetMax

func (s *Schema) SetMax(n interface{}) *Schema

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

func (s *Schema) SetMin(n interface{}) *Schema

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

func (s *Schema) SetPatternFmt(fmt string) *Schema

SetPatternFmt sets the message about regular expression to show when the pattern is not matched.

func (*Schema) SetRange

func (s *Schema) SetRange(min, max interface{}) *Schema

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

func (s *Schema) SetTimeFmt(fmt string) *Schema

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

Jump to

Keyboard shortcuts

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