it

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2022 License: MIT Imports: 14 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 IsEAN13 added in v0.8.0

IsEAN13 is used to validate EAN-13 value.

See https://en.wikipedia.org/wiki/International_Article_Number.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	err := validator.Validate(context.Background(), validation.String("4006381333932", it.IsEAN13()))
	fmt.Println(err)
}
Output:

violation: "This value is not a valid EAN-13."

func IsEAN8 added in v0.8.0

IsEAN8 is used to validate EAN-8 value.

See https://en.wikipedia.org/wiki/EAN-8.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	err := validator.Validate(context.Background(), validation.String("42345670", it.IsEAN8()))
	fmt.Println(err)
}
Output:

violation: "This value is not a valid EAN-8."

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 (
	"context"
	"fmt"

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

func main() {
	v := "user example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsEmail()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

func main() {
	v := "user@example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsEmail()))
	fmt.Println(err)
}
Output:

<nil>

func IsHTML5Email added in v0.2.0

func IsHTML5Email() validation.StringFuncConstraint

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 (
	"context"
	"fmt"

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

func main() {
	v := "@example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsEmail()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

func main() {
	v := "{}~!@example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsEmail()))
	fmt.Println(err)
}
Output:

<nil>

func IsHostname added in v0.2.0

func IsHostname() validation.StringFuncConstraint

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 (
	"context"
	"fmt"

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

func main() {
	v := "example-.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsHostname()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

func main() {
	v := "example.localhost"
	err := validator.Validate(context.Background(), validation.String(v, it.IsHostname()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

func main() {
	v := "example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsHostname()))
	fmt.Println(err)
}
Output:

<nil>

func IsInteger added in v0.8.0

func IsInteger() validation.StringFuncConstraint

IsInteger checks that string value is an integer.

Example (InvalidInteger)
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "foo"
	err := validator.Validate(context.Background(), validation.String(v, it.IsInteger()))
	fmt.Println(err)
}
Output:

violation: "This value is not an integer."
Example (ValidInteger)
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "123"
	err := validator.Validate(context.Background(), validation.String(v, it.IsInteger()))
	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 (
	"context"
	"fmt"

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

func main() {
	v := `"invalid": true`
	err := validator.Validate(context.Background(), validation.String(v, it.IsJSON()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

func main() {
	v := `{"valid": true}`
	err := validator.Validate(context.Background(), validation.String(v, it.IsJSON()))
	fmt.Println(err)
}
Output:

<nil>

func IsLooseHostname added in v0.2.0

func IsLooseHostname() validation.StringFuncConstraint

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 (
	"context"
	"fmt"

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

func main() {
	v := "example-.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsLooseHostname()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

func main() {
	v := "example.localhost"
	err := validator.Validate(context.Background(), validation.String(v, it.IsLooseHostname()))
	fmt.Println(err)
}
Output:

<nil>
Example (ValidHostname)
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsLooseHostname()))
	fmt.Println(err)
}
Output:

<nil>

func IsNumeric added in v0.8.0

func IsNumeric() validation.StringFuncConstraint

IsNumeric checks that string value is a valid numeric (integer or float).

Example (InvalidNumeric)
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "foo.bar"
	err := validator.Validate(context.Background(), validation.String(v, it.IsNumeric()))
	fmt.Println(err)
}
Output:

violation: "This value is not a numeric."
Example (ValidNumeric)
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "123.123"
	err := validator.Validate(context.Background(), validation.String(v, it.IsNumeric()))
	fmt.Println(err)
}
Output:

<nil>

func IsUPCA added in v0.8.0

IsUPCA is used to validate UPC-A value.

See https://en.wikipedia.org/wiki/Universal_Product_Code.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	err := validator.Validate(context.Background(), validation.String("614141000037", it.IsUPCA()))
	fmt.Println(err)
}
Output:

violation: "This value is not a valid UPC-A."

func IsUPCE added in v0.8.0

IsUPCE is used to validate UPC-E value.

See https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	err := validator.Validate(context.Background(), validation.String("01234501", it.IsUPCE()))
	fmt.Println(err)
}
Output:

violation: "This value is not a valid UPC-E."

Types

type BlankConstraint

type BlankConstraint[T comparable] 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[string]

IsBlank creates a BlankConstraint for checking that value is empty.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(context.Background(), validation.String("foo", it.IsBlank())))
	fmt.Println(validator.Validate(context.Background(), validation.Countable(len([]string{"foo"}), it.IsBlank())))
	fmt.Println(validator.Validate(context.Background(), validation.Comparable[string]("foo", it.IsBlank())))
}
Output:

violation: "This value should be blank."
violation: "This value should be blank."
violation: "This value should be blank."

func IsBlankComparable added in v0.10.0

func IsBlankComparable[T comparable]() BlankConstraint[T]

IsBlankComparable creates a BlankConstraint for checking that comparable value is not empty.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(context.Background(), validation.Comparable[int](1, it.IsBlankComparable[int]())))
	fmt.Println(validator.Validate(context.Background(), validation.Comparable[string]("foo", it.IsBlankComparable[string]())))
}
Output:

violation: "This value should be blank."
violation: "This value should be blank."

func IsBlankNumber added in v0.9.0

func IsBlankNumber[T validation.Numeric]() BlankConstraint[T]

IsBlankNumber creates a BlankConstraint for checking that numeric value is nil or zero.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(context.Background(), validation.Number[int](1, it.IsBlankNumber[int]())))
	fmt.Println(validator.Validate(context.Background(), validation.Number[float64](1.1, it.IsBlankNumber[float64]())))
}
Output:

violation: "This value should be blank."
violation: "This value should be blank."

func (BlankConstraint[T]) ValidateBool

func (c BlankConstraint[T]) ValidateBool(ctx context.Context, validator *validation.Validator, value *bool) error

func (BlankConstraint[T]) ValidateComparable added in v0.10.0

func (c BlankConstraint[T]) ValidateComparable(ctx context.Context, validator *validation.Validator, value *T) error

func (BlankConstraint[T]) ValidateCountable

func (c BlankConstraint[T]) ValidateCountable(ctx context.Context, validator *validation.Validator, count int) error

func (BlankConstraint[T]) ValidateNumber

func (c BlankConstraint[T]) ValidateNumber(ctx context.Context, validator *validation.Validator, value *T) error

func (BlankConstraint[T]) ValidateString

func (c BlankConstraint[T]) ValidateString(ctx context.Context, validator *validation.Validator, value *string) error

func (BlankConstraint[T]) ValidateTime

func (c BlankConstraint[T]) ValidateTime(ctx context.Context, validator *validation.Validator, value *time.Time) error

func (BlankConstraint[T]) When

func (c BlankConstraint[T]) When(condition bool) BlankConstraint[T]

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

func (BlankConstraint[T]) WhenGroups added in v0.8.0

func (c BlankConstraint[T]) WhenGroups(groups ...string) BlankConstraint[T]

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (BlankConstraint[T]) WithError added in v0.13.0

func (c BlankConstraint[T]) WithError(err error) BlankConstraint[T]

WithError overrides default error for produced violation.

func (BlankConstraint[T]) WithMessage added in v0.13.0

func (c BlankConstraint[T]) WithMessage(template string, parameters ...validation.TemplateParameter) BlankConstraint[T]

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message.

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
package main

import (
	"context"
	"fmt"

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

func main() {
	err := validator.Validate(context.Background(), validation.Bool(true, it.IsFalse()))
	fmt.Println(err)
}
Output:

violation: "This value should be false."

func IsTrue

func IsTrue() BoolConstraint

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	err := validator.Validate(context.Background(), validation.Bool(false, it.IsTrue()))
	fmt.Println(err)
}
Output:

violation: "This value should be true."

func (BoolConstraint) ValidateBool

func (c BoolConstraint) ValidateBool(ctx context.Context, validator *validation.Validator, value *bool) 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.

func (BoolConstraint) WhenGroups added in v0.8.0

func (c BoolConstraint) WhenGroups(groups ...string) BoolConstraint

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (BoolConstraint) WithError added in v0.13.0

func (c BoolConstraint) WithError(err error) BoolConstraint

WithError overrides default error for produced violation.

func (BoolConstraint) WithMessage added in v0.13.0

func (c BoolConstraint) WithMessage(template string, parameters ...validation.TemplateParameter) BoolConstraint

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message.

type ChoiceConstraint

type ChoiceConstraint[T comparable] struct {
	// contains filtered or unexported fields
}

ChoiceConstraint is used to ensure that the given value corresponds to one of the expected choices. Zero values (zero numbers or empty strings) are considered as valid. Use NotBlankConstraint to check that they are not empty. Also, in order for a blank value to be checked, use the WithoutBlank method.

func IsOneOf added in v0.9.0

func IsOneOf[T comparable](values ...T) ChoiceConstraint[T]

IsOneOf creates a ChoiceConstraint for checking that values are in the expected list of values. Zero values (zero numbers or empty strings) are considered as valid. Use NotBlankConstraint to check that they are not empty. Also, in order for a blank value to be checked, use the WithoutBlank method.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Comparable[string]("foo", it.IsOneOf("one", "two", "three"))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.String("foo", it.IsOneOf("one", "two", "three"))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Comparable[int](1, it.IsOneOf(2, 3, 4))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsOneOf(2, 3, 4))),
	)
}
Output:

