Skip to main content

Array Properties

Validating array properties is made easy with the .ruleForEach method.

The .ruleForEach method works almost exactly the same as the .ruleFor method, so it's worth reading up on that first if you haven't already.

The Gist

You can validate an array property using the .ruleFor method:

this.ruleFor('scores').must(
(scores) => scores.filter((score) => score < 0 || score > 100).length === 0
);

Alternatively, you can use the .ruleForEach method:

this.ruleForEach('scores').greaterThanOrEqualTo(0).lessThanOrEqualTo(100);