it

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 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.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 (
	"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

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

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.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 (
	"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

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]) Code added in v0.3.0

func (c BlankConstraint[T]) Code(code string) BlankConstraint[T]

Code overrides default code for produced violation.

func (BlankConstraint[T]) Message

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

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

func (BlankConstraint[T]) ValidateBool

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

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

func (c BlankConstraint[T]) ValidateComparable(value *T, scope validation.Scope) error

func (BlankConstraint[T]) ValidateCountable

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

func (BlankConstraint[T]) ValidateNumber

func (c BlankConstraint[T]) ValidateNumber(value *T, scope validation.Scope) error

func (BlankConstraint[T]) ValidateString

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

func (BlankConstraint[T]) ValidateTime

func (c BlankConstraint[T]) ValidateTime(value *time.Time, scope validation.Scope) 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.

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) Code added in v0.3.0

func (c BoolConstraint) Code(code string) BoolConstraint

Code overrides default code for produced violation.

func (BoolConstraint) Message

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

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

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.

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.

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.

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.

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]) Code added in v0.3.0

func (c ChoiceConstraint[T]) Code(code string) ChoiceConstraint[T]

Code overrides default code for produced violation.

func (ChoiceConstraint[T]) Message

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

Message 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]) ValidateComparable added in v0.9.0

func (c ChoiceConstraint[T]) ValidateComparable(value *T, scope validation.Scope) error

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

func (c ChoiceConstraint[T]) ValidateNumber(value *T, scope validation.Scope) error

func (ChoiceConstraint[T]) ValidateString

func (c ChoiceConstraint[T]) ValidateString(value *T, scope validation.Scope) 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.

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 IsEqualToString

func IsEqualToString(value string) ComparisonConstraint[string]

IsEqualToString checks that the string value is equal to the specified string value. Deprecated: use IsEqualTo instead.

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.IsEqualToString("bar"))
	fmt.Println(err)
}
Output:

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 IsNotEqualToString

func IsNotEqualToString(value string) ComparisonConstraint[string]

IsNotEqualToString checks that the string value is not equal to the specified string value. Deprecated: use IsNotEqualTo instead.

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.IsNotEqualToString("foo"))
	fmt.Println(err)
}
Output:

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

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

Code overrides default code for produced violation.

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

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

Message 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.

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

func (c ComparisonConstraint[T]) ValidateComparable(value *T, scope validation.Scope) error

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

func (c ComparisonConstraint[T]) ValidateNumber(value *T, scope validation.Scope) error

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

func (c ComparisonConstraint[T]) ValidateString(value *T, scope validation.Scope) 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.

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) ExactCode added in v0.3.0

func (c CountConstraint) ExactCode(code string) CountConstraint

ExactCode overrides default code 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) ExactMessage

func (c CountConstraint) ExactMessage(template string, parameters ...validation.TemplateParameter) 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. 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) MaxCode added in v0.3.0

func (c CountConstraint) MaxCode(code string) CountConstraint

MaxCode overrides default code for violation that will be shown if the collection length is greater than the maximum value.

func (CountConstraint) MaxMessage

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

MaxMessage 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) MinCode added in v0.3.0

func (c CountConstraint) MinCode(code string) CountConstraint

MinCode overrides default code for violation that will be shown if the collection length is less than the minimum value.

func (CountConstraint) MinMessage

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

MinMessage 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.

func (CountConstraint) ValidateCountable

func (c CountConstraint) ValidateCountable(count int, 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.

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.

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) InvalidCode added in v0.3.0

func (c IPConstraint) InvalidCode(code string) IPConstraint

InvalidCode overrides default code for violation produced on invalid IP case.

func (IPConstraint) InvalidMessage added in v0.2.0

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

InvalidMessage 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) ProhibitedCode added in v0.3.0

func (c IPConstraint) ProhibitedCode(code string) IPConstraint

ProhibitedCode overrides default code for violation produced on prohibited IP case.

func (IPConstraint) ProhibitedMessage added in v0.2.0

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

ProhibitedMessage 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.

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.

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.

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) ExactCode added in v0.3.0

func (c LengthConstraint) ExactCode(code string) LengthConstraint

ExactCode overrides default code 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) ExactMessage

func (c LengthConstraint) ExactMessage(template string, parameters ...validation.TemplateParameter) 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. 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) MaxCode added in v0.3.0

func (c LengthConstraint) MaxCode(code string) LengthConstraint

MaxCode overrides default code for violation that will be shown if the string length is greater than the maximum value.

func (LengthConstraint) MaxMessage

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

MaxMessage 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) MinCode added in v0.3.0

func (c LengthConstraint) MinCode(code string) LengthConstraint

MinCode overrides default code for violation that will be shown if the string length is less than the minimum value.

func (LengthConstraint) MinMessage

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

MinMessage 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.

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.

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.

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]) Code added in v0.3.0

func (c NilConstraint[T]) Code(code string) NilConstraint[T]

Code overrides default code for produced violation.

func (NilConstraint[T]) Message

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

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

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

func (c NilConstraint[T]) ValidateBool(value *bool, scope validation.Scope) error

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

func (c NilConstraint[T]) ValidateComparable(value *T, scope validation.Scope) error

func (NilConstraint[T]) ValidateNil