violation: "The value you selected is not a valid choice."
violation: "The value you selected is not a valid choice."
violation: "The value you selected is not a valid choice."
violation: "The value you selected is not a valid choice."

func (ChoiceConstraint[T]) ValidateComparable added in v0.9.0

func (c ChoiceConstraint[T]) ValidateComparable(ctx context.Context, validator *validation.Validator, value *T) error

func (ChoiceConstraint[T]) ValidateNumber added in v0.10.0

func (c ChoiceConstraint[T]) ValidateNumber(ctx context.Context, validator *validation.Validator, value *T) error

func (ChoiceConstraint[T]) ValidateString

func (c ChoiceConstraint[T]) ValidateString(ctx context.Context, validator *validation.Validator, value *T) error

func (ChoiceConstraint[T]) When

func (c ChoiceConstraint[T]) When(condition bool) ChoiceConstraint[T]

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

func (ChoiceConstraint[T]) WhenGroups added in v0.8.0

func (c ChoiceConstraint[T]) WhenGroups(groups ...string) ChoiceConstraint[T]

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (ChoiceConstraint[T]) WithError added in v0.13.0

func (c ChoiceConstraint[T]) WithError(err error) ChoiceConstraint[T]

WithError overrides default error for produced violation.

func (ChoiceConstraint[T]) WithMessage added in v0.13.0

func (c ChoiceConstraint[T]) WithMessage(template string, parameters ...validation.TemplateParameter) ChoiceConstraint[T]

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

func (ChoiceConstraint[T]) WithoutBlank added in v0.13.2

func (c ChoiceConstraint[T]) WithoutBlank() ChoiceConstraint[T]

WithoutBlank makes zero values valid.

type ComparisonConstraint added in v0.10.0

type ComparisonConstraint[T comparable] struct {
	// contains filtered or unexported fields
}

ComparisonConstraint is used for comparisons between comparable generic types.

func IsEqualTo added in v0.10.0

func IsEqualTo[T comparable](value T) ComparisonConstraint[T]

IsEqualTo checks that the value is equal to the specified value.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsEqualTo(2)),
	))
	fmt.Println(validator.Validate(
		context.Background(),
		validation.String("foo", it.IsEqualTo("bar")),
	))
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Comparable[string]("foo", it.IsEqualTo("bar")),
	))
}
Output:

violation: "This value should be equal to 2."
violation: "This value should be equal to "bar"."
violation: "This value should be equal to "bar"."

func IsNotEqualTo added in v0.10.0

func IsNotEqualTo[T comparable](value T) ComparisonConstraint[T]

IsNotEqualTo checks that the value is not equal to the specified value.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsNotEqualTo(1)),
	))
	fmt.Println(validator.Validate(
		context.Background(),
		validation.String("foo", it.IsNotEqualTo("foo")),
	))
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Comparable[string]("foo", it.IsNotEqualTo("foo")),
	))
}
Output:

violation: "This value should not be equal to 1."
violation: "This value should not be equal to "foo"."
violation: "This value should not be equal to "foo"."

func (ComparisonConstraint[T]) ValidateComparable added in v0.10.0

func (c ComparisonConstraint[T]) ValidateComparable(ctx context.Context, validator *validation.Validator, value *T) error

func (ComparisonConstraint[T]) ValidateNumber added in v0.10.0

func (c ComparisonConstraint[T]) ValidateNumber(ctx context.Context, validator *validation.Validator, value *T) error

func (ComparisonConstraint[T]) ValidateString added in v0.10.0

func (c ComparisonConstraint[T]) ValidateString(ctx context.Context, validator *validation.Validator, value *T) error

func (ComparisonConstraint[T]) When added in v0.10.0

func (c ComparisonConstraint[T]) When(condition bool) ComparisonConstraint[T]

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

func (ComparisonConstraint[T]) WhenGroups added in v0.10.0

func (c ComparisonConstraint[T]) WhenGroups(groups ...string) ComparisonConstraint[T]

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (ComparisonConstraint[T]) WithError added in v0.13.0

func (c ComparisonConstraint[T]) WithError(err error) ComparisonConstraint[T]

