it

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package it contains validation constraints that are used to validate specific types of values.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEmail added in v0.2.0

IsEmail is used for simplified validation of an email address. It allows all values with an "@" symbol in, and a "." in the second host part of the email address.

Example (InvalidEmail)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "user example.com"
	err := validator.ValidateString(&v, it.IsEmail())
	fmt.Println(err)
}
Output:

violation: This value is not a valid email address.
Example (ValidEmail)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "user@example.com"
	err := validator.ValidateString(&v, it.IsEmail())
	fmt.Println(err)
}
Output:

<nil>

func IsHTML5Email added in v0.2.0

func IsHTML5Email() validation.CustomStringConstraint

IsHTML5Email is used for validation of an email address based on pattern for HTML5 (see https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address).

Example (InvalidEmail)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "@example.com"
	err := validator.ValidateString(&v, it.IsEmail())
	fmt.Println(err)
}
Output:

violation: This value is not a valid email address.
Example (ValidEmail)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "{}~!@example.com"
	err := validator.ValidateString(&v, it.IsEmail())
	fmt.Println(err)
}
Output:

<nil>

func IsHostname added in v0.2.0

IsHostname validates that a value is a valid hostname. It checks that:

  • each label within a valid hostname may be no more than 63 octets long;
  • the total length of the hostname must not exceed 255 characters;
  • hostname is fully qualified and include its top-level domain name (for instance, example.com is valid but example is not);
  • checks for reserved top-level domains according to RFC 2606 (hostnames containing them are not considered valid: .example, .invalid, .localhost, and .test).

If you do not want to check for top-level domains use IsLooseHostname version of constraint.

Example (InvalidHostname)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "example-.com"
	err := validator.ValidateString(&v, it.IsHostname())
	fmt.Println(err)
}
Output:

violation: This value is not a valid hostname.
Example (ReservedHostname)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "example.localhost"
	err := validator.ValidateString(&v, it.IsHostname())
	fmt.Println(err)
}
Output:

violation: This value is not a valid hostname.
Example (ValidHostname)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "example.com"
	err := validator.ValidateString(&v, it.IsHostname())
	fmt.Println(err)
}
Output:

<nil>

func IsJSON added in v0.2.0

IsJSON validates that a value is a valid JSON.

Example (InvalidJSON)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := `"invalid": true`
	err := validator.ValidateString(&v, it.IsJSON())
	fmt.Println(err)
}
Output:

violation: This value should be valid JSON.
Example (ValidJSON)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := `{"valid": true}`
	err := validator.ValidateString(&v, it.IsJSON())
	fmt.Println(err)
}
Output:

<nil>

func IsLooseHostname added in v0.2.0

func IsLooseHostname() validation.CustomStringConstraint

IsLooseHostname validates that a value is a valid hostname. It checks that:

  • each label within a valid hostname may be no more than 63 octets long;
  • the total length of the hostname must not exceed 255 characters.
Example (InvalidHostname)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "example-.com"
	err := validator.ValidateString(&v, it.IsLooseHostname())
	fmt.Println(err)
}
Output:

violation: This value is not a valid hostname.
Example (ReservedHostname)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "example.localhost"
	err := validator.ValidateString(&v, it.IsLooseHostname())
	fmt.Println(err)
}
Output:

<nil>
Example (ValidHostname)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "example.com"
	err := validator.ValidateString(&v, it.IsLooseHostname())
	fmt.Println(err)
}
Output:

<nil>

Types

type BlankConstraint

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

BlankConstraint checks that a value is blank: equal to false, nil, zero, an empty string, an empty slice, array, or a map.

func IsBlank

func IsBlank() BlankConstraint

IsBlank creates a BlankConstraint for checking that value is empty.

Example

s := "foo"
err := validator.ValidateString(&s, it.IsBlank())

func (BlankConstraint) Message

func (c BlankConstraint) Message(message string) BlankConstraint

Message sets the violation message template.

func (BlankConstraint) Name

func (c BlankConstraint) Name() string

Name is the constraint name.

func (BlankConstraint) SetUp

func (c BlankConstraint) SetUp() error

SetUp always returns no error.

func (BlankConstraint) ValidateBool

func (c BlankConstraint) ValidateBool(value *bool, scope validation.Scope) error

func (BlankConstraint) ValidateCountable

func (c BlankConstraint) ValidateCountable(count int, scope validation.Scope) error

func (BlankConstraint) ValidateIterable

