Validot icon indicating copy to clipboard operation
Validot copied to clipboard

async/await support

Open bartoszlenar opened this issue 4 years ago • 1 comments

Feature description

  • Async version of Validate method.
  • Validation is fast enough that I don't believe we need async/await just for sake of having it.
    • Task & thread orchestration would probably take more time than the validation itself.
  • However the async version could be useful when validation tries to handle large collections.

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

  • Async strategy would be used for collections only (parameter command that affects AsCollection).
  • Task orchestration done with task scheduler set in the settings.
  • ValidationContext would need to be created for each bulk and at the end - all the contexts from all of the bulks would be merged into the master one.
    • At the same time, it would be nice that the original ValidationContext continue with the regular validation in other places.

bartoszlenar avatar Jun 11 '20 14:06 bartoszlenar

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 avatar Aug 24 '20 06:08 bartoszlenar

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);

bartoszlenar avatar Sep 17 '22 10:09 bartoszlenar