WithError overrides default error for produced violation.

func (ComparisonConstraint[T]) WithMessage added in v0.13.0

func (c ComparisonConstraint[T]) WithMessage(
	template string,
	parameters ...validation.TemplateParameter,
) ComparisonConstraint[T]

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

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
package main

import (
	"context"
	"fmt"

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

func main() {
	v := []int{1, 2}
	err := validator.ValidateCountable(context.Background(), len(v), it.HasCountBetween(3, 10))
	fmt.Println(err)
}
Output:

violation: "This collection should contain 3 elements or more."

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
package main

import (
	"context"
	"fmt"

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

func main() {
	v := []int{1, 2}
	err := validator.ValidateCountable(context.Background(), len(v), it.HasExactCount(3))
	fmt.Println(err)
}
Output:

violation: "This collection should contain exactly 3 elements."

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
package main

import (
	"context"
	"fmt"

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

func main() {
	v := []int{1, 2}
	err := validator.ValidateCountable(context.Background(), len(v), it.HasMaxCount(1))
	fmt.Println(err)
}
Output:

violation: "This collection should contain 1 element or less."

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
package main

import (
	"context"
	"fmt"

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

func main() {
	v := []int{1, 2}
	err := validator.ValidateCountable(context.Background(), len(v), it.HasMinCount(3))
	fmt.Println(err)
}
Output:

violation: "This collection should contain 3 elements or more."

func (CountConstraint) ValidateCountable

func (c CountConstraint) ValidateCountable(ctx context.Context, validator *validation.Validator, count int) 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.

func (CountConstraint) WhenGroups added in v0.8.0

func (c CountConstraint) WhenGroups(groups ...string) CountConstraint

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (CountConstraint) WithExactError added in v0.13.0

func (c CountConstraint) WithExactError(err error) CountConstraint

WithExactError overrides default underlying error for violation that will be shown if minimum and maximum values are equal and the length of the collection is not exactly this value.

func (CountConstraint) WithExactMessage added in v0.13.0

func (c CountConstraint) WithExactMessage(template string, parameters ...validation.TemplateParameter) CountConstraint

WithExactMessage 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. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

{{ count }} - the current collection size;
{{ limit }} - the exact expected collection size.

func (CountConstraint) WithMaxError added in v0.13.0

func (c CountConstraint) WithMaxError(err error) CountConstraint

WithMaxError overrides default underlying error for violation that will be shown if the collection length is greater than the maximum value.

func (CountConstraint) WithMaxMessage added in v0.13.0

func (c CountConstraint) WithMaxMessage(template string, parameters ...validation.TemplateParameter) CountConstraint

WithMaxMessage sets the violation message that will be shown if the collection length is greater than the maximum value. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

{{ count }} - the current collection size;
{{ limit }} - the upper limit.

func (CountConstraint) WithMinError added in v0.13.0

func (c CountConstraint) WithMinError(err error) CountConstraint

WithMinError overrides default underlying error for violation that will be shown if the collection length is less than the minimum value.

func (CountConstraint) WithMinMessage added in v0.13.0

func (c CountConstraint) WithMinMessage(template string, parameters ...validation.TemplateParameter) CountConstraint

WithMinMessage sets the violation message that will be shown if the collection length is less than the minimum value. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

{{ count }} - the current collection size;
{{ limit }} - the lower limit.

type DateTimeConstraint added in v0.14.0

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

DateTimeConstraint checks that the string value is a valid date and time value specified by a specific layout. The layout can be redefined using the WithLayout method.

func IsDate added in v0.14.0

func IsDate() DateTimeConstraint

IsDate checks that the string value is a valid date. It uses "2006-01-02" layout. The layout can be redefined using the WithLayout method.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(
		"#1 invalid:",
		validator.Validate(
			context.Background(),
			validation.String(`invalid`, it.IsDate()),
		),
	)
	fmt.Println(
		"#2 valid:",
		validator.Validate(
			context.Background(),
			validation.String(`2022-07-12`, it.IsDate()),
		),
	)
	fmt.Println(
		"#3 custom layout:",
		validator.Validate(
			context.Background(),
			validation.String(`12 Jul 22 12:34`, it.IsDate().WithLayout("02 Jan 06 15:04")),
		),
	)
}
Output:

#1 invalid: violation: "This value is not a valid date."
#2 valid: <nil>
#3 custom layout: <nil>

func IsDateTime added in v0.14.0

func IsDateTime() DateTimeConstraint

IsDateTime checks that the string value is a valid date and time. By default, it uses time.RFC3339 layout. The layout can be redefined using the WithLayout method.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(
		"#1 invalid:",
		validator.Validate(
			context.Background(),
			validation.String(`invalid`, it.IsDateTime()),
		),
	)
	fmt.Println(
		"#2 valid RFC3339:",
		validator.Validate(
			context.Background(),
			validation.String(`2022-07-12T12:34:56+00:00`, it.IsDateTime()),
		),
	)
	fmt.Println(
		"#3 custom layout:",
		validator.Validate(
			context.Background(),
			validation.String(`2022-07-12 12:34:56`, it.IsDateTime().WithLayout("2006-01-02 15:04:05")),
		),
	)
}
Output:

#1 invalid: violation: "This value is not a valid datetime."
#2 valid RFC3339: <nil>
#3 custom layout: <nil>

func IsTime added in v0.14.0

func IsTime() DateTimeConstraint

IsTime checks that the string value is a valid time. It uses "15:04:05" layout. The layout can be redefined using the WithLayout method.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(
		"#1 invalid:",
		validator.Validate(
			context.Background(),
			validation.String(`invalid`, it.IsTime()),
		),
	)
	fmt.Println(
		"#2 valid:",
		validator.Validate(
			context.Background(),
			validation.String(`12:34:56`, it.IsTime()),
		),
	)
	fmt.Println(
		"#3 custom layout:",
		validator.Validate(
			context.Background(),
			validation.String(`12:34:56+00:00`, it.IsTime().WithLayout("15:04:05Z07:00")),
		),
	)
}
Output:

#1 invalid: violation: "This value is not a valid time."
#2 valid: <nil>
#3 custom layout: <nil>

func (DateTimeConstraint) ValidateString added in v0.14.0

func (c DateTimeConstraint) ValidateString(ctx context.Context, validator *validation.Validator, value *string) error

func (DateTimeConstraint) When added in v0.14.0

func (c DateTimeConstraint) When(condition bool) DateTimeConstraint

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

func (DateTimeConstraint) WhenGroups added in v0.14.0

func (c DateTimeConstraint) WhenGroups(groups ...string) DateTimeConstraint

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (DateTimeConstraint) WithError added in v0.14.0

func (c DateTimeConstraint) WithError(err error) DateTimeConstraint

WithError overrides default error for produced violation.

func (DateTimeConstraint) WithLayout added in v0.14.0

func (c DateTimeConstraint) WithLayout(layout string) DateTimeConstraint

WithLayout specifies the layout to be used for datetime parsing.

func (DateTimeConstraint) WithMessage added in v0.14.0

func (c DateTimeConstraint) WithMessage(template string, parameters ...validation.TemplateParameter) DateTimeConstraint

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

{{ layout }} - date time layout used for parsing;
{{ value }} - the current (invalid) value.

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 (
	"context"
	"fmt"

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

func main() {
	v := "123.123.123.345"
	err := validator.Validate(context.Background(), validation.String(v, it.IsIP()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

func main() {
	v := "123.123.123.123"
	err := validator.Validate(context.Background(), validation.String(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 (
	"context"
	"fmt"

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

func main() {
	v := "123.123.123.345"
	err := validator.Validate(context.Background(), validation.String(v, it.IsIPv4()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

func main() {
	v := "123.123.123.123"
	err := validator.Validate(context.Background(), validation.String(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 (
	"context"
	"fmt"

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

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

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

import (
	"context"
	"fmt"

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

func main() {
	v := "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
	err := validator.Validate(context.Background(), validation.String(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 (
	"context"
	"fmt"
	"net"

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

func main() {
	v := "127.0.0.1"
	err := validator.Validate(
		context.Background(),
		validation.String(
			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 (
	"context"
	"fmt"

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

func main() {
	v := "192.168.1.0"
	err := validator.Validate(context.Background(), validation.String(v, it.IsIP().DenyPrivateIP()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

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

violation: "This IP address is prohibited to use."

func (IPConstraint) ValidateString added in v0.2.0

func (c IPConstraint) ValidateString(ctx context.Context, validator *validation.Validator, value *string) 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.

func (IPConstraint) WhenGroups added in v0.8.0

func (c IPConstraint) WhenGroups(groups ...string) IPConstraint

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (IPConstraint) WithInvalidError added in v0.13.0

func (c IPConstraint) WithInvalidError(err error) IPConstraint

WithInvalidError overrides default underlying error for violation produced on invalid IP case.

func (IPConstraint) WithInvalidMessage added in v0.13.0

func (c IPConstraint) WithInvalidMessage(template string, parameters ...validation.TemplateParameter) IPConstraint

WithInvalidMessage sets the violation message template for invalid IP case. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

func (IPConstraint) WithProhibitedError added in v0.13.0

func (c IPConstraint) WithProhibitedError(err error) IPConstraint

WithProhibitedError overrides default underlying error for violation produced on prohibited IP case.

func (IPConstraint) WithProhibitedMessage added in v0.13.0

func (c IPConstraint) WithProhibitedMessage(template string, parameters ...validation.TemplateParameter) IPConstraint

WithProhibitedMessage sets the violation message template for prohibited IP case. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

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
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "foo"
	err := validator.ValidateString(context.Background(), v, it.HasExactLength(5))
	fmt.Println(err)
}
Output:

violation: "This value should have exactly 5 characters."

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
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "foo"
	err := validator.ValidateString(context.Background(), v, it.HasLengthBetween(5, 10))
	fmt.Println(err)
}
Output:

violation: "This value is too short. It should have 5 characters or more."

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
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "foo"
	err := validator.ValidateString(context.Background(), v, it.HasMaxLength(2))
	fmt.Println(err)
}
Output:

violation: "This value is too long. It should have 2 characters or less."

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
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "foo"
	err := validator.ValidateString(context.Background(), v, it.HasMinLength(5))
	fmt.Println(err)
}
Output:

violation: "This value is too short. It should have 5 characters or more."

func (LengthConstraint) ValidateString

func (c LengthConstraint) ValidateString(ctx context.Context, validator *validation.Validator, value *string) 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.

func (LengthConstraint) WhenGroups added in v0.8.0

func (c LengthConstraint) WhenGroups(groups ...string) LengthConstraint

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (LengthConstraint) WithExactError added in v0.13.0

func (c LengthConstraint) WithExactError(err error) LengthConstraint

WithExactError overrides default underlying error for violation that will be shown if minimum and maximum values are equal and the length of the string is not exactly this value.

func (LengthConstraint) WithExactMessage added in v0.13.0

func (c LengthConstraint) WithExactMessage(template string, parameters ...validation.TemplateParameter) LengthConstraint

WithExactMessage 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. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

{{ length }} - the current string length;
{{ limit }} - the lower limit;
{{ value }} - the current (invalid) value.

func (LengthConstraint) WithMaxError added in v0.13.0

func (c LengthConstraint) WithMaxError(err error) LengthConstraint

WithMaxError overrides default underlying error for violation that will be shown if the string length is greater than the maximum value.

func (LengthConstraint) WithMaxMessage added in v0.13.0

func (c LengthConstraint) WithMaxMessage(template string, parameters ...validation.TemplateParameter) LengthConstraint

WithMaxMessage sets the violation message that will be shown if the string length is greater than the maximum value. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

{{ length }} - the current string length;
{{ limit }} - the lower limit;
{{ value }} - the current (invalid) value.

func (LengthConstraint) WithMinError added in v0.13.0

func (c LengthConstraint) WithMinError(err error) LengthConstraint

WithMinError overrides default underlying error for violation that will be shown if the string length is less than the minimum value.

func (LengthConstraint) WithMinMessage added in v0.13.0

func (c LengthConstraint) WithMinMessage(template string, parameters ...validation.TemplateParameter) LengthConstraint

WithMinMessage sets the violation message that will be shown if the string length is less than the minimum value. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

{{ length }} - the current string length;
{{ limit }} - the lower limit;
{{ value }} - the current (invalid) value.

type NilConstraint

type NilConstraint[T comparable] 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[string]

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	s := ""
	sp := &s
	fmt.Println(validator.Validate(context.Background(), validation.Nil(sp == nil, it.IsNil())))
	fmt.Println(validator.Validate(context.Background(), validation.NilString(&s, it.IsNil())))
	fmt.Println(validator.Validate(context.Background(), validation.NilComparable[string](&s, it.IsNil())))
}
Output:

violation: "This value should be nil."
violation: "This value should be nil."
violation: "This value should be nil."

func IsNilComparable added in v0.10.0

func IsNilComparable[T comparable]() NilConstraint[T]

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	n := 0
	s := ""
	fmt.Println(validator.Validate(context.Background(), validation.NilComparable[int](&n, it.IsNilComparable[int]())))
	fmt.Println(validator.Validate(context.Background(), validation.NilComparable[string](&s, it.IsNilComparable[string]())))
}
Output:

violation: "This value should be nil."
violation: "This value should be nil."

func IsNilNumber added in v0.10.0

func IsNilNumber[T validation.Numeric]() NilConstraint[T]

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	n := 0
	f := 0.0
	fmt.Println(validator.Validate(context.Background(), validation.NilNumber[int](&n, it.IsNilNumber[int]())))
	fmt.Println(validator.Validate(context.Background(), validation.NilNumber[float64](&f, it.IsNilNumber[float64]())))
}
Output:

violation: "This value should be nil."
violation: "This value should be nil."

func (NilConstraint[T]) ValidateBool added in v0.4.0

func (c NilConstraint[T]) ValidateBool(ctx context.Context, validator *validation.Validator, value *bool) error

func (NilConstraint[T]) ValidateComparable added in v0.10.0

func (c NilConstraint[T]) ValidateComparable(ctx context.Context, validator *validation.Validator, value *T) error

func (NilConstraint[T]) ValidateNil

func (c NilConstraint[T]) ValidateNil(ctx context.Context, validator *validation.Validator, isNil bool) error

func (NilConstraint[T]) ValidateNumber

func (c NilConstraint[T]) ValidateNumber(ctx context.Context, validator *validation.Validator, value *T) error

func (NilConstraint[T]) ValidateString

func (c NilConstraint[T]) ValidateString(ctx context.Context, validator *validation.Validator, value *string) error

func (NilConstraint[T]) ValidateTime

func (c NilConstraint[T]) ValidateTime(ctx context.Context, validator *validation.Validator, value *time.Time) error

func (NilConstraint[T]) When

func (c NilConstraint[T]) When(condition bool) NilConstraint[T]

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

func (NilConstraint[T]) WhenGroups added in v0.8.0

func (c NilConstraint[T]) WhenGroups(groups ...string) NilConstraint[T]

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (NilConstraint[T]) WithError added in v0.13.0

func (c NilConstraint[T]) WithError(err error) NilConstraint[T]

WithError overrides default error for produced violation.

func (NilConstraint[T]) WithMessage added in v0.13.0

func (c NilConstraint[T]) WithMessage(template string, parameters ...validation.TemplateParameter) NilConstraint[T]

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message.

type NotBlankConstraint

type NotBlankConstraint[T comparable] struct {
	// contains filtered or unexported fields
}

NotBlankConstraint checks that a value is not blank: an empty string, an empty countable (slice/array/map), an empty generic number, generic comparable, false or nil. Nil behavior is configurable via WithAllowedNil() method. To check that a value is not nil only use NotNilConstraint.

func IsNotBlank

func IsNotBlank() NotBlankConstraint[string]

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(context.Background(), validation.String("", it.IsNotBlank())))
	fmt.Println(validator.Validate(context.Background(), validation.Countable(len([]string{}), it.IsNotBlank())))
	fmt.Println(validator.Validate(context.Background(), validation.Comparable[string]("", it.IsNotBlank())))
}
Output:

violation: "This value should not be blank."
violation: "This value should not be blank."
violation: "This value should not be blank."

func IsNotBlankComparable added in v0.10.0

func IsNotBlankComparable[T comparable]() NotBlankConstraint[T]

IsNotBlankComparable creates a NotBlankConstraint for checking that comparable value is not empty.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(context.Background(), validation.Comparable[int](0, it.IsNotBlankComparable[int]())))
	fmt.Println(validator.Validate(context.Background(), validation.Comparable[string]("", it.IsNotBlankComparable[string]())))
}
Output:

violation: "This value should not be blank."
violation: "This value should not be blank."

func IsNotBlankNumber added in v0.9.0

func IsNotBlankNumber[T validation.Numeric]() NotBlankConstraint[T]

IsNotBlankNumber creates a NotBlankConstraint for checking that numeric value is not empty.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(context.Background(), validation.Number[int](0, it.IsNotBlankNumber[int]())))
	fmt.Println(validator.Validate(context.Background(), validation.Number[float64](0.0, it.IsNotBlankNumber[float64]())))
}
Output:

violation: "This value should not be blank."
violation: "This value should not be blank."

func (NotBlankConstraint[T]) ValidateBool

func (c NotBlankConstraint[T]) ValidateBool(ctx context.Context, validator *validation.Validator, value *bool) error

func (NotBlankConstraint[T]) ValidateComparable added in v0.10.0

func (c NotBlankConstraint[T]) ValidateComparable(ctx context.Context, validator *validation.Validator, value *T) error

func (NotBlankConstraint[T]) ValidateCountable

func (c NotBlankConstraint[T]) ValidateCountable(ctx context.Context, validator *validation.Validator, count int) error

func (NotBlankConstraint[T]) ValidateNumber

func (c NotBlankConstraint[T]) ValidateNumber(ctx context.Context, validator *validation.Validator, value *T) error

func (NotBlankConstraint[T]) ValidateString

func (c NotBlankConstraint[T]) ValidateString(ctx context.Context, validator *validation.Validator, value *string) error

func (NotBlankConstraint[T]) ValidateTime

func (c NotBlankConstraint[T]) ValidateTime(ctx context.Context, validator *validation.Validator, value *time.Time) error

func (NotBlankConstraint[T]) When

func (c NotBlankConstraint[T]) When(condition bool) NotBlankConstraint[T]

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

func (NotBlankConstraint[T]) WhenGroups added in v0.8.0

func (c NotBlankConstraint[T]) WhenGroups(groups ...string) NotBlankConstraint[T]

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (NotBlankConstraint[T]) WithAllowedNil added in v0.13.0

func (c NotBlankConstraint[T]) WithAllowedNil() NotBlankConstraint[T]

WithAllowedNil makes nil values valid.

func (NotBlankConstraint[T]) WithError added in v0.13.0

func (c NotBlankConstraint[T]) WithError(err error) NotBlankConstraint[T]

WithError overrides default error for produced violation.

func (NotBlankConstraint[T]) WithMessage added in v0.13.0

func (c NotBlankConstraint[T]) WithMessage(template string, parameters ...validation.TemplateParameter) NotBlankConstraint[T]

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message.

type NotNilConstraint

type NotNilConstraint[T comparable] 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[string]

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	var s *string
	fmt.Println(validator.Validate(context.Background(), validation.Nil(s == nil, it.IsNotNil())))
	fmt.Println(validator.Validate(context.Background(), validation.NilString(s, it.IsNotNil())))
	fmt.Println(validator.Validate(context.Background(), validation.NilComparable[string](s, it.IsNotNil())))
}
Output:

violation: "This value should not be nil."
violation: "This value should not be nil."
violation: "This value should not be nil."

func IsNotNilComparable added in v0.10.0

func IsNotNilComparable[T comparable]() NotNilConstraint[T]

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	var n *int
	var s *string
	fmt.Println(validator.Validate(context.Background(), validation.NilComparable[int](n, it.IsNotNilComparable[int]())))
	fmt.Println(validator.Validate(context.Background(), validation.NilComparable[string](s, it.IsNotNilComparable[string]())))
}
Output:

violation: "This value should not be nil."
violation: "This value should not be nil."

func IsNotNilNumber added in v0.10.0

func IsNotNilNumber[T validation.Numeric]() NotNilConstraint[T]

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	var n *int
	var f *float64
	fmt.Println(validator.Validate(context.Background(), validation.NilNumber[int](n, it.IsNotNilNumber[int]())))
	fmt.Println(validator.Validate(context.Background(), validation.NilNumber[float64](f, it.IsNotNilNumber[float64]())))
}
Output:

violation: "This value should not be nil."
violation: "This value should not be nil."

func (NotNilConstraint[T]) ValidateBool added in v0.4.0

func (c NotNilConstraint[T]) ValidateBool(ctx context.Context, validator *validation.Validator, value *bool) error

func (NotNilConstraint[T]) ValidateComparable added in v0.10.0

func (c NotNilConstraint[T]) ValidateComparable(ctx context.Context, validator *validation.Validator, value *T) error

func (NotNilConstraint[T]) ValidateNil

func (c NotNilConstraint[T]) ValidateNil(ctx context.Context, validator *validation.Validator, isNil bool) error

func (NotNilConstraint[T]) ValidateNumber

func (c NotNilConstraint[T]) ValidateNumber(ctx context.Context, validator *validation.Validator, value *T) error

func (NotNilConstraint[T]) ValidateString

func (c NotNilConstraint[T]) ValidateString(ctx context.Context, validator *validation.Validator, value *string) error

func (NotNilConstraint[T]) ValidateTime

func (c NotNilConstraint[T]) ValidateTime(ctx context.Context, validator *validation.Validator, value *time.Time) error

func (NotNilConstraint[T]) When

func (c NotNilConstraint[T]) When(condition bool) NotNilConstraint[T]

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

func (NotNilConstraint[T]) WhenGroups added in v0.8.0

func (c NotNilConstraint[T]) WhenGroups(groups ...string) NotNilConstraint[T]

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (NotNilConstraint[T]) WithError added in v0.13.0

func (c NotNilConstraint[T]) WithError(err error) NotNilConstraint[T]

WithError overrides default error for produced violation.

func (NotNilConstraint[T]) WithMessage added in v0.13.0

func (c NotNilConstraint[T]) WithMessage(template string, parameters ...validation.TemplateParameter) NotNilConstraint[T]

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message.

type NumberComparisonConstraint

type NumberComparisonConstraint[T validation.Numeric] struct {
	// contains filtered or unexported fields
}

NumberComparisonConstraint is used for various numeric comparisons between integer and float values.

func IsGreaterThan added in v0.9.0

func IsGreaterThan[T validation.Numeric](value T) NumberComparisonConstraint[T]

IsGreaterThan checks that the number is greater than the specified value.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsGreaterThan(1))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](1.1, it.IsGreaterThan(1.1))),
	)
}
Output:

violation: "This value should be greater than 1."
violation: "This value should be greater than 1.1."

func IsGreaterThanOrEqual added in v0.9.0

func IsGreaterThanOrEqual[T validation.Numeric](value T) NumberComparisonConstraint[T]

IsGreaterThanOrEqual checks that the number is greater than or equal to the specified value.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsGreaterThanOrEqual(2))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](1.1, it.IsGreaterThanOrEqual(1.2))),
	)
}
Output:

violation: "This value should be greater than or equal to 2."
violation: "This value should be greater than or equal to 1.2."

func IsLessThan added in v0.9.0

func IsLessThan[T validation.Numeric](value T) NumberComparisonConstraint[T]

IsLessThan checks that the number is less than the specified value.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsLessThan(1))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](1.1, it.IsLessThan(1.1))),
	)
}
Output:

