bartoszlenar / Validot

Validot is a performance-first, compact library for advanced model validation. Using a simple declarative fluent interface, it efficiently handles classes, structs, nested members, collections, nullables, plus any relation or combination of them. It also supports translations, custom logic extensions with tests, and DI containers.
MIT License
298 stars 18 forks source link

async/await support #2

Closed bartoszlenar closed 2 years ago

bartoszlenar commented 4 years ago

Feature description

Feature in action

var validator = Validator.Factory.Create(specification, settings => settings
    .WithTaskScheduler(taskScheduler)
)
Specification<object[]> specification = s => s
    .AsCollection(itemSpecification)
    .WithAsyncStrategy(); // will use some default strategy
 // will use the async version only if more than 10k items, validating 100 items in a single task:

Specification<object[]> specification = s => s
    .AsCollection(itemSpecification)
    .WithAsyncStrategy(bulkSize: 100, minCount: 10000);
var result1 = validator.Validate(hugeCollection); // under the hood it triggers async collection validation anyway

var result2 = await validator.Validate(hugeCollection); // same as above, but bubbles up the task?

Feature details

bartoszlenar commented 4 years ago

After a lot of offline discussions, I decided to postpone it a little bit. In the vast majority of cases, async/await support is not needed. There are only two justifications for that:

  1. The predicates are awaitable

And Validot doesn't support this approach either now and in the foreseeable future. The validation logic should be quick and straightforward (or at least static) without calling external services or querying remote databases.

In this case, Validot follows a different path than FluentValidation.

  1. Large collections.

This is a valid case, described in this issue's description. However, it's a low priority matter.

bartoszlenar commented 2 years ago

I'm closing this down. Validot will not be supporting awaitable predicates as rules.

Large collections are a separate issue and I've heard user voices encouraging me to address it... but I don't think that anything similar to WithAsyncStrategy is the solution.

Most probably it will be handled with a separate, dedicated validator. Imagine something more like:


var collectionValidator = ValidatorFactory.CreateForCollections(specification);

var results = await collectionsValidator.Validate(largeCollection);