Validation rule that checks that only one property of a list of properties exists in an object.
Description
The OnlyOneExistsRule allows you to check that only one property of a list of properties exists in an object.
Constructors
NewOnlyOneExistsRule
Creates a new validation rule and sets its values
NewOnlyOneExistsRule(properties …string) *OnlyOneExistsRule
- properties: …string - list of property names where only one property must exist
Methods
Validate
Validates a given value against this rule.
(c *OnlyOneExistsRule) Validate(path string, schema ISchema, value any) []*ValidationResult
- path: string - dot notation path to the value.
- schema: ISchema - schema this rule is called from
- value: any - value to be validated.
- results: ValidationResult[] - list with validation results to add new results.
Examples
var schema = NewSchema().WithRule(NewOnlyOneExistsRule("field1", "field2"))
schema.Validate(struct {
field1 int
field2 string
}{field1: 1, field2: "A"}) // Result: only one of properties field1, field2 must exist
schema.Validate(struct{ field1 int }{field1: 1}) // Result: no errors
schema.Validate(struct{}{})