violation: "This value should be less than 1."
violation: "This value should be less than 1.1."

func IsLessThanOrEqual added in v0.9.0

func IsLessThanOrEqual[T validation.Numeric](value T) NumberComparisonConstraint[T]

IsLessThanOrEqual checks that the number is less than or equal to the specified value.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsLessThanOrEqual(0))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](1.1, it.IsLessThanOrEqual(0.1))),
	)
}
Output:

violation: "This value should be less than or equal to 0."
violation: "This value should be less than or equal to 0.1."

func IsNegative

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsNegative[int]())),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](1.1, it.IsNegative[float64]())),
	)
}
Output:

violation: "This value should be negative."
violation: "This value should be negative."

func IsNegativeOrZero

func IsNegativeOrZero[T validation.Numeric]() NumberComparisonConstraint[T]

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsNegativeOrZero[int]())),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](1.1, it.IsNegativeOrZero[float64]())),
	)
}
Output:

violation: "This value should be either negative or zero."
violation: "This value should be either negative or zero."

func IsPositive

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](-1, it.IsPositive[int]())),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](-1.1, it.IsPositive[float64]())),
	)
}
Output:

violation: "This value should be positive."
violation: "This value should be positive."

func IsPositiveOrZero

func IsPositiveOrZero[T validation.Numeric]() NumberComparisonConstraint[T]

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

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](-1, it.IsPositiveOrZero[int]())),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](-1.1, it.IsPositiveOrZero[float64]())),
	)
}
Output:

