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.

Validating primitive types

See original GitHub issue

I’m trying to find a way how I can use built-in validators to validate primitive types like strings or ints. For example, I would like to be able to use EmailValidator to validate a string value (not a string property of a complex type) by calling something like Validate. But EmailValidator is IPropertyValidator, not IValidator.

Any ideas how to use FluentValidation to validate primitive types?

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
JeremySkinnercommented, Mar 17, 2016

Sure, here’s an example that chains multiple rules on a string property, within an extension method.

// In the validator
RuleFor(x => x.Surname).CustomSurnameValidation();
// Custom extensions class
public static class MyExtensions {

  // Generic type is hard-coded to string, menaing this method will only appear for string properties.
  // If you wanted it to appear for *all* properties, introduce a second type-parameter called TProperty
  public static IRuleBuilderOptions<T, string> CustomSurnameValidation<T>(this IRuleBuilder<T, string> rule) {
    return rule.NotNull().NotEqual("foo");
  } 
}

This is actually how all of FV’s default validation rules (NotNull, NotEmpty etc) work- they’re all extesnions on IRuleBuilder that perform certain actions (usually calling SetValidator to encapsulate within a custom property validator)

1reaction
JeremySkinnercommented, Mar 17, 2016

Yes, you have a couple of options. You can use the approach I documented above, by creating an AbstractValidator instance that acts on strings. The other option is to write an extension that wraps the multiple calls to RuleFor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to validate primitive types with FluentValidation in ...
The question is, how to gracefully validate primitive type arguments in ASP.net core using FluentValidation? For complex types, I have a ...
Read more >
Validating primitive types · Issue #184 · FluentValidation ...
I'm trying to find a way how I can use built-in validators to validate primitive types like strings or ints. For example, I...
Read more >
Recommended and Unrecommended Primitive Types
Our Platform includes a validation mechanism that identifies Unrecommended Primitive Types, which are more likely to cause issues in the ...
Read more >
Data Validation — Overview, Types, How To Perform
Type validation often refers to checking whether or not an entry matches the field. For example, you might try entering text in the...
Read more >
Data Validation - Overview, Types, Practical Examples
Types of Data Validation · 1. Data Type Check · 2. Code Check · 3. Range Check · 4. Format Check · 5....
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