0

Using FluentValidation I have a rule that looks like this:

RuleFor(x => x.CodeDescription).Matches(ValidatorUtility.Contains_Html_Regex)
    .WithMessage(EpisodeCodeDescription_Contains_HTML.ErrorMessage());

My ValidatorUtility.Contains_Html_Regex = <[a-z][\s\S]*>

This only fails if a description DOES NOT contain HTML (which is everything).

I want to do the opposite. I want to fail validation if the description contains simple html <html></b><asdf/> etc

Is there a way to create a regex statement (or some other fluentValidation method) that will fail if a field CONTAINS html?

crthompson
  • 15,653
  • 6
  • 58
  • 80

1 Answers1

1

You have a few options.

  1. Use When or Unless.
  2. Change your regex to match a non match.
  3. Pass in a lambda.

    RuleFor(x => x.CodeDescription) .Must(x=> !Regex.IsMatch(x, ValidatorUtility.Contains_Html_Regex));

Isma
  • 14,604
  • 5
  • 37
  • 51
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41