Collection Functions


Count

Ensures the collection has a specified item count.

Methods
Count<T>(int count)
Count<T, P>(Expression<Func<T, IEnumerable<P>>> selector, int count)
Broken Rule

Collection count must equal {count}.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<IEnumerable<string>>()
    .Count(2)
    .Build();

var result = validator.Validate(new[] { "One", "Two" });

Using selector expression.

var validator = new ValidatorBuilder<Employee>()
    .Count(e => e.Skills, 2)
    .Build();

var result = validator.Validate(new Employee { Skills = new[] { "Cromulent" } });

Using For function.

var validator = new ValidatorBuilder<Employee>()
    .For(e => e.Skills, v => v.Count(1))
    .Build();

var result = validator.Validate(new Employee { Skills = new[] { "Vandalism" } });

MinCount

Ensures the collection has a specified minimum item count.

Methods
MinCount<T>(int count)
MinCount<T, P>(Expression<Func<T, IEnumerable<P>>> selector, int count)
Broken Rule

Collection count must be at least {count}.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<IEnumerable<string>>()
    .MinCount(2)
    .Build();

var result = validator.Validate(new[] { "One" });

Using selector expression.

var validator = new ValidatorBuilder<Employee>()
    .MinCount(e => e.Skills, 2)
    .Build();

var result = validator.Validate(new Employee { Skills = new[] { "Cromulent" } });

Using For function.

var validator = new ValidatorBuilder<Employee>()
    .For(e => e.Skills, v => v.MinCount(1))
    .Build();

var result = validator.Validate(new Employee { Skills = new[] { "Vandalism" } });

MaxCount

Ensures the collection has a specified maximum item count.

Methods
MaxCount<T>(int count)
MaxCount<T, P>(Expression<Func<T, IEnumerable<P>>> selector, int count)
Broken Rule

Collection count cannot be greater than {count}.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<IEnumerable<string>>()
    .MaxCount(2)
    .Build();

var result = validator.Validate(new[] { "One" });

Using selector expression.

var validator = new ValidatorBuilder<Employee>()
    .MaxCount(e => e.Skills, 2)
    .Build();

var result = validator.Validate(new Employee { Skills = new[] { "Cromulent" } });

Using For function.

var validator = new ValidatorBuilder<Employee>()
    .For(e => e.Skills, v => v.MaxCount(1))
    .Build();

var result = validator.Validate(new Employee { Skills = new[] { "Vandalism" } });

Contains

Ensures the collection contains the specified item.

Methods
Contains<T>(T item)
Contains<T, P>(Expression<Func<T, IEnumerable<P>>> selector, P item)
Broken Rule

Collection must contain item '{item}'.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<IEnumerable<string>>()
    .Contains("Two")
    .Build();

var result = validator.Validate(new[] { "One", "Two", "Three" });

Using selector expression.

var validator = new ValidatorBuilder<Employee>()
    .Contains(e => e.Skills, "Cromulent")
    .Build();

var result = validator.Validate(new Employee { Skills = new[] { "Cromulent" } });

Using For function.

var validator = new ValidatorBuilder<Employee>()
    .For(e => e.Skills, v => v.Contains("Chill"))
    .Build();

var result = validator.Validate(new Employee { Skills = new[] { "Wisdom" } });