MustNot and MustNotAsync API
See original GitHub issueIs your feature request related to a problem? Please describe.
When I want to apply MustNotAsync
, I have to do something like this and I think we can make it neater
public Validator(ICameraRepository cameraRepository)
{
RuleFor(e => e.SerialNumber)
.Cascade(CascadeMode.Stop)
.NotEmpty()
.MustAsync(async (serialNumber, cancellationToken) => !(await cameraRepository.ContainsSerialNumberAsync(serialNumber, cancellationToken)))
}
Describe the solution you’d like
I suggest to add MustNot
and MustNotAsync
as new apis. It will be used like this
public Validator(ICameraRepository cameraRepository)
{
RuleFor(e => e.SerialNumber)
.Cascade(CascadeMode.Stop)
.NotEmpty()
.MustNotAsync(cameraRepository.ContainsSerialNumberAsync);
}
Describe alternatives you’ve considered
No response
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Javascript not waiting for ajax with bootstrap-wizard.js
When we using promise the code can be asynchronous. You must not async: false - this is not very good idea at all....
Read more >Boolean query | Elasticsearch Guide [8.9]
The bool query takes a more-matches-is-better approach, so the score from each matching must or should clause will be added together to provide...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
-1 for this feature. While it may seem simple, this is not something that should be the responsibility of the maintainer. This is the equivalent of asking to add
ifNot
to the base language. The maintainers work hard enough in their spare times to build out the base level features and adding trivial pieces such as this has a net negative on the cost/reward scale, it increases the burden for next to no value.For those that find this you can implement this quite simply in two ways either wrap your predicate function in a
Not
function you import from a static class or implement your own extension. Both can be seen below.if if you want you can create your own extensions as seen belo
Hi, thanks for the suggestion!
I’m not currently planning on adding any new validators to the core library, but this is something that I’ll consider for a future version sometime after 11.0 has shipped. However if you want to use these as methods in your own project you could implement them as extension methods that wrap the existing Must/MustAsync