func (c BlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error

func (BlankConstraint) ValidateNil

func (c BlankConstraint) ValidateNil(scope validation.Scope) error

func (BlankConstraint) ValidateNumber

func (c BlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error

func (BlankConstraint) ValidateString

func (c BlankConstraint) ValidateString(value *string, scope validation.Scope) error

func (BlankConstraint) ValidateTime

func (c BlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error

func (BlankConstraint) When

func (c BlankConstraint) When(condition bool) BlankConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type BoolConstraint

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

BoolConstraint checks that a bool value in strictly equal to expected bool value.

func IsFalse

func IsFalse() BoolConstraint

IsFalse creates a BoolConstraint to check that a value is not strictly equal to false.

Example

var b *bool
err := validator.ValidateBool(b, it.IsFalse())

func IsTrue

func IsTrue() BoolConstraint

IsTrue creates a BoolConstraint to check that a value is not strictly equal to true.

Example

var b *bool
err := validator.ValidateBool(b, it.IsTrue())

func (BoolConstraint) Message

func (c BoolConstraint) Message(message string) BoolConstraint

Message sets the violation message template.

func (BoolConstraint) Name

func (c BoolConstraint) Name() string

Name is the constraint name.

func (BoolConstraint) SetUp

func (c BoolConstraint) SetUp() error

SetUp always returns no error.

func (BoolConstraint) ValidateBool

func (c BoolConstraint) ValidateBool(value *bool, scope validation.Scope) error

func (BoolConstraint) When

func (c BoolConstraint) When(condition bool) BoolConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type ChoiceConstraint

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

ChoiceConstraint is used to ensure that the given value corresponds to one of the expected choices.

func IsOneOfStrings

func IsOneOfStrings(values ...string) ChoiceConstraint

IsOneOfStrings creates a ChoiceConstraint for checking that values are in the expected list of strings.

Example

err := validator.ValidateString(&s, it.IsOneOfStrings("one", "two", "three"))

func (ChoiceConstraint) Message

func (c ChoiceConstraint) Message(message string) ChoiceConstraint

Message sets the violation message template. You can use template parameters for injecting its values into the final message:

{{ choices }} - a comma-separated list of available choices;
{{ value }} - the current (invalid) value.

func (ChoiceConstraint) Name

func (c ChoiceConstraint) Name() string

Name is the constraint name.

func (ChoiceConstraint) SetUp

func (c ChoiceConstraint) SetUp() error

SetUp will return an error if the list of choices is empty.

func (ChoiceConstraint) ValidateString

func (c ChoiceConstraint) ValidateString(value *string, scope validation.Scope) error

func (ChoiceConstraint) When

func (c ChoiceConstraint) When(condition bool) ChoiceConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type CountConstraint

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

CountConstraint checks that a given collection's (array, slice or a map) length is between some minimum and maximum value.

func HasCountBetween

func HasCountBetween(min int, max int) CountConstraint

HasCountBetween creates a CountConstraint that checks the length of the iterable (slice, array, or map) is between some minimum and maximum value.

Example

v := []int{1, 2}
err := validator.ValidateIterable(v, it.HasCountBetween(3, 10))

func HasExactCount

func HasExactCount(count int) CountConstraint

HasExactCount creates a CountConstraint that checks the length of the iterable (slice, array, or map) has exact value.

Example

v := []int{1, 2}
err := validator.ValidateIterable(v, it.HasExactCount(3))

func HasMaxCount

func HasMaxCount(max int) CountConstraint

HasMaxCount creates a CountConstraint that checks the length of the iterable (slice, array, or map) is less than the maximum value.

Example

v := []int{1, 2}
err := validator.ValidateIterable(v, it.HasMaxCount(1))

func HasMinCount

func HasMinCount(min int) CountConstraint

HasMinCount creates a CountConstraint that checks the length of the iterable (slice, array, or map) is greater than the minimum value.

Example

v := []int{1, 2}
err := validator.ValidateIterable(v, it.HasMinCount(3))

func (CountConstraint) ExactMessage

func (c CountConstraint) ExactMessage(message string) CountConstraint

ExactMessage sets the violation message that will be shown if minimum and maximum values are equal and the length of the collection is not exactly this value.

func (CountConstraint) MaxMessage

func (c CountConstraint) MaxMessage(message string) CountConstraint

MaxMessage sets the violation message that will be shown if the collection length is greater than the maximum value.

func (CountConstraint) MinMessage

func (c CountConstraint) MinMessage(message string) CountConstraint

MinMessage sets the violation message that will be shown if the collection length is less than the minimum value.

func (CountConstraint) Name

func (c CountConstraint) Name() string

Name is the constraint name.

func (CountConstraint) SetUp

func (c CountConstraint) SetUp() error

SetUp always returns no error.

func (CountConstraint) ValidateCountable

func (c CountConstraint) ValidateCountable(count int, scope validation.Scope) error

func (CountConstraint) ValidateIterable

func (c CountConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error

func (CountConstraint) When

func (c CountConstraint) When(condition bool) CountConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type IPConstraint added in v0.2.0

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

IPConstraint is used to validate IP address. You can check for different versions and restrict some ranges by additional options.

func IsIP added in v0.2.0

func IsIP() IPConstraint

IsIP creates an IPConstraint to validate an IP address (IPv4 or IPv6).

Example (InvalidIP)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "123.123.123.345"
	err := validator.ValidateString(&v, it.IsIP())
	fmt.Println(err)
}
Output:

violation: This is not a valid IP address.
Example (ValidIP)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "123.123.123.123"
	err := validator.ValidateString(&v, it.IsIP())
	fmt.Println(err)
}
Output:

<nil>

func IsIPv4 added in v0.2.0

func IsIPv4() IPConstraint

IsIPv4 creates an IPConstraint to validate an IPv4 address.

Example (InvalidIP)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "123.123.123.345"
	err := validator.ValidateString(&v, it.IsIPv4())
	fmt.Println(err)
}
Output:

violation: This is not a valid IP address.
Example (ValidIP)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "123.123.123.123"
	err := validator.ValidateString(&v, it.IsIPv4())
	fmt.Println(err)
}
Output:

