validation

package
v0.0.0-...-1676a5e Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2020 License: MIT, MIT Imports: 18 Imported by: 0

Documentation

Overview

Package validation contains configurations for validating payload and path variable input.

Index

Constants

View Source
const (
	STRING_MAX_LENGTH   = 2000.0
	PASSWORD_MIN_LENGTH = 8.0
	ALPHABET            = "abcdefghijklmnopqrstuvwxyz"
	NUMERIC             = "0123456789"
	USERNAME_CHARS      = ALPHABET + "_" + NUMERIC

	// mocking
	CONST_MOCK_EMAIL = "user@examplemail.com"
)
View Source
const (
	ERR_INVALID_CHARS = "PARAM CONTENTS INVALID"
	ERR_RANGE_EXCEED  = "PARAM VALUE IS OUT OF RANGE BOUNDS"
	ERR_RANGE_NAN     = "PARAM VALUE IS NaN"

	ERR_NOT_OBJECT  = "PARAM IS NOT AN OBJECT"
	ERR_NOT_ARRAY   = "PARAM IS NOT AN ARRAY"
	ERR_NOT_STRING  = "PARAM IS NOT A STRING"
	ERR_NOT_INT     = "PARAM IS NOT AN INT"
	ERR_NOT_INT64   = "PARAM IS NOT AN INT64"
	ERR_NOT_FLOAT64 = "PARAM IS NOT A FLOAT64"
	ERR_NOT_BOOL    = "PARAM IS NOT A BOOLEAN"

	ERR_PARSE_INT64   = "PARAM FAILED TO PARSE AS INT64"
	ERR_PARSE_FLOAT64 = "PARAM FAILED TO PARSE AS FLOAT64"
	ERR_PARSE_TIME    = "PARAM FAILED TO PARSE AS TIME"
	ERR_PARSE_URL     = "PARAM FAILED TO PARSE AS URL"
)
View Source
const (
	COUNTRY_CSV = `` /* 21763-byte string literal not displayed */

)

Variables

This section is empty.

Functions

func CountriesISO2

func CountriesISO2() map[string]*Country

CountriesISO2 returns the map of alpha-2 codes to country structs.

func CountriesISO3

func CountriesISO3() map[string]*Country

CountriesISO3 returns the map of alpha-3 codes to country structs.

func Hash256

func Hash256(input string) string

Hash256 is a tool to exec the SHA25 on strings, taking a string input, and outputting hex.

func IsAlpha

func IsAlpha(s string) (bool, string)

IsAlpha checkes whether a string is alphabetic.

func IsAlphanumeric

func IsAlphanumeric(s string) (bool, string)

IsAlphanumeric checks if a string is alphanumeric.

func Languages

func Languages() map[string]*Language

Languages returns a map of alpha-2 lanugage codes to language structs.

func ReverseCountries

func ReverseCountries() map[string]*Country

ReverseCountries maps the country names to the country structs.

func Sanitize

func Sanitize(s string) string

Sanitize is used for string inputs to ensure some basic safety from injectioms or whatever (see bluemonday).

Types

type Array

type Array []interface{}

type BodyValidationFunction

type BodyValidationFunction func(http.Request, interface{}) (*http.Status, interface{})

type Config

type Config struct {
	Model            interface{}            `json:"model"`
	Type             string                 `json:"type"`
	PathFunction     PathValidationFunction `json:"-"`
	BodyFunction     BodyValidationFunction `json:"-"`
	Keys             []string               `json:"-"`
	Min              float64                `json:"min"`
	Max              float64                `json:"max"`
	RequiredValue    bool                   `json:"required"`
	SummaryValue     string                 `json:"summary"`
	DefaultValue     interface{}            `json:"default"`
	DescriptionValue string                 `json:"description"`
}

func ArrayFloat64

func ArrayFloat64() *Config

ArrayFloat64 returns a validation object that checks for a slice of float64

func ArrayInt

func ArrayInt() *Config

ArrayInt returns a validation object that checks for a slice of integers

func ArrayInterface

func ArrayInterface() *Config

ArrayInterface returns a validation object for request body that checks a property to see if it's an array

func ArrayString

func ArrayString() *Config

ArrayString returns a validation object that allows any value

func Base64Decode

func Base64Decode() *Config

Base64Decode returns a validation object which checks for valid username

func Bool

func Bool() *Config

Bool returns a validation object that checks for a bool which parses correctly

func CountryISO2

func CountryISO2() *Config

CountryISO2 returns a validation object that checks to see if it can resolve to a country struct

func CountryISO3

func CountryISO3() *Config

CountryISO3 returns a validation object that checks to see if it can resolve to a country struct

func EmailAddress

func EmailAddress() *Config

EmailAddress returns a validation object which checks for valid email address

func EmailAddressOptional

func EmailAddressOptional() *Config

Returns a validation object which checks for valid email address

func Float64

func Float64(ranges ...int) *Config

Float64 returns a validation object that checks for a float64 which parses correctly

func Hex

func Hex(min, max float64) *Config

Hex returns a validation object that checks for a hex string with a length within optional range

func HexDecode

