question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MustNot and MustNotAsync API

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dalefrancis88commented, Jan 16, 2022

-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.


public static class Functions
{
    public static Func<T, bool> Not<T>(Func<T, bool> predicate)
        => data => !predicate(data);
    public static Func<T, Task<bool>> Not<T>(Func<T, Task<bool>> predicate)
        => async data => !(await predicate(data));
}

public Validator(ICameraRepository cameraRepository)
{
    RuleFor(e => e.SerialNumber)
        .Cascade(CascadeMode.Stop)
        .NotEmpty()
        .MustAsync(Not(cameraRepository.ContainsSerialNumberAsync));
}

if if you want you can create your own extensions as seen belo

public static class ExtraValidatorExtensions
{
    public static IRuleBuilderOptions<T, TProperty> MustNot<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, Func<TProperty, bool> predicate) =>
        ruleBuilder.Must(val => !predicate(val));
    public static IRuleBuilderOptions<T, TProperty> MustNotAsync<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, Func<TProperty, CancellationToken, Task<bool>> predicate) => 
        ruleBuilder.MustAsync(async (val, cancel) => !(await predicate(val, cancel)));
}
1reaction
JeremySkinnercommented, Nov 18, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found