Validation rule that allows you to check combinations of rules created with the OR logical operations.
Description
The OrRule class allows you to validate combinations of rules created with the OR logical operations.
Important points
- When one of the combined rules returns no errors, then this rule also returns no errors.
- When all combined rules return errors, then this rule returns all the errors.
Constructors
NewOrRule
Creates a new validation rule and sets its values.
NewOrRule(rules …IValidationRule) *OrRule
- rules: …IValidationRule - list of rules to join with OR operator
Methods
validate
Validates a given value against this rule.
(c *OrRule) Validate(path string, schema ISchema, value interface{}) []*ValidationResult
- path: string - dot notation path to the value.
- schema: ISchema - schema this rule is called from
- value: interface{} - value to be validated.
- results: []*ValidationResult - list with validation results to add new results.
Examples
var schema = NewSchema()
.WithRule(NewOrRule(
NewValueComparisonRule("LT", 1),
NewValueComparisonRule("GT", 10),
))
schema.Validate() // Result: no error
schema.Validate(5) // Result: 5 must be less than 1 or 5 must be more than 10
schema.Validate(20) // Result: no error