<nil>

func IsIPv6 added in v0.2.0

func IsIPv6() IPConstraint

IsIPv6 creates an IPConstraint to validate an IPv4 address.

Example (InvalidIP)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "z001:0db8:85a3:0000:0000:8a2e:0370:7334"
	err := validator.ValidateString(&v, it.IsIPv6())
	fmt.Println(err)
}
Output:

violation: This is not a valid IP address.
Example (ValidIP)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
	err := validator.ValidateString(&v, it.IsIPv6())
	fmt.Println(err)
}
Output:

<nil>

func (IPConstraint) DenyIP added in v0.2.0

func (c IPConstraint) DenyIP(restrict func(ip net.IP) bool) IPConstraint

DenyIP can be used to deny custom range of IP addresses.

Example
package main

import (
	"fmt"
	"net"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "127.0.0.1"
	err := validator.ValidateString(&v, it.IsIP().DenyIP(func(ip net.IP) bool {
		return ip.IsLoopback()
	}))
	fmt.Println(err)
}
Output:

violation: This IP address is prohibited to use.

func (IPConstraint) DenyPrivateIP added in v0.2.0

func (c IPConstraint) DenyPrivateIP() IPConstraint

DenyPrivateIP denies using of private IPs according to RFC 1918 (IPv4 addresses) and RFC 4193 (IPv6 addresses).

Example (RestrictedPrivateIPv4)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "192.168.1.0"
	err := validator.ValidateString(&v, it.IsIP().DenyPrivateIP())
	fmt.Println(err)
}
Output:

violation: This IP address is prohibited to use.
Example (RestrictedPrivateIPv6)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c"
	err := validator.ValidateString(&v, it.IsIPv6().DenyPrivateIP())
	fmt.Println(err)
}
Output:

violation: This IP address is prohibited to use.

func (IPConstraint) InvalidMessage added in v0.2.0

func (c IPConstraint) InvalidMessage(message string) IPConstraint

InvalidMessage sets the violation message template for invalid IP case. You can use template parameters for injecting its values into the final message:

{{ value }} - the current (invalid) value.

func (IPConstraint) Name added in v0.2.0

func (c IPConstraint) Name() string

Name is the constraint name.

func (IPConstraint) ProhibitedMessage added in v0.2.0

func (c IPConstraint) ProhibitedMessage(message string) IPConstraint

ProhibitedMessage sets the violation message template for prohibited IP case. You can use template parameters for injecting its values into the final message:

{{ value }} - the current (invalid) value.

func (IPConstraint) SetUp added in v0.2.0

func (c IPConstraint) SetUp() error

SetUp always returns no error.