violation: "This value should be either positive or zero."
violation: "This value should be either positive or zero."

func (NumberComparisonConstraint[T]) ValidateNumber

func (c NumberComparisonConstraint[T]) ValidateNumber(ctx context.Context, validator *validation.Validator, value *T) error

func (NumberComparisonConstraint[T]) When

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

func (NumberComparisonConstraint[T]) WhenGroups added in v0.8.0

func (c NumberComparisonConstraint[T]) WhenGroups(groups ...string) NumberComparisonConstraint[T]

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (NumberComparisonConstraint[T]) WithError added in v0.13.0

WithError overrides default error for produced violation.

func (NumberComparisonConstraint[T]) WithMessage added in v0.13.0

func (c NumberComparisonConstraint[T]) WithMessage(
	template string,
	parameters ...validation.TemplateParameter,
) NumberComparisonConstraint[T]

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

type RangeConstraint

type RangeConstraint[T validation.Numeric] struct {
	// contains filtered or unexported fields
}

RangeConstraint is used to check that a given number value is between some minimum and maximum.

func IsBetween added in v0.9.0

func IsBetween[T validation.Numeric](min, max T) RangeConstraint[T]

IsBetween checks that the number is between specified minimum and maximum numeric values.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[int](1, it.IsBetween(10, 20))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](1.1, it.IsBetween(10.111, 20.222))),
	)
}
Output:

violation: "This value should be between 10 and 20."
violation: "This value should be between 10.111 and 20.222."

func (RangeConstraint[T]) Name

func (c RangeConstraint[T]) Name() string

Name is the constraint name.

func (RangeConstraint[T]) ValidateNumber

func (c RangeConstraint[T]) ValidateNumber(ctx context.Context, validator *validation.Validator, value *T) error

func (RangeConstraint[T]) When

func (c RangeConstraint[T]) When(condition bool) RangeConstraint[T]

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

func (RangeConstraint[T]) WhenGroups added in v0.8.0

func (c RangeConstraint[T]) WhenGroups(groups ...string) RangeConstraint[T]

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (RangeConstraint[T]) WithError added in v0.13.0

func (c RangeConstraint[T]) WithError(err error) RangeConstraint[T]

WithError overrides default error for produced violation.

func (RangeConstraint[T]) WithMessage added in v0.13.0

func (c RangeConstraint[T]) WithMessage(template string, parameters ...validation.TemplateParameter) RangeConstraint[T]

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

type RegexpConstraint added in v0.13.0

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

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

func DoesNotMatch

func DoesNotMatch(regex *regexp.Regexp) RegexpConstraint

DoesNotMatch creates a RegexpConstraint for checking whether a value does not match a regular expression.

