Documentation
¶
Overview ¶
Package it contains validation constraints that are used to validate specific types of values.
Index ¶
- func IsEmail() validation.CustomStringConstraint
- func IsHTML5Email() validation.CustomStringConstraint
- func IsHostname() validation.CustomStringConstraint
- func IsJSON() validation.CustomStringConstraint
- func IsLooseHostname() validation.CustomStringConstraint
- type BlankConstraint
- func (c BlankConstraint) Code(code string) BlankConstraint
- func (c BlankConstraint) Message(template string, parameters ...validation.TemplateParameter) BlankConstraint
- func (c BlankConstraint) Name() string
- func (c BlankConstraint) SetUp() error
- func (c BlankConstraint) ValidateBool(value *bool, scope validation.Scope) error
- func (c BlankConstraint) ValidateCountable(count int, scope validation.Scope) error
- func (c BlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
- func (c BlankConstraint) ValidateNil(scope validation.Scope) error
- func (c BlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
- func (c BlankConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c BlankConstraint) ValidateStrings(values []string, scope validation.Scope) error
- func (c BlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
- func (c BlankConstraint) When(condition bool) BlankConstraint
- type BoolConstraint
- func (c BoolConstraint) Code(code string) BoolConstraint
- func (c BoolConstraint) Message(template string, parameters ...validation.TemplateParameter) BoolConstraint
- func (c BoolConstraint) Name() string
- func (c BoolConstraint) SetUp() error
- func (c BoolConstraint) ValidateBool(value *bool, scope validation.Scope) error
- func (c BoolConstraint) When(condition bool) BoolConstraint
- type ChoiceConstraint
- func (c ChoiceConstraint) Code(code string) ChoiceConstraint
- func (c ChoiceConstraint) Message(template string, parameters ...validation.TemplateParameter) ChoiceConstraint
- func (c ChoiceConstraint) Name() string
- func (c ChoiceConstraint) SetUp() error
- func (c ChoiceConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c ChoiceConstraint) When(condition bool) ChoiceConstraint
- type CountConstraint
- func (c CountConstraint) ExactCode(code string) CountConstraint
- func (c CountConstraint) ExactMessage(template string, parameters ...validation.TemplateParameter) CountConstraint
- func (c CountConstraint) MaxCode(code string) CountConstraint
- func (c CountConstraint) MaxMessage(template string, parameters ...validation.TemplateParameter) CountConstraint
- func (c CountConstraint) MinCode(code string) CountConstraint
- func (c CountConstraint) MinMessage(template string, parameters ...validation.TemplateParameter) CountConstraint
- func (c CountConstraint) Name() string
- func (c CountConstraint) SetUp() error
- func (c CountConstraint) ValidateCountable(count int, scope validation.Scope) error
- func (c CountConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
- func (c CountConstraint) When(condition bool) CountConstraint
- type IPConstraint
- func (c IPConstraint) DenyIP(restrict func(ip net.IP) bool) IPConstraint
- func (c IPConstraint) DenyPrivateIP() IPConstraint
- func (c IPConstraint) InvalidCode(code string) IPConstraint
- func (c IPConstraint) InvalidMessage(template string, parameters ...validation.TemplateParameter) IPConstraint
- func (c IPConstraint) Name() string
- func (c IPConstraint) ProhibitedCode(code string) IPConstraint
- func (c IPConstraint) ProhibitedMessage(template string, parameters ...validation.TemplateParameter) IPConstraint
- func (c IPConstraint) SetUp() error
- func (c IPConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c IPConstraint) When(condition bool) IPConstraint
- type LengthConstraint
- func (c LengthConstraint) ExactCode(code string) LengthConstraint
- func (c LengthConstraint) ExactMessage(template string, parameters ...validation.TemplateParameter) LengthConstraint
- func (c LengthConstraint) MaxCode(code string) LengthConstraint
- func (c LengthConstraint) MaxMessage(template string, parameters ...validation.TemplateParameter) LengthConstraint
- func (c LengthConstraint) MinCode(code string) LengthConstraint
- func (c LengthConstraint) MinMessage(template string, parameters ...validation.TemplateParameter) LengthConstraint
- func (c LengthConstraint) Name() string
- func (c LengthConstraint) SetUp() error
- func (c LengthConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c LengthConstraint) When(condition bool) LengthConstraint
- type NilConstraint
- func (c NilConstraint) Code(code string) NilConstraint
- func (c NilConstraint) Message(template string, parameters ...validation.TemplateParameter) NilConstraint
- func (c NilConstraint) Name() string
- func (c NilConstraint) SetUp() error
- func (c NilConstraint) ValidateBool(value *bool, scope validation.Scope) error
- func (c NilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
- func (c NilConstraint) ValidateNil(scope validation.Scope) error
- func (c NilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
- func (c NilConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c NilConstraint) ValidateStrings(values []string, scope validation.Scope) error
- func (c NilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
- func (c NilConstraint) When(condition bool) NilConstraint
- type NotBlankConstraint
- func (c NotBlankConstraint) AllowNil() NotBlankConstraint
- func (c NotBlankConstraint) Code(code string) NotBlankConstraint
- func (c NotBlankConstraint) Message(template string, parameters ...validation.TemplateParameter) NotBlankConstraint
- func (c NotBlankConstraint) Name() string
- func (c NotBlankConstraint) SetUp() error
- func (c NotBlankConstraint) ValidateBool(value *bool, scope validation.Scope) error
- func (c NotBlankConstraint) ValidateCountable(count int, scope validation.Scope) error
- func (c NotBlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
- func (c NotBlankConstraint) ValidateNil(scope validation.Scope) error
- func (c NotBlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
- func (c NotBlankConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c NotBlankConstraint) ValidateStrings(values []string, scope validation.Scope) error
- func (c NotBlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
- func (c NotBlankConstraint) When(condition bool) NotBlankConstraint
- type NotNilConstraint
- func (c NotNilConstraint) Code(code string) NotNilConstraint
- func (c NotNilConstraint) Message(template string, parameters ...validation.TemplateParameter) NotNilConstraint
- func (c NotNilConstraint) Name() string
- func (c NotNilConstraint) SetUp() error
- func (c NotNilConstraint) ValidateBool(value *bool, scope validation.Scope) error
- func (c NotNilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
- func (c NotNilConstraint) ValidateNil(scope validation.Scope) error
- func (c NotNilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
- func (c NotNilConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c NotNilConstraint) ValidateStrings(values []string, scope validation.Scope) error
- func (c NotNilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
- func (c NotNilConstraint) When(condition bool) NotNilConstraint
- type NumberComparisonConstraint
- func IsEqualToFloat(value float64) NumberComparisonConstraint
- func IsEqualToInteger(value int64) NumberComparisonConstraint
- func IsGreaterThanFloat(value float64) NumberComparisonConstraint
- func IsGreaterThanInteger(value int64) NumberComparisonConstraint
- func IsGreaterThanOrEqualFloat(value float64) NumberComparisonConstraint
- func IsGreaterThanOrEqualInteger(value int64) NumberComparisonConstraint
- func IsLessThanFloat(value float64) NumberComparisonConstraint
- func IsLessThanInteger(value int64) NumberComparisonConstraint
- func IsLessThanOrEqualFloat(value float64) NumberComparisonConstraint
- func IsLessThanOrEqualInteger(value int64) NumberComparisonConstraint
- func IsNegative() NumberComparisonConstraint
- func IsNegativeOrZero() NumberComparisonConstraint
- func IsNotEqualToFloat(value float64) NumberComparisonConstraint
- func IsNotEqualToInteger(value int64) NumberComparisonConstraint
- func IsPositive() NumberComparisonConstraint
- func IsPositiveOrZero() NumberComparisonConstraint
- func (c NumberComparisonConstraint) Code(code string) NumberComparisonConstraint
- func (c NumberComparisonConstraint) Message(template string, parameters ...validation.TemplateParameter) NumberComparisonConstraint
- func (c NumberComparisonConstraint) Name() string
- func (c NumberComparisonConstraint) SetUp() error
- func (c NumberComparisonConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
- func (c NumberComparisonConstraint) When(condition bool) NumberComparisonConstraint
- type RangeConstraint
- func (c RangeConstraint) Code(code string) RangeConstraint
- func (c RangeConstraint) Message(template string, parameters ...validation.TemplateParameter) RangeConstraint
- func (c RangeConstraint) Name() string
- func (c RangeConstraint) SetUp() error
- func (c RangeConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
- func (c RangeConstraint) When(condition bool) RangeConstraint
- type RegexConstraint
- func (c RegexConstraint) Code(code string) RegexConstraint
- func (c RegexConstraint) Message(template string, parameters ...validation.TemplateParameter) RegexConstraint
- func (c RegexConstraint) Name() string
- func (c RegexConstraint) SetUp() error
- func (c RegexConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c RegexConstraint) When(condition bool) RegexConstraint
- type StringComparisonConstraint
- func (c StringComparisonConstraint) Code(code string) StringComparisonConstraint
- func (c StringComparisonConstraint) Message(template string, parameters ...validation.TemplateParameter) StringComparisonConstraint
- func (c StringComparisonConstraint) Name() string
- func (c StringComparisonConstraint) SetUp() error
- func (c StringComparisonConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c StringComparisonConstraint) When(condition bool) StringComparisonConstraint
- type TimeComparisonConstraint
- func (c TimeComparisonConstraint) Code(code string) TimeComparisonConstraint
- func (c TimeComparisonConstraint) Layout(layout string) TimeComparisonConstraint
- func (c TimeComparisonConstraint) Message(template string, parameters ...validation.TemplateParameter) TimeComparisonConstraint
- func (c TimeComparisonConstraint) Name() string
- func (c TimeComparisonConstraint) SetUp() error
- func (c TimeComparisonConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
- func (c TimeComparisonConstraint) When(condition bool) TimeComparisonConstraint
- type TimeRangeConstraint
- func (c TimeRangeConstraint) Code(code string) TimeRangeConstraint
- func (c TimeRangeConstraint) Layout(layout string) TimeRangeConstraint
- func (c TimeRangeConstraint) Message(template string, parameters ...validation.TemplateParameter) TimeRangeConstraint
- func (c TimeRangeConstraint) Name() string
- func (c TimeRangeConstraint) SetUp() error
- func (c TimeRangeConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
- func (c TimeRangeConstraint) When(condition bool) TimeRangeConstraint
- type URLConstraint
- func (c URLConstraint) Code(code string) URLConstraint
- func (c URLConstraint) Message(template string, parameters ...validation.TemplateParameter) URLConstraint
- func (c URLConstraint) Name() string
- func (c URLConstraint) SetUp() error
- func (c URLConstraint) ValidateString(value *string, scope validation.Scope) error
- func (c URLConstraint) When(condition bool) URLConstraint
- func (c URLConstraint) WithRelativeSchema() URLConstraint
- func (c URLConstraint) WithSchemas(schemas ...string) URLConstraint
- type UniqueConstraint
- func (c UniqueConstraint) Code(code string) UniqueConstraint
- func (c UniqueConstraint) Message(template string, parameters ...validation.TemplateParameter) UniqueConstraint
- func (c UniqueConstraint) Name() string
- func (c UniqueConstraint) SetUp() error
- func (c UniqueConstraint) ValidateStrings(values []string, scope validation.Scope) error
- func (c UniqueConstraint) When(condition bool) UniqueConstraint
Examples ¶
- DoesNotMatch
- HasCountBetween
- HasExactCount
- HasExactLength
- HasLengthBetween
- HasMaxCount
- HasMaxLength
- HasMinCount
- HasMinLength
- HasUniqueValues
- IPConstraint.DenyIP
- IPConstraint.DenyPrivateIP (RestrictedPrivateIPv4)
- IPConstraint.DenyPrivateIP (RestrictedPrivateIPv6)
- IsBetweenFloats
- IsBetweenIntegers
- IsBetweenTime
- IsBlank
- IsEarlierThan
- IsEarlierThanOrEqual
- IsEmail (InvalidEmail)
- IsEmail (ValidEmail)
- IsEqualToFloat
- IsEqualToInteger
- IsEqualToString
- IsFalse
- IsGreaterThanFloat
- IsGreaterThanInteger
- IsGreaterThanOrEqualFloat
- IsGreaterThanOrEqualInteger
- IsHTML5Email (InvalidEmail)
- IsHTML5Email (ValidEmail)
- IsHostname (InvalidHostname)
- IsHostname (ReservedHostname)
- IsHostname (ValidHostname)
- IsIP (InvalidIP)
- IsIP (ValidIP)
- IsIPv4 (InvalidIP)
- IsIPv4 (ValidIP)
- IsIPv6 (InvalidIP)
- IsIPv6 (ValidIP)
- IsJSON (InvalidJSON)
- IsJSON (ValidJSON)
- IsLaterThan
- IsLaterThanOrEqual
- IsLessThanFloat
- IsLessThanInteger
- IsLessThanOrEqualFloat
- IsLessThanOrEqualInteger
- IsLooseHostname (InvalidHostname)
- IsLooseHostname (ReservedHostname)
- IsLooseHostname (ValidHostname)
- IsNegative
- IsNegativeOrZero
- IsNil
- IsNotBlank
- IsNotEqualToFloat
- IsNotEqualToInteger
- IsNotEqualToString
- IsNotNil
- IsOneOfStrings
- IsPositive
- IsPositiveOrZero
- IsTrue
- IsURL (InvalidURL)
- IsURL (ValidURL)
- Matches
- URLConstraint.WithRelativeSchema
- URLConstraint.WithSchemas
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsEmail ¶ added in v0.2.0
func IsEmail() validation.CustomStringConstraint
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
func IsHostname() validation.CustomStringConstraint
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 IsJSON ¶ added in v0.2.0
func IsJSON() validation.CustomStringConstraint
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>
Types ¶
type BlankConstraint ¶
type BlankConstraint struct {
// contains filtered or unexported fields
}
BlankConstraint checks that a value is blank: equal to false, nil, zero, an empty string, an empty slice, array, or a map.
func IsBlank ¶
func IsBlank() BlankConstraint
IsBlank creates a BlankConstraint for checking that value is empty.
Example ¶
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.IsBlank())) fmt.Println(err) }
Output: violation: This value should be blank.
func (BlankConstraint) Code ¶ added in v0.3.0
func (c BlankConstraint) Code(code string) BlankConstraint
Code overrides default code for produced violation.
func (BlankConstraint) Message ¶
func (c BlankConstraint) Message(template string, parameters ...validation.TemplateParameter) BlankConstraint
Message sets the violation message template. You can set custom template parameters for injecting its values into the final message.
func (BlankConstraint) SetUp ¶
func (c BlankConstraint) SetUp() error
SetUp always returns no error.
func (BlankConstraint) ValidateBool ¶
func (c BlankConstraint) ValidateBool(value *bool, scope validation.Scope) error
func (BlankConstraint) ValidateCountable ¶
func (c BlankConstraint) ValidateCountable(count int, scope validation.Scope) error
func (BlankConstraint) ValidateIterable ¶
func (c BlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
func (BlankConstraint) ValidateNil ¶
func (c BlankConstraint) ValidateNil(scope validation.Scope) error
func (BlankConstraint) ValidateNumber ¶
func (c BlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
func (BlankConstraint) ValidateString ¶
func (c BlankConstraint) ValidateString(value *string, scope validation.Scope) error
func (BlankConstraint) ValidateStrings ¶ added in v0.4.0
func (c BlankConstraint) ValidateStrings(values []string, scope validation.Scope) error
func (BlankConstraint) ValidateTime ¶
func (c BlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
func (BlankConstraint) When ¶
func (c BlankConstraint) When(condition bool) BlankConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type BoolConstraint ¶
type BoolConstraint struct {
// contains filtered or unexported fields
}
BoolConstraint checks that a bool value in strictly equal to expected bool value.
func IsFalse ¶
func IsFalse() BoolConstraint
IsFalse creates a BoolConstraint to check that a value is not strictly equal to false.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { b := true err := validator.Validate(context.Background(), validation.Bool(b, 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() { b := false err := validator.Validate(context.Background(), validation.Bool(b, 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.
type ChoiceConstraint ¶
type ChoiceConstraint struct {
// contains filtered or unexported fields
}
ChoiceConstraint is used to ensure that the given value corresponds to one of the expected choices.
func IsOneOfStrings ¶
func IsOneOfStrings(values ...string) ChoiceConstraint
IsOneOfStrings creates a ChoiceConstraint for checking that values are in the expected list of strings.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { s := "foo" err := validator.ValidateString(context.Background(), s, it.IsOneOfStrings("one", "two", "three")) fmt.Println(err) }
Output: violation: The value you selected is not a valid choice.
func (ChoiceConstraint) Code ¶ added in v0.3.0
func (c ChoiceConstraint) Code(code string) ChoiceConstraint
Code overrides default code for produced violation.
func (ChoiceConstraint) Message ¶
func (c ChoiceConstraint) Message(template string, parameters ...validation.TemplateParameter) ChoiceConstraint
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) SetUp ¶
func (c ChoiceConstraint) SetUp() error
SetUp will return an error if the list of choices is empty.
func (ChoiceConstraint) ValidateString ¶
func (c ChoiceConstraint) ValidateString(value *string, scope validation.Scope) error
func (ChoiceConstraint) When ¶
func (c ChoiceConstraint) When(condition bool) ChoiceConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type CountConstraint ¶
type CountConstraint struct {
// contains filtered or unexported fields
}
CountConstraint checks that a given collection's (array, slice or a map) length is between some minimum and maximum value.
func HasCountBetween ¶
func HasCountBetween(min int, max int) CountConstraint
HasCountBetween creates a CountConstraint that checks the length of the iterable (slice, array, or map) is between some minimum and maximum value.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := []int{1, 2} err := validator.ValidateIterable(context.Background(), 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.ValidateIterable(context.Background(), 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.ValidateIterable(context.Background(), 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.ValidateIterable(context.Background(), 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) SetUp ¶
func (c CountConstraint) SetUp() error
SetUp always returns no error.
func (CountConstraint) ValidateCountable ¶
func (c CountConstraint) ValidateCountable(count int, scope validation.Scope) error
func (CountConstraint) ValidateIterable ¶
func (c CountConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
func (CountConstraint) When ¶
func (c CountConstraint) When(condition bool) CountConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type IPConstraint ¶ added in v0.2.0
type IPConstraint struct {
// contains filtered or unexported fields
}
IPConstraint is used to validate IP address. You can check for different versions and restrict some ranges by additional options.
func IsIP ¶ added in v0.2.0
func IsIP() IPConstraint
IsIP creates an IPConstraint to validate an IP address (IPv4 or IPv6).
Example (InvalidIP) ¶
package main import ( "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) Name ¶ added in v0.2.0
func (c IPConstraint) Name() string
Name is the constraint name.
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) SetUp ¶ added in v0.2.0
func (c IPConstraint) SetUp() error
SetUp always returns no error.
func (IPConstraint) ValidateString ¶ added in v0.2.0
func (c IPConstraint) ValidateString(value *string, scope validation.Scope) error
func (IPConstraint) When ¶ added in v0.2.0
func (c IPConstraint) When(condition bool) IPConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type LengthConstraint ¶
type LengthConstraint struct {
// contains filtered or unexported fields
}
LengthConstraint checks that a given string length is between some minimum and maximum value. If you want to check the length of the array, slice or a map use CountConstraint.
func HasExactLength ¶
func HasExactLength(count int) LengthConstraint
HasExactLength creates a LengthConstraint that checks the length of the string has exact value.
Example ¶
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) SetUp ¶
func (c LengthConstraint) SetUp() error
SetUp always returns no error.
func (LengthConstraint) ValidateString ¶
func (c LengthConstraint) ValidateString(value *string, scope validation.Scope) error
func (LengthConstraint) When ¶
func (c LengthConstraint) When(condition bool) LengthConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type NilConstraint ¶
type NilConstraint struct {
// contains filtered or unexported fields
}
NilConstraint checks that a value in strictly equal to nil. To check that values in blank use BlankConstraint.
func IsNil ¶
func IsNil() NilConstraint
IsNil creates a NilConstraint to check that a value is strictly equal to nil.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { s := "" err := validator.Validate(context.Background(), validation.NilString(&s, it.IsNil())) fmt.Println(err) }
Output: violation: This value should be nil.
func (NilConstraint) Code ¶ added in v0.3.0
func (c NilConstraint) Code(code string) NilConstraint
Code overrides default code for produced violation.
func (NilConstraint) Message ¶
func (c NilConstraint) Message(template string, parameters ...validation.TemplateParameter) NilConstraint
Message sets the violation message template. You can set custom template parameters for injecting its values into the final message.
func (NilConstraint) ValidateBool ¶ added in v0.4.0
func (c NilConstraint) ValidateBool(value *bool, scope validation.Scope) error
func (NilConstraint) ValidateIterable ¶
func (c NilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
func (NilConstraint) ValidateNil ¶
func (c NilConstraint) ValidateNil(scope validation.Scope) error
func (NilConstraint) ValidateNumber ¶
func (c NilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
func (NilConstraint) ValidateString ¶
func (c NilConstraint) ValidateString(value *string, scope validation.Scope) error
func (NilConstraint) ValidateStrings ¶ added in v0.4.0
func (c NilConstraint) ValidateStrings(values []string, scope validation.Scope) error
func (NilConstraint) ValidateTime ¶
func (c NilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
func (NilConstraint) When ¶
func (c NilConstraint) When(condition bool) NilConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type NotBlankConstraint ¶
type NotBlankConstraint struct {
// contains filtered or unexported fields
}
NotBlankConstraint checks that a value is not blank: not equal to zero, an empty string, an empty slice/array, an empty map, false or nil. Nil behavior is configurable via AllowNil() method. To check that a value is not nil only use NotNilConstraint.
func IsNotBlank ¶
func IsNotBlank() NotBlankConstraint
IsNotBlank creates a NotBlankConstraint for checking that value is not empty.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := "" err := validator.Validate(context.Background(), validation.String(v, it.IsNotBlank())) fmt.Println(err) }
Output: violation: This value should not be blank.
func (NotBlankConstraint) AllowNil ¶
func (c NotBlankConstraint) AllowNil() NotBlankConstraint
AllowNil makes nil values valid.
func (NotBlankConstraint) Code ¶ added in v0.3.0
func (c NotBlankConstraint) Code(code string) NotBlankConstraint
Code overrides default code for produced violation.
func (NotBlankConstraint) Message ¶
func (c NotBlankConstraint) Message(template string, parameters ...validation.TemplateParameter) NotBlankConstraint
Message sets the violation message template. You can set custom template parameters for injecting its values into the final message.
func (NotBlankConstraint) Name ¶
func (c NotBlankConstraint) Name() string
Name is the constraint name.
func (NotBlankConstraint) SetUp ¶
func (c NotBlankConstraint) SetUp() error
SetUp always returns no error.
func (NotBlankConstraint) ValidateBool ¶
func (c NotBlankConstraint) ValidateBool(value *bool, scope validation.Scope) error
func (NotBlankConstraint) ValidateCountable ¶
func (c NotBlankConstraint) ValidateCountable(count int, scope validation.Scope) error
func (NotBlankConstraint) ValidateIterable ¶
func (c NotBlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
func (NotBlankConstraint) ValidateNil ¶
func (c NotBlankConstraint) ValidateNil(scope validation.Scope) error
func (NotBlankConstraint) ValidateNumber ¶
func (c NotBlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
func (NotBlankConstraint) ValidateString ¶
func (c NotBlankConstraint) ValidateString(value *string, scope validation.Scope) error
func (NotBlankConstraint) ValidateStrings ¶ added in v0.4.0
func (c NotBlankConstraint) ValidateStrings(values []string, scope validation.Scope) error
func (NotBlankConstraint) ValidateTime ¶
func (c NotBlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
func (NotBlankConstraint) When ¶
func (c NotBlankConstraint) When(condition bool) NotBlankConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type NotNilConstraint ¶
type NotNilConstraint struct {
// contains filtered or unexported fields
}
NotNilConstraint checks that a value in not strictly equal to nil. To check that values in not blank use NotBlankConstraint.
func IsNotNil ¶
func IsNotNil() NotNilConstraint
IsNotNil creates a NotNilConstraint to check that a value is not strictly equal to nil.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { var s *string err := validator.Validate(context.Background(), validation.NilString(s, it.IsNotNil())) fmt.Println(err) }
Output: violation: This value should not be nil.
func (NotNilConstraint) Code ¶ added in v0.3.0
func (c NotNilConstraint) Code(code string) NotNilConstraint
Code overrides default code for produced violation.
func (NotNilConstraint) Message ¶
func (c NotNilConstraint) Message(template string, parameters ...validation.TemplateParameter) NotNilConstraint
Message sets the violation message template. You can set custom template parameters for injecting its values into the final message.
func (NotNilConstraint) SetUp ¶
func (c NotNilConstraint) SetUp() error
SetUp always returns no error.
func (NotNilConstraint) ValidateBool ¶ added in v0.4.0
func (c NotNilConstraint) ValidateBool(value *bool, scope validation.Scope) error
func (NotNilConstraint) ValidateIterable ¶
func (c NotNilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error
func (NotNilConstraint) ValidateNil ¶
func (c NotNilConstraint) ValidateNil(scope validation.Scope) error
func (NotNilConstraint) ValidateNumber ¶
func (c NotNilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
func (NotNilConstraint) ValidateString ¶
func (c NotNilConstraint) ValidateString(value *string, scope validation.Scope) error
func (NotNilConstraint) ValidateStrings ¶ added in v0.4.0
func (c NotNilConstraint) ValidateStrings(values []string, scope validation.Scope) error
func (NotNilConstraint) ValidateTime ¶
func (c NotNilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
func (NotNilConstraint) When ¶
func (c NotNilConstraint) When(condition bool) NotNilConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type NumberComparisonConstraint ¶
type NumberComparisonConstraint struct {
// contains filtered or unexported fields
}
NumberComparisonConstraint is used for various numeric comparisons between integer and float values. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
func IsEqualToFloat ¶
func IsEqualToFloat(value float64) NumberComparisonConstraint
IsEqualToFloat checks that the number (integer or float) is equal to the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1.1 err := validator.ValidateNumber(context.Background(), v, it.IsEqualToFloat(1.2)) fmt.Println(err) }
Output: violation: This value should be equal to 1.2.
func IsEqualToInteger ¶
func IsEqualToInteger(value int64) NumberComparisonConstraint
IsEqualToInteger checks that the number (integer or float) is equal to the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1 err := validator.ValidateNumber(context.Background(), v, it.IsEqualToInteger(2)) fmt.Println(err) }
Output: violation: This value should be equal to 2.
func IsGreaterThanFloat ¶
func IsGreaterThanFloat(value float64) NumberComparisonConstraint
IsGreaterThanFloat checks that the number (integer or float) is greater than the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1.1 err := validator.ValidateNumber(context.Background(), v, it.IsGreaterThanFloat(1.1)) fmt.Println(err) }
Output: violation: This value should be greater than 1.1.
func IsGreaterThanInteger ¶
func IsGreaterThanInteger(value int64) NumberComparisonConstraint
IsGreaterThanInteger checks that the number (integer or float) is greater than the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1 err := validator.ValidateNumber(context.Background(), v, it.IsGreaterThanInteger(1)) fmt.Println(err) }
Output: violation: This value should be greater than 1.
func IsGreaterThanOrEqualFloat ¶
func IsGreaterThanOrEqualFloat(value float64) NumberComparisonConstraint
IsGreaterThanOrEqualFloat checks that the number (integer or float) is greater than or equal to the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1.1 err := validator.ValidateNumber(context.Background(), v, it.IsGreaterThanOrEqualFloat(1.2)) fmt.Println(err) }
Output: violation: This value should be greater than or equal to 1.2.
func IsGreaterThanOrEqualInteger ¶
func IsGreaterThanOrEqualInteger(value int64) NumberComparisonConstraint
IsGreaterThanOrEqualInteger checks that the number (integer or float) is greater than or equal to the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1 err := validator.ValidateNumber(context.Background(), v, it.IsGreaterThanOrEqualInteger(2)) fmt.Println(err) }
Output: violation: This value should be greater than or equal to 2.
func IsLessThanFloat ¶
func IsLessThanFloat(value float64) NumberComparisonConstraint
IsLessThanFloat checks that the number (integer or float) is less than the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1.1 err := validator.ValidateNumber(context.Background(), v, it.IsLessThanFloat(1.1)) fmt.Println(err) }
Output: violation: This value should be less than 1.1.
func IsLessThanInteger ¶
func IsLessThanInteger(value int64) NumberComparisonConstraint
IsLessThanInteger checks that the number (integer or float) is less than the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1 err := validator.ValidateNumber(context.Background(), v, it.IsLessThanInteger(1)) fmt.Println(err) }
Output: violation: This value should be less than 1.
func IsLessThanOrEqualFloat ¶
func IsLessThanOrEqualFloat(value float64) NumberComparisonConstraint
IsLessThanOrEqualFloat checks that the number (integer or float) is less than or equal to the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1.1 err := validator.ValidateNumber(context.Background(), v, it.IsLessThanOrEqualFloat(0.1)) fmt.Println(err) }
Output: violation: This value should be less than or equal to 0.1.
func IsLessThanOrEqualInteger ¶
func IsLessThanOrEqualInteger(value int64) NumberComparisonConstraint
IsLessThanOrEqualInteger checks that the number (integer or float) is less than or equal to the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1 err := validator.ValidateNumber(context.Background(), v, it.IsLessThanOrEqualInteger(0)) fmt.Println(err) }
Output: violation: This value should be less than or equal to 0.
func IsNegative ¶
func IsNegative() NumberComparisonConstraint
IsNegative checks that the value is a negative number (integer or float). Zero is neither positive nor negative. If you want to allow zero use IsNegativeOrZero comparison.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1 err := validator.ValidateNumber(context.Background(), v, it.IsNegative()) fmt.Println(err) }
Output: violation: This value should be negative.
func IsNegativeOrZero ¶
func IsNegativeOrZero() NumberComparisonConstraint
IsNegativeOrZero checks that the value is a negative number (integer or float) or equal to zero. If you don't want to allow zero as a valid value, use IsNegative comparison.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1 err := validator.ValidateNumber(context.Background(), v, it.IsNegativeOrZero()) fmt.Println(err) }
Output: violation: This value should be either negative or zero.
func IsNotEqualToFloat ¶
func IsNotEqualToFloat(value float64) NumberComparisonConstraint
IsNotEqualToFloat checks that the number (integer or float) is not equal to the specified float value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1.1 err := validator.ValidateNumber(context.Background(), v, it.IsNotEqualToFloat(1.1)) fmt.Println(err) }
Output: violation: This value should not be equal to 1.1.
func IsNotEqualToInteger ¶
func IsNotEqualToInteger(value int64) NumberComparisonConstraint
IsNotEqualToInteger checks that the number (integer or float) is not equal to the specified integer value. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1 err := validator.ValidateNumber(context.Background(), v, it.IsNotEqualToInteger(1)) fmt.Println(err) }
Output: violation: This value should not be equal to 1.
func IsPositive ¶
func IsPositive() NumberComparisonConstraint
IsPositive checks that the value is a positive number (integer or float). Zero is neither positive nor negative. If you want to allow zero use IsPositiveOrZero comparison.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := -1 err := validator.ValidateNumber(context.Background(), v, it.IsPositive()) fmt.Println(err) }
Output: violation: This value should be positive.
func IsPositiveOrZero ¶
func IsPositiveOrZero() NumberComparisonConstraint
IsPositiveOrZero checks that the value is a positive number (integer or float) or equal to zero. If you don't want to allow zero as a valid value, use IsPositive comparison.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := -1 err := validator.ValidateNumber(context.Background(), v, it.IsPositiveOrZero()) fmt.Println(err) }
Output: violation: This value should be either positive or zero.
func (NumberComparisonConstraint) Code ¶ added in v0.3.0
func (c NumberComparisonConstraint) Code(code string) NumberComparisonConstraint
Code overrides default code for produced violation.
func (NumberComparisonConstraint) Message ¶
func (c NumberComparisonConstraint) Message( template string, parameters ...validation.TemplateParameter, ) NumberComparisonConstraint
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) Name ¶
func (c NumberComparisonConstraint) Name() string
Name is the constraint name.
func (NumberComparisonConstraint) SetUp ¶
func (c NumberComparisonConstraint) SetUp() error
SetUp always returns no error.
func (NumberComparisonConstraint) ValidateNumber ¶
func (c NumberComparisonConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
func (NumberComparisonConstraint) When ¶
func (c NumberComparisonConstraint) When(condition bool) NumberComparisonConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type RangeConstraint ¶
type RangeConstraint struct {
// contains filtered or unexported fields
}
RangeConstraint is used to check that a given number value is between some minimum and maximum. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
func IsBetweenFloats ¶
func IsBetweenFloats(min, max float64) RangeConstraint
IsBetweenFloats checks that the number (integer or float) is between specified minimum and maximum float values. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1.1 err := validator.ValidateNumber(context.Background(), v, it.IsBetweenFloats(10.111, 20.222)) fmt.Println(err) }
Output: violation: This value should be between 10.111 and 20.222.
func IsBetweenIntegers ¶
func IsBetweenIntegers(min, max int64) RangeConstraint
IsBetweenIntegers checks that the number (integer or float) is between specified minimum and maximum integer values. Values are compared as integers if the compared and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
Example ¶
package main import ( "context" "fmt" "github.com/muonsoft/validation/it" "github.com/muonsoft/validation/validator" ) func main() { v := 1 err := validator.ValidateNumber(context.Background(), v, it.IsBetweenIntegers(10, 20)) fmt.Println(err) }
Output: violation: This value should be between 10 and 20.
func (RangeConstraint) Code ¶ added in v0.3.0
func (c RangeConstraint) Code(code string) RangeConstraint
Code overrides default code for produced violation.
func (RangeConstraint) Message ¶
func (c RangeConstraint) Message(template string, parameters ...validation.TemplateParameter) RangeConstraint
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) SetUp ¶
func (c RangeConstraint) SetUp() error
SetUp returns an error if min is greater than or equal to max.
func (RangeConstraint) ValidateNumber ¶
func (c RangeConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error
func (RangeConstraint) When ¶
func (c RangeConstraint) When(condition bool) RangeConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type RegexConstraint ¶
type RegexConstraint struct {
// contains filtered or unexported fields
}
RegexConstraint is used to ensure that the given value corresponds to regex pattern.
func DoesNotMatch ¶
func DoesNotMatch(regex *regexp.Regexp) RegexConstraint
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) SetUp ¶
func (c RegexConstraint) SetUp() error
SetUp will return an error if the pattern is empty.
func (RegexConstraint) ValidateString ¶
func (c RegexConstraint) ValidateString(value *string, scope validation.Scope) error
func (RegexConstraint) When ¶
func (c RegexConstraint) When(condition bool) RegexConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type StringComparisonConstraint ¶
type StringComparisonConstraint struct {
// contains filtered or unexported fields
}
StringComparisonConstraint is used to compare strings.
func IsEqualToString ¶
func IsEqualToString(value string) StringComparisonConstraint
IsEqualToString checks that the string value is equal to the specified string value.
Example ¶
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 IsNotEqualToString ¶
func IsNotEqualToString(value string) StringComparisonConstraint
IsNotEqualToString checks that the string value is not equal to the specified string 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.IsNotEqualToString("foo")) fmt.Println(err) }
Output: violation: This value should not be equal to "foo".
func (StringComparisonConstraint) Code ¶ added in v0.3.0
func (c StringComparisonConstraint) Code(code string) StringComparisonConstraint
Code overrides default code for produced violation.
func (StringComparisonConstraint) Message ¶
func (c StringComparisonConstraint) Message( template string, parameters ...validation.TemplateParameter, ) StringComparisonConstraint
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 string values are quoted strings.
func (StringComparisonConstraint) Name ¶
func (c StringComparisonConstraint) Name() string
Name is the constraint name.
func (StringComparisonConstraint) SetUp ¶
func (c StringComparisonConstraint) SetUp() error
SetUp always returns no error.
func (StringComparisonConstraint) ValidateString ¶
func (c StringComparisonConstraint) ValidateString(value *string, scope validation.Scope) error
func (StringComparisonConstraint) When ¶
func (c StringComparisonConstraint) When(condition bool) StringComparisonConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type TimeComparisonConstraint ¶
type TimeComparisonConstraint struct {
// contains filtered or unexported fields
}
TimeComparisonConstraint is used to compare time values.
func IsEarlierThan ¶
func IsEarlierThan(value time.Time) TimeComparisonConstraint
IsEarlierThan checks that the given time is earlier than the specified value.
Example ¶
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
func (c TimeComparisonConstraint) Code(code string) TimeComparisonConstraint
Code overrides default code for produced violation.
func (TimeComparisonConstraint) Layout ¶
func (c TimeComparisonConstraint) Layout(layout string) TimeComparisonConstraint
Layout can be used to set the layout that is used to format time values.
func (TimeComparisonConstraint) Message ¶
func (c TimeComparisonConstraint) Message( template string, parameters ...validation.TemplateParameter, ) TimeComparisonConstraint
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) Name ¶
func (c TimeComparisonConstraint) Name() string
Name is the constraint name.
func (TimeComparisonConstraint) SetUp ¶
func (c TimeComparisonConstraint) SetUp() error
SetUp always returns no error.
func (TimeComparisonConstraint) ValidateTime ¶
func (c TimeComparisonConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
func (TimeComparisonConstraint) When ¶
func (c TimeComparisonConstraint) When(condition bool) TimeComparisonConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type TimeRangeConstraint ¶
type TimeRangeConstraint struct {
// contains filtered or unexported fields
}
TimeRangeConstraint is used to check that a given time value is between some minimum and maximum.
func IsBetweenTime ¶
func IsBetweenTime(min, max time.Time) TimeRangeConstraint
IsBetweenTime checks that the time is between specified minimum and maximum time values.
Example ¶
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
func (c TimeRangeConstraint) Code(code string) TimeRangeConstraint
Code overrides default code for produced violation.
func (TimeRangeConstraint) Layout ¶
func (c TimeRangeConstraint) Layout(layout string) TimeRangeConstraint
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) Name ¶
func (c TimeRangeConstraint) Name() string
Name is the constraint name.
func (TimeRangeConstraint) SetUp ¶
func (c TimeRangeConstraint) SetUp() error
SetUp returns an error if min is greater than or equal to max.
func (TimeRangeConstraint) ValidateTime ¶
func (c TimeRangeConstraint) ValidateTime(value *time.Time, scope validation.Scope) error
func (TimeRangeConstraint) When ¶
func (c TimeRangeConstraint) When(condition bool) TimeRangeConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
type URLConstraint ¶ added in v0.2.0
type URLConstraint struct {
// contains filtered or unexported fields
}
URLConstraint is used to validate URL string. This constraint doesn’t check that the host of the given URL really exists, because the information of the DNS records is not reliable.
This constraint doesn't check the length of the URL. Use LengthConstraint to check the length of the given value.
func IsURL ¶ added in v0.2.0
func IsURL() URLConstraint
IsURL creates a URLConstraint to validate an URL. By default, constraint checks only for the http:// and https:// schemas. Use the WithSchemas method to configure the list of expected schemas. Also, you can use WithRelativeSchema to enable support of the relative schema (without schema, e.g. "//example.com").
Example (InvalidURL) ¶
package main import ( "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) Name ¶ added in v0.2.0
func (c URLConstraint) Name() string
Name is the constraint name.
func (URLConstraint) SetUp ¶ added in v0.2.0
func (c URLConstraint) SetUp() error
SetUp will return an error if the list of schemas is empty.
func (URLConstraint) ValidateString ¶ added in v0.2.0
func (c URLConstraint) ValidateString(value *string, scope validation.Scope) error
func (URLConstraint) When ¶ added in v0.2.0
func (c URLConstraint) When(condition bool) URLConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.
func (URLConstraint) WithRelativeSchema ¶ added in v0.2.0
func (c URLConstraint) WithRelativeSchema() URLConstraint
WithRelativeSchema enables support of relative URL schema, which means that URL value may be treated as relative (without schema, e.g. "//example.com").
Example ¶
package main import ( "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 by the SetUp method.
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 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() UniqueConstraint
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() { v := []string{"foo", "bar", "baz", "foo"} err := validator.Validate( context.Background(), validation.Strings(v, it.HasUniqueValues()), ) fmt.Println(err) }
Output: violation: This collection should contain only unique elements.
func (UniqueConstraint) Code ¶ added in v0.4.0
func (c UniqueConstraint) Code(code string) UniqueConstraint
Code overrides default code for produced violation.
func (UniqueConstraint) Message ¶ added in v0.4.0
func (c UniqueConstraint) Message(template string, parameters ...validation.TemplateParameter) UniqueConstraint
Message sets the violation message template. You can set custom template parameters for injecting its values into the final message.
func (UniqueConstraint) Name ¶ added in v0.4.0
func (c UniqueConstraint) Name() string
Name is the constraint name.
func (UniqueConstraint) SetUp ¶ added in v0.4.0
func (c UniqueConstraint) SetUp() error
SetUp always returns no error.
func (UniqueConstraint) ValidateStrings ¶ added in v0.4.0
func (c UniqueConstraint) ValidateStrings(values []string, scope validation.Scope) error
func (UniqueConstraint) When ¶ added in v0.4.0
func (c UniqueConstraint) When(condition bool) UniqueConstraint
When enables conditional validation of this constraint. If the expression evaluates to false, then the constraint will be ignored.