func (IPConstraint) ValidateString added in v0.2.0

func (c IPConstraint) ValidateString(value *string, scope validation.Scope) error

func (IPConstraint) When added in v0.2.0

func (c IPConstraint) When(condition bool) IPConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type LengthConstraint

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

LengthConstraint checks that a given string length is between some minimum and maximum value. If you want to check the length of the array, slice or a map use CountConstraint.

func HasExactLength

func HasExactLength(count int) LengthConstraint

HasExactLength creates a LengthConstraint that checks the length of the string has exact value.

Example

v := "foo"
err := validator.ValidateString(&v, it.HasExactLength(5))

func HasLengthBetween

func HasLengthBetween(min int, max int) LengthConstraint

HasLengthBetween creates a LengthConstraint that checks the length of the string is between some minimum and maximum value.

Example

v := "foo"
err := validator.ValidateString(&v, it.HasLengthBetween(5, 10))

func HasMaxLength

func HasMaxLength(max int) LengthConstraint

HasMaxLength creates a LengthConstraint that checks the length of the string is less than the maximum value.

Example

v := "foo"
err := validator.ValidateString(&v, it.HasMaxLength(2))

func HasMinLength

func HasMinLength(min int) LengthConstraint

HasMinLength creates a LengthConstraint that checks the length of the string is greater than the minimum value.

Example

v := "foo"
err := validator.ValidateString(&v, it.HasMinLength(5))

func (LengthConstraint) ExactMessage

func (c LengthConstraint) ExactMessage(message string) LengthConstraint

ExactMessage sets the violation message that will be shown if minimum and maximum values are equal and the length of the string is not exactly this value.

func (LengthConstraint) MaxMessage

func (c LengthConstraint) MaxMessage(message string) LengthConstraint

MaxMessage sets the violation message that will be shown if the string length is greater than the maximum value.

func (LengthConstraint) MinMessage

func (c LengthConstraint) MinMessage(message string) LengthConstraint

MinMessage sets the violation message that will be shown if the string length is less than the minimum value.

func (LengthConstraint) Name

func (c LengthConstraint) Name() string

Name is the constraint name.

func (LengthConstraint) SetUp

func (c LengthConstraint) SetUp() error

SetUp always returns no error.

func (LengthConstraint) ValidateString

func (c LengthConstraint) ValidateString(value *string, scope validation.Scope) error

func (LengthConstraint) When

func (c LengthConstraint) When(condition bool) LengthConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type NilConstraint

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

NilConstraint checks that a value in strictly equal to nil. To check that values in blank use BlankConstraint.

func IsNil

func IsNil() NilConstraint

IsNil creates a NilConstraint to check that a value is strictly equal to nil.

Example

var s *string
err := validator.ValidateString(s, it.IsNil())

func (NilConstraint) Message

func (c NilConstraint) Message(message string) NilConstraint

Message sets the violation message template.

func (NilConstraint) Name

func (c NilConstraint) Name() string

Name is the constraint name.

func (NilConstraint) SetUp

func (c NilConstraint) SetUp() error

SetUp always returns no error.

func (NilConstraint) ValidateIterable