Example
package main

import (
	"context"
	"fmt"
	"regexp"

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

func main() {
	v := "foo"
	err := validator.ValidateString(context.Background(), v, it.DoesNotMatch(regexp.MustCompile("^[a-z]+$")))
	fmt.Println(err)
}
Output:

violation: "This value is not valid."

func Matches

func Matches(regex *regexp.Regexp) RegexpConstraint

Matches creates a RegexpConstraint for checking whether a value matches a regular expression.

Example
package main

import (
	"context"
	"fmt"
	"regexp"

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

func main() {
	v := "foo123"
	err := validator.ValidateString(context.Background(), v, it.Matches(regexp.MustCompile("^[a-z]+$")))
	fmt.Println(err)
}
Output:

violation: "This value is not valid."

func (RegexpConstraint) ValidateString added in v0.13.0

func (c RegexpConstraint) ValidateString(ctx context.Context, validator *validation.Validator, value *string) error

func (RegexpConstraint) When added in v0.13.0

func (c RegexpConstraint) When(condition bool) RegexpConstraint

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

func (RegexpConstraint) WhenGroups added in v0.13.0

func (c RegexpConstraint) WhenGroups(groups ...string) RegexpConstraint

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (RegexpConstraint) WithError added in v0.13.0

func (c RegexpConstraint) WithError(err error) RegexpConstraint

WithError overrides default error for produced violation.

func (RegexpConstraint) WithMessage added in v0.13.0

func (c RegexpConstraint) WithMessage(template string, parameters ...validation.TemplateParameter) RegexpConstraint

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

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
package main

import (
	"context"
	"fmt"
	"time"

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

func main() {
	t, _ := time.Parse(time.RFC3339, "2009-02-04T21:00:57-08:00")
	t2, _ := time.Parse(time.RFC3339, "2009-02-03T21:00:57-08:00")
	err := validator.ValidateTime(context.Background(), t, it.IsEarlierThan(t2))
	fmt.Println(err)
}
Output:

violation: "This value should be earlier than 2009-02-03T21:00:57-08:00."

func IsEarlierThanOrEqual

func IsEarlierThanOrEqual(value time.Time) TimeComparisonConstraint

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

Example
package main

import (
	"context"
	"fmt"
	"time"

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

func main() {
	t, _ := time.Parse(time.RFC3339, "2009-02-04T21:00:57-08:00")
	t2, _ := time.Parse(time.RFC3339, "2009-02-03T21:00:57-08:00")
	err := validator.ValidateTime(context.Background(), t, it.IsEarlierThanOrEqual(t2))
	fmt.Println(err)
}
Output:

violation: "This value should be earlier than or equal to 2009-02-03T21:00:57-08:00."

func IsLaterThan

func IsLaterThan(value time.Time) TimeComparisonConstraint

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

Example
package main

import (
	"context"
	"fmt"
	"time"

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

func main() {
	t, _ := time.Parse(time.RFC3339, "2009-02-04T21:00:57-08:00")
	t2, _ := time.Parse(time.RFC3339, "2009-02-05T21:00:57-08:00")
	err := validator.ValidateTime(context.Background(), t, it.IsLaterThan(t2))
	fmt.Println(err)
}
Output:

violation: "This value should be later than 2009-02-05T21:00:57-08:00."

func IsLaterThanOrEqual

func IsLaterThanOrEqual(value time.Time) TimeComparisonConstraint

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

Example
package main

import (
	"context"
	"fmt"
	"time"

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

func main() {
	t, _ := time.Parse(time.RFC3339, "2009-02-04T21:00:57-08:00")
	t2, _ := time.Parse(time.RFC3339, "2009-02-05T21:00:57-08:00")
	err := validator.ValidateTime(context.Background(), t, it.IsLaterThanOrEqual(t2))
	fmt.Println(err)
}
Output:

violation: "This value should be later than or equal to 2009-02-05T21:00:57-08:00."

func (TimeComparisonConstraint) ValidateTime

func (c TimeComparisonConstraint) ValidateTime(ctx context.Context, validator *validation.Validator, value *time.Time) error

func (TimeComparisonConstraint) When

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

func (TimeComparisonConstraint) WhenGroups added in v0.8.0

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (TimeComparisonConstraint) WithError added in v0.13.0

WithError overrides default error for produced violation.

func (TimeComparisonConstraint) WithLayout added in v0.13.0

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

func (TimeComparisonConstraint) WithMessage added in v0.13.0

func (c TimeComparisonConstraint) WithMessage(
	template string,
	parameters ...validation.TemplateParameter,
) TimeComparisonConstraint

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

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

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
package main

import (
	"context"
	"fmt"
	"time"

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

func main() {
	t, _ := time.Parse(time.RFC3339, "2009-02-04T21:00:57-08:00")
	after, _ := time.Parse(time.RFC3339, "2009-02-05T21:00:57-08:00")
	before, _ := time.Parse(time.RFC3339, "2009-02-06T21:00:57-08:00")
	err := validator.ValidateTime(context.Background(), t, it.IsBetweenTime(after, before))
	fmt.Println(err)
}
Output:

violation: "This value should be between 2009-02-05T21:00:57-08:00 and 2009-02-06T21:00:57-08:00."

func (TimeRangeConstraint) ValidateTime

func (c TimeRangeConstraint) ValidateTime(ctx context.Context, validator *validation.Validator, value *time.Time) 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.

func (TimeRangeConstraint) WhenGroups added in v0.8.0

func (c TimeRangeConstraint) WhenGroups(groups ...string) TimeRangeConstraint

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (TimeRangeConstraint) WithError added in v0.13.0

WithError overrides default error for produced violation.

func (TimeRangeConstraint) WithLayout added in v0.13.0

func (c TimeRangeConstraint) WithLayout(layout string) TimeRangeConstraint

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

func (TimeRangeConstraint) WithMessage added in v0.13.0

func (c TimeRangeConstraint) WithMessage(template string, parameters ...validation.TemplateParameter) TimeRangeConstraint

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

{{ 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 WithLayout method. Default layout is time.RFC3339.

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 (
	"context"
	"fmt"

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

func main() {
	v := "example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsURL()))
	fmt.Println(err)
}
Output:

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

import (
	"context"
	"fmt"

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

func main() {
	v := "http://example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsURL()))
	fmt.Println(err)
}
Output:

<nil>

func (URLConstraint) ValidateString added in v0.2.0

func (c URLConstraint) ValidateString(ctx context.Context, validator *validation.Validator, value *string) 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) WhenGroups added in v0.8.0

func (c URLConstraint) WhenGroups(groups ...string) URLConstraint

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (URLConstraint) WithError added in v0.13.0

func (c URLConstraint) WithError(err error) URLConstraint

WithError overrides default error for produced violation.

func (URLConstraint) WithHostMatches added in v0.14.0

func (c URLConstraint) WithHostMatches(pattern *regexp.Regexp) URLConstraint

WithHostMatches is used to set up restricted host validation by regexp pattern. If pattern is nil, then validation will be ignored.

Example
package main

import (
	"context"
	"fmt"
	"regexp"

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

func main() {
	fmt.Println(
		"valid:",
		validator.Validate(
			context.Background(),
			validation.String("https://sub.example.com", it.IsURL().WithHostMatches(regexp.MustCompile(`^.*\.example\.com`))),
		),
	)
	fmt.Println(
		"invalid:",
		validator.Validate(
			context.Background(),
			validation.String("https://sub.sample.com", it.IsURL().WithHostMatches(regexp.MustCompile(`^.*\.example\.com`))),
		),
	)
}
Output:

valid: <nil>
invalid: violation: "This URL is prohibited to use."

func (URLConstraint) WithHosts added in v0.14.0

func (c URLConstraint) WithHosts(hosts ...string) URLConstraint

WithHosts is used to set up a list of accepted hosts. If the list is empty, then any host will be treated as valid.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	fmt.Println(
		"valid:",
		validator.Validate(
			context.Background(),
			validation.String("https://example.com", it.IsURL().WithHosts("example.com")),
		),
	)
	fmt.Println(
		"invalid:",
		validator.Validate(
			context.Background(),
			validation.String("https://sample.com", it.IsURL().WithHosts("example.com")),
		),
	)
}
Output:

valid: <nil>
invalid: violation: "This URL is prohibited to use."

func (URLConstraint) WithMessage added in v0.13.0

func (c URLConstraint) WithMessage(template string, parameters ...validation.TemplateParameter) URLConstraint

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

func (URLConstraint) WithProhibitedError added in v0.14.0

func (c URLConstraint) WithProhibitedError(err error) URLConstraint

WithProhibitedError overrides default error for produced violation on a prohibited value.

func (URLConstraint) WithProhibitedMessage added in v0.14.0

func (c URLConstraint) WithProhibitedMessage(template string, parameters ...validation.TemplateParameter) URLConstraint

WithProhibitedMessage sets the violation message template for violation on a prohibited value. You can set custom template parameters for injecting its values into the final message. Also, you can use default parameters:

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

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 (
	"context"
	"fmt"

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

func main() {
	v := "//example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsURL().WithRelativeSchema()))
	fmt.Println(err)
}
Output:

<nil>

func (URLConstraint) WithRestriction added in v0.14.0

func (c URLConstraint) WithRestriction(isAllowed func(u *url.URL) bool) URLConstraint

WithRestriction is used to additionally check parsed URL by callback function.

Example
package main

import (
	"context"
	"fmt"
	"net/url"

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

func main() {
	fmt.Println(
		"valid:",
		validator.Validate(
			context.Background(),
			validation.String("https://example.com", it.IsURL().WithRestriction(func(u *url.URL) bool {
				return u.Host == "example.com"
			})),
		),
	)
	fmt.Println(
		"invalid:",
		validator.Validate(
			context.Background(),
			validation.String("https://sample.com", it.IsURL().WithRestriction(func(u *url.URL) bool {
				return u.Host == "example.com"
			})),
		),
	)
}
Output:

valid: <nil>
invalid: violation: "This URL is prohibited to use."

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.

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	v := "ftp://example.com"
	err := validator.Validate(context.Background(), validation.String(v, it.IsURL().WithSchemas("http", "https", "ftp")))
	fmt.Println(err)
}
Output:

<nil>

type UniqueConstraint added in v0.4.0

type UniqueConstraint[T comparable] struct {
	// contains filtered or unexported fields
}

UniqueConstraint is used to check that all elements of the given collection are unique.

func HasUniqueValues added in v0.4.0

func HasUniqueValues[T comparable]() UniqueConstraint[T]

HasUniqueValues checks that all elements of the given collection are unique (none of them is present more than once).

Example
package main

import (
	"context"
	"fmt"

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

func main() {
	strings := []string{"foo", "bar", "baz", "foo"}
	ints := []int{1, 2, 3, 1}
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Comparables[string](strings, it.HasUniqueValues[string]()),
	))
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Comparables[int](ints, it.HasUniqueValues[int]()),
	))
}
Output:

violation: "This collection should contain only unique elements."
violation: "This collection should contain only unique elements."

func (UniqueConstraint[T]) ValidateComparables added in v0.9.0

func (c UniqueConstraint[T]) ValidateComparables(ctx context.Context, validator *validation.Validator, values []T) error

func (UniqueConstraint[T]) When added in v0.4.0

func (c UniqueConstraint[T]) When(condition bool) UniqueConstraint[T]

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

func (UniqueConstraint[T]) WhenGroups added in v0.8.0

func (c UniqueConstraint[T]) WhenGroups(groups ...string) UniqueConstraint[T]

WhenGroups enables conditional validation of the constraint by using the validation groups.

func (UniqueConstraint[T]) WithError added in v0.13.0

func (c UniqueConstraint[T]) WithError(err error) UniqueConstraint[T]

WithError overrides default error for produced violation.

func (UniqueConstraint[T]) WithMessage added in v0.13.0

func (c UniqueConstraint[T]) WithMessage(template string, parameters ...validation.TemplateParameter) UniqueConstraint[T]

WithMessage sets the violation message template. You can set custom template parameters for injecting its values into the final message.

Jump to

Keyboard shortcuts

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