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.

ChildRules() inside ForEach() ignores WithErrorCode(), uses defaults: RegularExpressionValidator, AsyncPredicateValidator, etc.

See original GitHub issue

Which version of FluentValidation are you using?

<PackageReference Include="FluentValidation" Version="9.5.3" />

Which version of ASP.NET are you using?

<Project Sdk="Microsoft.NET.Sdk.Web">
<TargetFramework>netcoreapp3.1</TargetFramework>

Describe the issue that you’re having

I have a pretty heavy validator with the rules such as:

RuleFor(x => x)
    .Must((_, _, context) => CheckAccountState(context.ParentContext))
    .WithMessage("Account must be in Succeeded state")
    .WithErrorCode(ErrorCode.AccountIsNotInSucceededState)

What produces correct error:

{
    "code": "AccountIsNotInSucceededState",
    "message": "Account must be in Succeeded state"
}

and

 RuleFor(x => x.Properties.IotHubs)
     .ForEach(x => x.Must((_, iotHub, context) => CheckIotHubName(iotHub, context.ParentContext))
          .WithMessage((_, iotHub) => $"Specified Iot Hub '{iotHub.ResourceId}' must match resource id and connection string")
          .WithErrorCode(ErrorCode.IoTHubNameDoesNotMatchResourceIdOrConnectionString)

          // other rules go here

hence:

{
    "code": "IoTHubNameDoesNotMatchResourceIdOrConnectionString",
    "message": "Specified Iot Hub '...' must match resource id and connection string"
}

However then I have a rule such as this:

.ChildRules(s => s.RuleFor(p => p.ResourceId)
    .NotNull()
    .NotEmpty()
    .Matches(config.Value.Validation.IoTHubResourceId, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Singleline))
    .WithErrorCode(ErrorCode.IoTHubNameDoesNotMatchPattern)

What ignores the provided error code altogether:

{
    "code": "RegularExpressionValidator",
    "message": "'Resource Id' is not in the correct format."
}

And more than on one occasion:

.ChildRules(s => s.RuleFor(p => p.IoTHubConnectionString)
    .NotNull()
    .NotEmpty()
    .MustAsync((_, str, context, ct) => CheckIotHubConnectionString(str, context.ParentContext, ct))
    .WithMessage("Specified Iot Hub connection string is invalid"))
    .WithErrorCode(ErrorCode.IoTHubConnectionStringIsInvalid)

hence:

{
    "code": "AsyncPredicateValidator",
    "message": "Specified Iot Hub connection string is invalid"
}

Have a setup anything incorrectly or it’s a bug?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jeremymengcommented, Mar 24, 2021

@jeremymeng, sure, here’s a repro.

I believe you meant @JeremySkinner 😄

0reactions
abatishchevcommented, Mar 24, 2021

@JeremySkinner thank you so much for detailed explanation and example, that helps a lot, I got it working now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

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