func (c NilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error

func (NilConstraint) ValidateNil

func (c NilConstraint) ValidateNil(scope validation.Scope) error

func (NilConstraint) ValidateNumber

func (c NilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error

func (NilConstraint) ValidateString

func (c NilConstraint) ValidateString(value *string, scope validation.Scope) error

func (NilConstraint) ValidateTime

func (c NilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error

func (NilConstraint) When

func (c NilConstraint) When(condition bool) NilConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type NotBlankConstraint

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

NotBlankConstraint checks that a value is not blank: not equal to zero, an empty string, an empty slice/array, an empty map, false or nil. Nil behavior is configurable via AllowNil() method. To check that a value is not nil only use NotNilConstraint.

func IsNotBlank

func IsNotBlank() NotBlankConstraint

IsNotBlank creates a NotBlankConstraint for checking that value is not empty.

Example

s := ""
err := validator.ValidateString(&s, it.IsNotBlank())

func (NotBlankConstraint) AllowNil

AllowNil makes nil values valid.

func (NotBlankConstraint) Message

func (c NotBlankConstraint) Message(message string) NotBlankConstraint

Message sets the violation message template.

func (NotBlankConstraint) Name

func (c NotBlankConstraint) Name() string

Name is the constraint name.

func (NotBlankConstraint) SetUp

func (c NotBlankConstraint) SetUp() error

SetUp always returns no error.

func (NotBlankConstraint) ValidateBool

func (c NotBlankConstraint) ValidateBool(value *bool, scope validation.Scope) error

func (NotBlankConstraint) ValidateCountable

func (c NotBlankConstraint) ValidateCountable(count int, scope validation.Scope) error

func (NotBlankConstraint) ValidateIterable

func (c NotBlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error

func (NotBlankConstraint) ValidateNil

func (c NotBlankConstraint) ValidateNil(scope validation.Scope) error

func (NotBlankConstraint) ValidateNumber

func (c NotBlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error

func (NotBlankConstraint) ValidateString

func (c NotBlankConstraint) ValidateString(value *string, scope validation.Scope) error

func (NotBlankConstraint) ValidateTime

func (c NotBlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error

func (NotBlankConstraint) When

func (c NotBlankConstraint) When(condition bool) NotBlankConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type NotNilConstraint

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

NotNilConstraint checks that a value in not strictly equal to nil. To check that values in not blank use NotBlankConstraint.

func IsNotNil

func IsNotNil() NotNilConstraint

IsNotNil creates a NotNilConstraint to check that a value is not strictly equal to nil.

Example

var s *string
err := validator.ValidateString(s, it.IsNotNil())

func (NotNilConstraint) Message

func (c NotNilConstraint) Message(message string) NotNilConstraint

Message sets the violation message template.

func (NotNilConstraint) Name

func (c NotNilConstraint) Name() string

Name is the constraint name.

func (NotNilConstraint) SetUp

func (c NotNilConstraint) SetUp() error

SetUp always returns no error.

func (NotNilConstraint) ValidateIterable

func (c NotNilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error

func (NotNilConstraint) ValidateNil

func (c NotNilConstraint) ValidateNil(scope validation.Scope) error

func (NotNilConstraint) ValidateNumber

func (c NotNilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error

func (NotNilConstraint) ValidateString

func (c NotNilConstraint) ValidateString(value *string, scope validation.Scope) error

func (NotNilConstraint) ValidateTime

func (c NotNilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error

func (NotNilConstraint) When

func (c NotNilConstraint) When(condition bool) NotNilConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type NumberComparisonConstraint

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

NumberComparisonConstraint is used for various numeric comparisons between integer and float values. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

func IsEqualToFloat

func IsEqualToFloat(value float64) NumberComparisonConstraint

IsEqualToFloat checks that the number (integer or float) is equal to the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1.1
err := validator.ValidateNumber(&v, it.IsEqualToFloat(1.2))

func IsEqualToInteger

func IsEqualToInteger(value int64) NumberComparisonConstraint

IsEqualToInteger checks that the number (integer or float) is equal to the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1
err := validator.ValidateNumber(&v, it.IsEqualToInteger(2))

func IsGreaterThanFloat

func IsGreaterThanFloat(value float64) NumberComparisonConstraint

IsGreaterThanFloat checks that the number (integer or float) is greater than the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1.1
err := validator.ValidateNumber(&v, it.IsGreaterThanFloat(1.1))

func IsGreaterThanInteger

func IsGreaterThanInteger(value int64) NumberComparisonConstraint

IsGreaterThanInteger checks that the number (integer or float) is greater than the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1
err := validator.ValidateNumber(&v, it.IsGreaterThanInteger(1))

func IsGreaterThanOrEqualFloat

func IsGreaterThanOrEqualFloat(value float64) NumberComparisonConstraint

IsGreaterThanOrEqualFloat checks that the number (integer or float) is greater than or equal to the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1.1
err := validator.ValidateNumber(&v, it.IsGreaterThanOrEqualFloat(1.2))

func IsGreaterThanOrEqualInteger

func IsGreaterThanOrEqualInteger(value int64) NumberComparisonConstraint

IsGreaterThanOrEqualInteger checks that the number (integer or float) is greater than or equal to the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1
err := validator.ValidateNumber(&v, it.IsGreaterThanOrEqualInteger(2))

func IsLessThanFloat

func IsLessThanFloat(value float64) NumberComparisonConstraint

IsLessThanFloat checks that the number (integer or float) is less than the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1.1
err := validator.ValidateNumber(&v, it.IsLessThanFloat(1.1))

func IsLessThanInteger

func IsLessThanInteger(value int64) NumberComparisonConstraint

IsLessThanInteger checks that the number (integer or float) is less than the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1
err := validator.ValidateNumber(&v, it.IsLessThanInteger(1))

func IsLessThanOrEqualFloat

func IsLessThanOrEqualFloat(value float64) NumberComparisonConstraint

IsLessThanOrEqualFloat checks that the number (integer or float) is less than or equal to the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1.1
err := validator.ValidateNumber(&v, it.IsLessThanOrEqualFloat(1.2))

func IsLessThanOrEqualInteger

func IsLessThanOrEqualInteger(value int64) NumberComparisonConstraint

IsLessThanOrEqualInteger checks that the number (integer or float) is less than or equal to the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1
err := validator.ValidateNumber(&v, it.IsLessThanOrEqualInteger(2))

func IsNegative

func IsNegative() NumberComparisonConstraint

IsNegative checks that the value is a negative number (integer or float). Zero is neither positive nor negative. If you want to allow zero use IsNegativeOrZero comparison.

Example

v := 1
err := validator.ValidateNumber(&v, it.IsNegative())

func IsNegativeOrZero

func IsNegativeOrZero() NumberComparisonConstraint

IsNegativeOrZero checks that the value is a negative number (integer or float) or equal to zero. If you don't want to allow zero as a valid value, use IsNegative comparison.

Example

v := -1
err := validator.ValidateNumber(&v, it.IsNegativeOrZero())

func IsNotEqualToFloat

func IsNotEqualToFloat(value float64) NumberComparisonConstraint

IsNotEqualToFloat checks that the number (integer or float) is not equal to the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1.1
err := validator.ValidateNumber(&v, it.IsNotEqualToFloat(1.1))

func IsNotEqualToInteger

func IsNotEqualToInteger(value int64) NumberComparisonConstraint

IsNotEqualToInteger checks that the number (integer or float) is not equal to the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1
err := validator.ValidateNumber(&v, it.IsNotEqualToInteger(1))

func IsPositive

func IsPositive() NumberComparisonConstraint

IsPositive checks that the value is a positive number (integer or float). Zero is neither positive nor negative. If you want to allow zero use IsPositiveOrZero comparison.

Example

v := -1
err := validator.ValidateNumber(&v, it.IsPositive())

func IsPositiveOrZero

func IsPositiveOrZero() NumberComparisonConstraint

IsPositiveOrZero checks that the value is a positive number (integer or float) or equal to zero. If you don't want to allow zero as a valid value, use IsPositive comparison.

Example

v := -1
err := validator.ValidateNumber(&v, it.IsPositiveOrZero())

func (NumberComparisonConstraint) Message

Message sets the violation message template. You can use template parameters for injecting its values into the final message:

{{ comparedValue }} - the expected value;
{{ value }} - the current (invalid) value.

func (NumberComparisonConstraint) Name

Name is the constraint name.

func (NumberComparisonConstraint) SetUp

SetUp always returns no error.

func (NumberComparisonConstraint) ValidateNumber

func (c NumberComparisonConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error

func (NumberComparisonConstraint) When

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type RangeConstraint

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

RangeConstraint is used to check that a given number value is between some minimum and maximum. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

func IsBetweenFloats

func IsBetweenFloats(min, max float64) RangeConstraint

IsBetweenFloats checks that the number (integer or float) is between specified minimum and maximum float values. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1.1
err := validator.ValidateNumber(&v, it.IsBetweenFloats(10.111, 20.222))

func IsBetweenIntegers

func IsBetweenIntegers(min, max int64) RangeConstraint

IsBetweenIntegers checks that the number (integer or float) is between specified minimum and maximum integer values. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.

Example

v := 1
err := validator.ValidateNumber(&v, it.IsBetweenIntegers(10, 20))

func (RangeConstraint) Message

func (c RangeConstraint) Message(message string) RangeConstraint

Message sets the violation message template. You can use template parameters for injecting its values into the final message:

{{ max }} - the upper limit;
{{ min }} - the lower limit;
{{ value }} - the current (invalid) value.

func (RangeConstraint) Name

func (c RangeConstraint) Name() string

Name is the constraint name.

func (RangeConstraint) SetUp

func (c RangeConstraint) SetUp() error

SetUp returns an error if min is greater than or equal to max.

func (RangeConstraint) ValidateNumber

func (c RangeConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error

func (RangeConstraint) When

func (c RangeConstraint) When(condition bool) RangeConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type RegexConstraint

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

RegexConstraint is used to ensure that the given value corresponds to regex pattern.

func DoesNotMatch

func DoesNotMatch(regex *regexp.Regexp) RegexConstraint

Matches creates a RegexConstraint for checking whether a value not match.

Example

err := validator.ValidateString(&s, it.DoesNotMatch(regexp.MustCompile("^[a-z]+$")))

func Matches

func Matches(regex *regexp.Regexp) RegexConstraint

Matches creates a RegexConstraint for checking whether a value match.

Example

err := validator.ValidateString(&s, it.Matches(regexp.MustCompile("^[a-z]+$")))

func (RegexConstraint) Message

func (c RegexConstraint) Message(message string) RegexConstraint

Message sets the violation message template. You can use template parameters. for injecting its values into the final message:

{{ value }} - the current (invalid) value.

func (RegexConstraint) Name

func (c RegexConstraint) Name() string

Name is the constraint name.

func (RegexConstraint) SetUp

func (c RegexConstraint) SetUp() error

SetUp will return an error if the pattern is empty.

func (RegexConstraint) ValidateString

func (c RegexConstraint) ValidateString(value *string, scope validation.Scope) error

func (RegexConstraint) When

func (c RegexConstraint) When(condition bool) RegexConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type StringComparisonConstraint

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

StringComparisonConstraint is used to compare strings.

func IsEqualToString

func IsEqualToString(value string) StringComparisonConstraint

IsEqualToString checks that the string value is equal to the specified string value.

Example

v := "actual"
err := validator.ValidateString(&v, it.IsEqualToString("expected"))

func IsNotEqualToString

func IsNotEqualToString(value string) StringComparisonConstraint

IsNotEqualToString checks that the string value is not equal to the specified string value.

Example

v := "expected"
err := validator.ValidateString(&v, it.IsNotEqualToString("expected"))

func (StringComparisonConstraint) Message

Message sets the violation message template. You can use template parameters for injecting its values into the final message:

{{ comparedValue }} - the expected value;
{{ value }} - the current (invalid) value.

All string values are quoted strings.

func (StringComparisonConstraint) Name

Name is the constraint name.

func (StringComparisonConstraint) SetUp

SetUp always returns no error.

func (StringComparisonConstraint) ValidateString

func (c StringComparisonConstraint) ValidateString(value *string, scope validation.Scope) error

func (StringComparisonConstraint) When

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type TimeComparisonConstraint

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

TimeComparisonConstraint is used to compare time values.

func IsEarlierThan

func IsEarlierThan(value time.Time) TimeComparisonConstraint

IsEarlierThan checks that the given time is earlier than the specified value.

Example

t := time.Now()
err := validator.ValidateTime(&t, it.IsEarlierThan(time.Now().Add(time.Hour)))

func IsEarlierThanOrEqual

func IsEarlierThanOrEqual(value time.Time) TimeComparisonConstraint

IsEarlierThanOrEqual checks that the given time is earlier or equal to the specified value.

Example

t := time.Now()
err := validator.ValidateTime(&t, it.IsEarlierThanOrEqual(time.Now().Add(time.Hour)))

func IsLaterThan

func IsLaterThan(value time.Time) TimeComparisonConstraint

IsLaterThan checks that the given time is later than the specified value.

Example

t := time.Now()
err := validator.ValidateTime(&t, it.IsLaterThan(time.Now().Sub(time.Hour)))

func IsLaterThanOrEqual

func IsLaterThanOrEqual(value time.Time) TimeComparisonConstraint

IsLaterThanOrEqual checks that the given time is later or equal to the specified value.

Example

t := time.Now()
err := validator.ValidateTime(&t, it.IsLaterThanOrEqual(time.Now().Sub(time.Hour)))

func (TimeComparisonConstraint) Layout

Layout can be used to set the layout that is used to format time values.

func (TimeComparisonConstraint) Message

Message sets the violation message template. You can use template parameters for injecting its values into the final message:

{{ comparedValue }} - the expected value;
{{ value }} - the current (invalid) value.

All values are formatted by the layout that can be defined by the Layout method. Default layout is time.RFC3339.

func (TimeComparisonConstraint) Name

Name is the constraint name.

func (TimeComparisonConstraint) SetUp

func (c TimeComparisonConstraint) SetUp() error

SetUp always returns no error.

func (TimeComparisonConstraint) ValidateTime

func (c TimeComparisonConstraint) ValidateTime(value *time.Time, scope validation.Scope) error

func (TimeComparisonConstraint) When

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type TimeRangeConstraint

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

TimeRangeConstraint is used to check that a given time value is between some minimum and maximum.

func IsBetweenTime

func IsBetweenTime(min, max time.Time) TimeRangeConstraint

IsBetweenTime checks that the time is between specified minimum and maximum time values.

Example

t := time.Now()
err := validator.ValidateTime(&t, it.IsBetweenTime(time.Now().Add(time.Hour), time.Now().Add(2*time.Hour)))

func (TimeRangeConstraint) Layout

Layout can be used to set the layout that is used to format time values.

func (TimeRangeConstraint) Message

func (c TimeRangeConstraint) Message(message string) TimeRangeConstraint

Message sets the violation message template. You can use template parameters for injecting its values into the final message:

{{ max }} - the upper limit;
{{ min }} - the lower limit;
{{ value }} - the current (invalid) value.

All values are formatted by the layout that can be defined by the Layout method. Default layout is time.RFC3339.

func (TimeRangeConstraint) Name

func (c TimeRangeConstraint) Name() string

Name is the constraint name.

func (TimeRangeConstraint) SetUp

func (c TimeRangeConstraint) SetUp() error

SetUp returns an error if min is greater than or equal to max.

func (TimeRangeConstraint) ValidateTime

func (c TimeRangeConstraint) ValidateTime(value *time.Time, scope validation.Scope) error

func (TimeRangeConstraint) When

func (c TimeRangeConstraint) When(condition bool) TimeRangeConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

type URLConstraint added in v0.2.0

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

URLConstraint is used to validate URL string. This constraint doesn’t check that the host of the given URL really exists, because the information of the DNS records is not reliable.

This constraint doesn't check the length of the URL. Use LengthConstraint to check the length of the given value.

func IsURL added in v0.2.0

func IsURL() URLConstraint

IsURL creates a URLConstraint to validate an URL. By default, constraint checks only for the http:// and https:// schemas. Use the WithSchemas method to configure the list of expected schemas. Also, you can use WithRelativeSchema to enable support of the relative schema (without schema, e.g. "//example.com").

Example (InvalidURL)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "example.com"
	err := validator.ValidateString(&v, it.IsURL())
	fmt.Println(err)
}
Output:

violation: This value is not a valid URL.
Example (ValidURL)
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "http://example.com"
	err := validator.ValidateString(&v, it.IsURL())
	fmt.Println(err)
}
Output:

<nil>

func (URLConstraint) Message added in v0.2.0

func (c URLConstraint) Message(message string) URLConstraint

Message sets the violation message template. You can use template parameters for injecting its values into the final message:

{{ value }} - the current (invalid) value.

func (URLConstraint) Name added in v0.2.0

func (c URLConstraint) Name() string

Name is the constraint name.

func (URLConstraint) SetUp added in v0.2.0

func (c URLConstraint) SetUp() error

SetUp will return an error if the list of schemas is empty.

func (URLConstraint) ValidateString added in v0.2.0

func (c URLConstraint) ValidateString(value *string, scope validation.Scope) error

func (URLConstraint) When added in v0.2.0

func (c URLConstraint) When(condition bool) URLConstraint

When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.

func (URLConstraint) WithRelativeSchema added in v0.2.0

func (c URLConstraint) WithRelativeSchema() URLConstraint

WithRelativeSchema enables support of relative URL schema, which means that URL value may be treated as relative (without schema, e.g. "//example.com").

Example
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "//example.com"
	err := validator.ValidateString(&v, it.IsURL().WithRelativeSchema())
	fmt.Println(err)
}
Output:

<nil>

func (URLConstraint) WithSchemas added in v0.2.0

func (c URLConstraint) WithSchemas(schemas ...string) URLConstraint

WithSchemas is used to set up a list of accepted schemas. For example, if you also consider the ftp:// type URLs to be valid, redefine the schemas list, listing http, https, and also ftp. If the list is empty, then an error will be returned by the SetUp method.

Example
package main

import (
	"fmt"

	"github.com/muonsoft/validation/it"
	"github.com/muonsoft/validation/validator"
)

func main() {
	v := "ftp://example.com"
	err := validator.ValidateString(&v, it.IsURL().WithSchemas("http", "https", "ftp"))
	fmt.Println(err)
}
Output:

<nil>

Jump to

Keyboard shortcuts

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