func HexDecode(min, max float64) *Config

Returns a validation object that decodes a hex string with a length within optional range

func HexSHA1

func HexSHA1() *Config

Returns a validation object that checks for a hex string with length of 40 chars.

func HexSHA256

func HexSHA256() *Config

Returns a validation object that checks for a hex string with length of 64 chars.

func IPv4Address

func IPv4Address() *Config

IPv4Address returns a validation object that checks for a string with a length within optional range

func Int

func Int() *Config

Int returns a validation object that checks for an int which parses correctly

func Int64

func Int64() *Config

Int64 returns a validation object that checks for an int64 which parses correctly

func IntOptimistic

func IntOptimistic() *Config

Returns a validation object that checks for an int which parses correctly and is zero or above

func IntPessimistic

func IntPessimistic() *Config

Returns a validation object that checks for an int which parses correctly and is zero or lower

func Interface

func Interface() *Config

Interface returns a validation object that allows any value

func Json

func Json() *Config

Json returns a validation object which checks for (in)valid json

func LanguageISO2

func LanguageISO2() *Config

LanguageISO2 returns a validation object that checks to see if it can resolve to a country struct

func MapStringInterface

func MapStringInterface() *Config

MapStringInterface returns a validation object for request body that checks a property to see if it's an object

func NegativeInt

func NegativeInt() *Config

Returns a validation object that checks for an int which parses correctly and is negative

func NewConfig

func NewConfig(validationType interface{}, pathFunction PathValidationFunction, bodyFunction BodyValidationFunction, ranges ...float64) *Config

NewConfig creates a new validation config with the supplied parameters.

func Now

func Now() *Config

Now returns a validation object which outputs the current time

func PasswordHard

func PasswordHard() *Config

Returns a validation object which checks for password

func PasswordWeak

func PasswordWeak() *Config

PasswordWeak returns a validation object which checks for password

func PhoneNumber

func PhoneNumber(countryCode string) *Config

PhoneNumber returns a validation object that checks to see if a valid phone number is provided

func PositiveInt

func PositiveInt() *Config

Returns a validation object that checks for an int which parses correctly and is positive

func SQLTimestamp

func SQLTimestamp() *Config

SQLTimestamp returns a validation object which checks for valid time

func Slice

func Slice(vc *Config) *Config

Slice returns a validation object that checks for a slice of integers

func String

func String(min, max float64) *Config

String returns a validation object that checks for a string with a length within optional range

func StringExact

func StringExact(max float64) *Config

StringExact returns a validation object that checks for a string with exact length

func StringSet

func StringSet(set ...string) *Config

StringSet returns a validation object which ensures string is whitelisted

func StringSplit

func StringSplit(delimiter string) *Config

StringSplit returns a validation object which checks for delimiter-separated string like CSV

func Time

func Time(layout string) *Config

Time returns a validation object which checks for valid time like 2017-06-23T00:00:00.000Z

func URL

func URL() *Config

Url returns a validation object which checks for valid url

func UUIDv4

func UUIDv4() *Config

UUIDv4 returns a validation object that checks for a valid UUID version 4

func Username

func Username(min, max float64) *Config

Username returns a validation object which checks for valid username

func (*Config) Default

func (vc *Config) Default(x interface{}) *Config

Default adds a default value to the config

func (*Config) Description

func (vc *Config) Description(x string) *Config

Description adds a description to the config

func (*Config) Expecting

func (vc *Config) Expecting() string

Expecting constructs a string that indicates what the required values were.

func (*Config) Key

func (vc *Config) Key() string

Key returns the first key associated with the path parameter

func (*Config) KeyJoin

func (vc *Config) KeyJoin(delim string) string

KeyJoin joins the keys that are accocuated to a validation config, and concatonates them.

func (*Config) Required

func (vc *Config) Required() *Config

Required signifies that the field is 'required'

func (*Config) Summary

func (vc *Config) Summary(x string) *Config

Summary adds a description to the config

type Country

type Country struct {
	Name       string  `json:"name"`
	Alpha2Code string  `json:"code"`
	Alpha3Code string  `json:"code"`
	Lat        float64 `json:"lat"`
	Lng        float64 `json:"lng"`
	Lang       string  `json:"lang"`
}

type IPv4

type IPv4 [4]int

func (IPv4) String

func (ipv4 IPv4) String() string

type JSON

type JSON struct{}

type Language

type Language struct {
	Code    string            `json:"code"`
	Native  string            `json:"native"`
	Names   map[string]string `json:"names"`
	Reverse bool              `json:"reversed"`
}

func Lang

func Lang(code, native string, rev bool) *Language

type Object

type Object map[string]interface{}

type Optional

type Optional map[string]*Config

type Patch

type Patch map[string]*Config

type PathValidationFunction

type PathValidationFunction func(http.Request, string) (*http.Status, interface{})

type Payload

type Payload map[string]*Config

func (Payload) WithFields

func (payload Payload) WithFields(fields Payload) Payload

WithFields does something, probably deprecated.

type Spec

type Spec struct {
	Type string
	Keys []string
}

Jump to

Keyboard shortcuts

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