typestack / class-validator

Decorator-based property validation for classes.
MIT License
10.87k stars 785 forks source link

Proposal: Memoization #254

Open aigoncharov opened 6 years ago

aigoncharov commented 6 years ago

Proposal: Add a flag to class-validator's config which enables memoization. When memoization is enabled, class validator keeps the result of last validation for each field and doesn't really run validation returning the result for the field right away. As to validating nested arrays, it keeps the results of last validation for each element.

Use case: Using class-validator on a client side when validating form that have lots of elements. If form has 100 inputs, then validation upon entering a new symbol into any one of them could take lots of resources and be very frustrating. Memoization should significantly ease that pain. Using class-validator on a client side is justified when we want to use the same DTO and validation for both: server and client.

Would you be willing to accept a PR with this functionality?

NoNameProvided commented 5 years ago

Hi @keenondrums!

Yes, I like it, but let's discuss the details first.

Now I am just quickly running through the issues to catch up, but if I have not replied to this thread in a week please ping me.

aigoncharov commented 5 years ago

@NoNameProvided ping. What are your concerns about the proposal? My only one is that we should keep in mind to memoize validation of nested structures at parent level (to support validating arrays) and pass memoization-cache to each child. So memoization functionality should support optional argument: memoizationCacheObj. Also I think we should make memoization optional.