In the world of web development, validation plays a crucial role in ensuring data integrity and security. ASP.NET Core offers a robust set of validation capabilities out-of-the-box, but sometimes you need more. Sometimes, you need custom validators. In this blog post, we’ll delve deep into the realm of custom validators in ASP.NET Core.

Why Custom Validators?

While the built-in validators like [Required], [StringLength], and [Range] are immensely useful, there are scenarios where domain-specific rules or complex logic is necessary. That’s where custom validators come into play.

Setting the Stage

Before we jump into creating custom validators, ensure you have:

  • An ASP.NET Core project up and running.
  • A basic understanding of data annotations.

Creating a Basic Custom Validator

Let’s start by creating a custom validation attribute to check if a string is a palindrome.

using System.ComponentModel.DataAnnotations;

public class PalindromeAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null) return new ValidationResult("Value is required.");

        var stringValue = value.ToString();
        if (stringValue != Reverse(stringValue))
        {
            return new ValidationResult("String is not a palindrome.");
        }

        return ValidationResult.Success;
    }

    private string Reverse(string s) => new string(s.Reverse().ToArray());
}

To use it, just annotate your model property:

public class SampleModel
{
    [Palindrome]
    public string PalindromeString { get; set; }
}

Custom Validator with Parameters

Consider a scenario where you want to check if a given number is a prime number and below a certain threshold.

public class PrimeNumberAttribute : ValidationAttribute
{
    public int MaxValue { get; set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null) return new ValidationResult("Value is required.");

        if (!IsPrime((int)value) || (int)value > MaxValue)
        {
            return new ValidationResult($"Value should be a prime number and less than {MaxValue}.");
        }

        return ValidationResult.Success;
    }

    private bool IsPrime(int number)
    {
        // Logic to determine if number is prime
    }
}

Use it as:

public class SampleModel
{
    [PrimeNumber(MaxValue = 50)]
    public int PrimeValue { get; set; }
}

Client-side Validation with Custom Validators

While server-side validation is essential, client-side validation enhances user experience. With ASP.NET Core, you can pair your custom validators with client-side validation by implementing IClientModelValidator.

Error Messages and Localization

For global audiences, you might want to return error messages in various languages. ASP.NET Core makes this easier with localization support.

public class LocalizedPalindromeAttribute : PalindromeAttribute
{
    public override string FormatErrorMessage(string name)
    {
        return LocalizationService.GetLocalizedError(name); // This is just a pseudocode. Replace with actual localization logic.
    }
}

Validating Complex Types

Sometimes you might need to validate a complex object rather than a single property. In such cases, override the IsValid method of ValidationAttribute and apply it to the class instead of the property.

Integrating with Dependency Injection

For validators that need to access services (e.g., database), ASP.NET Core’s dependency injection can be integrated into the custom validator pattern.

Best Practices

  • Do Not Rely Solely on Client-Side Validation: Always validate on the server-side as well.
  • Provide Clear Error Messages: Ensure your user understands what went wrong.
  • DRY Principle: If you see repetitive validation logic, it’s time for a custom validator.

Conclusion

Custom validators in ASP.NET Core empower developers to handle a wide array of validation scenarios. While the built-in validators handle many common use cases, knowing how to craft your validators offers flexibility and ensures your application’s data remains accurate and safe.

Whether you’re checking for business-specific rules, combining multiple validation checks, or integrating with other services, custom validators can be tailored to fit perfectly within your application’s architecture.

Advertisements

Leave a comment

Recent posts

Advertisements

Quote of the week

“People ask me what I do in the winter when there’s no baseball. I’ll tell you what I do. I stare out the window and wait for spring.”

~ Rogers Hornsby

Designed with WordPress