OnlyOneExistsRule

Validation rule that checks that only one property of a list of properties exists in an object.

Inherits: IValidationRule

Description

The OnlyOneExistsRule allows you to check that only one property of a list of properties exists in an object.

Constructors

Creates a new validation rule and sets its values

public OnlyOneExistsRule(params string[] properties)

  • properties: string[] - list of property names where only one property must exist

Instance methods

Validate

Validates a given value against this rule.

public void Validate(string path, Schema schema, object value, List<ValidationResult> results)

  • path: string - path to the value in dot notation.
  • schema: Schema - schema this rule is called from
  • value: object - value to be validated.
  • results: ValidationResult[] - list with validation results to add new results.

Examples

var schema = new Schema().WithRule(new OnlyOneExistsRule("field1", "field2"));

schema.Validate({ field1: 1, field2: "A" });     // Result: only one of properties field1, field2 must exist
schema.Validate({ field1: 1 });                  // Result: no errors
schema.Validate({ });                            // Result: only one of properties field1, field2 must exist

See also