func (c NilConstraint[T]) ValidateNil(isNil bool, scope validation.Scope) error

func (NilConstraint[T]) ValidateNumber

func (c NilConstraint[T]) ValidateNumber(value *T, scope validation.Scope) error

func (NilConstraint[T]) ValidateString

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

func (NilConstraint[T]) ValidateTime

func (c NilConstraint[T]) ValidateTime(value *time.Time, scope validation.Scope) 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.

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 AllowNil() 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]) AllowNil

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

AllowNil makes nil values valid.

func (NotBlankConstraint[T]) Code added in v0.3.0

func (c NotBlankConstraint[T]) Code(code string) NotBlankConstraint[T]

Code overrides default code for produced violation.

func (NotBlankConstraint[T]) Message

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

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

func (NotBlankConstraint[T]) ValidateBool

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

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

func (c NotBlankConstraint[T]) ValidateComparable(value *T, scope validation.Scope) error

func (NotBlankConstraint[T]) ValidateCountable

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

func (NotBlankConstraint[T]) ValidateNumber

func (c NotBlankConstraint[T]) ValidateNumber(value *T, scope validation.Scope) error

func (NotBlankConstraint[T]) ValidateString

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

func (NotBlankConstraint[T]) ValidateTime

func (c NotBlankConstraint[T]) ValidateTime(value *time.Time, scope validation.Scope) 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.

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]) Code added in v0.3.0

func (c NotNilConstraint[T]) Code(code string) NotNilConstraint[T]

Code overrides default code for produced violation.

func (NotNilConstraint[T]) Message

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

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

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

func (c NotNilConstraint[T]) ValidateBool(value *bool, scope validation.Scope) error

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

func (c NotNilConstraint[T]) ValidateComparable(value *T, scope validation.Scope) error

func (NotNilConstraint[T]) ValidateNil

func (c NotNilConstraint[T]) ValidateNil(isNil bool, scope validation.Scope) error

func (NotNilConstraint[T]) ValidateNumber

func (c NotNilConstraint[T]) ValidateNumber(value *T, scope validation.Scope) error

func (NotNilConstraint[T]) ValidateString

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

func (NotNilConstraint[T]) ValidateTime

func (c NotNilConstraint[T]) ValidateTime(value *time.Time, scope validation.Scope) 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.

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 IsEqualToNumber added in v0.9.0

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

IsEqualToNumber checks that the number is equal to the specified value. Deprecated: use IsEqualTo instead.

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.IsEqualToNumber(2))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](1.1, it.IsEqualToNumber(1.2))),
	)
}
Output:

violation: This value should be equal to 2.
violation: This value should be equal to 1.2.

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 IsNotEqualToNumber added in v0.9.0

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

IsNotEqualToNumber checks that the number is not equal to the specified value. Deprecated: use IsNotEqualTo instead.

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.IsNotEqualToNumber(1))),
	)
	fmt.Println(validator.Validate(
		context.Background(),
		validation.Number[float64](1.1, it.IsNotEqualToNumber(1.1))),
	)
}
Output:

violation: This value should not be equal to 1.
violation: This value should not be equal to 1.1.

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]) Code added in v0.3.0

Code overrides default code for produced violation.

func (NumberComparisonConstraint[T]) Message

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

Message 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.

func (NumberComparisonConstraint[T]) ValidateNumber

func (c NumberComparisonConstraint[T]) ValidateNumber(value *T, scope validation.Scope) 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.

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]) Code added in v0.3.0

func (c RangeConstraint[T]) Code(code string) RangeConstraint[T]

Code overrides default code for produced violation.

func (RangeConstraint[T]) Message

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

Message 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.

func (RangeConstraint[T]) Name

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

Name is the constraint name.

func (RangeConstraint[T]) ValidateNumber

func (c RangeConstraint[T]) ValidateNumber(value *T, scope validation.Scope) 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.

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

DoesNotMatch creates a RegexConstraint 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) RegexConstraint

Matches creates a RegexConstraint 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 (RegexConstraint) Code added in v0.3.0

func (c RegexConstraint) Code(code string) RegexConstraint

Code overrides default code for produced violation.

func (RegexConstraint) Message

func (c RegexConstraint) Message(template string, parameters ...validation.TemplateParameter) RegexConstraint

Message 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 (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.

func (RegexConstraint) WhenGroups added in v0.8.0

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

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

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) Code added in v0.3.0

Code overrides default code for produced violation.

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

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.

func (TimeComparisonConstraint) WhenGroups added in v0.8.0

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

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) Code added in v0.3.0

Code overrides default code for produced violation.

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(template string, parameters ...validation.TemplateParameter) TimeRangeConstraint

Message 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 Layout method. Default layout is time.RFC3339.

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.

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.

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) Code added in v0.3.0

func (c URLConstraint) Code(code string) URLConstraint

Code overrides default code for produced violation.

func (URLConstraint) Message added in v0.2.0

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

Message 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) 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) 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) 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) 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]) Code added in v0.4.0

func (c UniqueConstraint[T]) Code(code string) UniqueConstraint[T]

Code overrides default code for produced violation.

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

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

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

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

func (c UniqueConstraint[T]) ValidateComparables(values []T, scope validation.Scope) 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.

Jump to

Keyboard shortcuts

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