किसी विशेष प्रॉपर्टी के लिए वैलिडेशन नियम निर्दिष्ट करने के लिए, RuleFor मेथड को कॉल करें, एक लैम्ब्डा एक्सप्रेशन पास करते हुए जो उस प्रॉपर्टी को इंगित करता है जिसे आप मान्य करना चाहते हैं।
नियम परिभाषा
RuleFor(p => p.DateOfBirth)
.Must(BeAValidAge)
.WithMessage("Invalid {PropertyName}");
वैलिडेटर चलाने के लिए, वैलिडेटर ऑब्जेक्ट को इंस्टेंटिएट करें और Validate मेथड को कॉल करें, मान्य किए जाने वाले ऑब्जेक्ट को पास करते हुए।
ValidationResult
Validate मेथड एक ValidationResult ऑब्जेक्ट लौटाता है जिसमें दो प्रॉपर्टीज होती हैं:
- IsValid - एक बूलियन जो बताता है कि वैलिडेशन सफल हुआ या नहीं
- Errors - किसी भी वैलिडेशन विफलता के बारे में विवरण वाले
ValidationFailureऑब्जेक्ट्स का संग्रह
उदाहरण: DateOfBirth वैलिडेशन (भविष्य की तारीख नहीं, उचित आयु सीमा)
using FluentValidation;
using FluentValidation.Results;
static void Main(string[] args)
{
List<string> errors = new List<string>();
PersonModel person = new PersonModel
{
FirstName = "TestUser",
LastName = "TestUser",
AccountBalance = 100,
DateOfBirth = DateTime.Now.Date.AddYears(1) // भविष्य की तारीख - अमान्य!
};
PersonValidator validator = new PersonValidator();
ValidationResult results = validator.Validate(person);
if (!results.IsValid)
{
foreach (ValidationFailure failure in results.Errors)
{
errors.Add(failure.ErrorMessage);
}
}
foreach (var item in errors)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
public class PersonModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public decimal AccountBalance { get; set; }
public DateTime DateOfBirth { get; set; }
}
public class PersonValidator : AbstractValidator<PersonModel>
{
public PersonValidator()
{
RuleFor(p => p.DateOfBirth)
.Must(BeAValidAge)
.WithMessage("Invalid {PropertyName}: Date of birth cannot be in the future and age must be between 0 and 120");
}
protected bool BeAValidAge(DateTime date)
{
int currentYear = DateTime.Now.Year;
int dobYear = date.Year;
// वर्ष वर्तमान वर्ष से कम या बराबर होना चाहिए और 120 साल से अधिक पुराना नहीं
return dobYear <= currentYear && dobYear > (currentYear - 120);
}
}
आउटपुट
Invalid DateOfBirth: Date of birth cannot be in the future and age must be between 0 and 120
उन्नत DateOfBirth वैलिडेशन
public class PersonValidator : AbstractValidator<PersonModel>
{
public PersonValidator()
{
RuleFor(p => p.DateOfBirth)
.NotEmpty().WithMessage("Date of birth is required")
.LessThanOrEqualTo(DateTime.Today).WithMessage("Date of birth cannot be in the future")
.GreaterThan(DateTime.Today.AddYears(-120)).WithMessage("Age cannot exceed 120 years")
.Must(BeAValidDate).WithMessage("Invalid date format");
}
private bool BeAValidDate(DateTime date)
{
// अतिरिक्त कस्टम लॉजिक (उदाहरण: लीप वर्ष, विशिष्ट फॉर्मेट)
return date != default && date.Year > 1900;
}
}
एक्सटेंशन मेथड के रूप में पुन: प्रयोज्य वैलिडेटर
public static class DateOfBirthValidators
{
public static IRuleBuilderOptions<T, DateTime> ValidDateOfBirth<T>(
this IRuleBuilder<T, DateTime> ruleBuilder,
int minAge = 0,
int maxAge = 120)
{
return ruleBuilder
.NotEmpty()
.LessThanOrEqualTo(DateTime.Today)
.GreaterThan(DateTime.Today.AddYears(-maxAge))
.LessThan(DateTime.Today.AddYears(-minAge));
}
}
// उपयोग
RuleFor(p => p.DateOfBirth).ValidDateOfBirth(minAge: 18, maxAge: 100);
सर्वोत्तम अभ्यास
Mustका उपयोग कस्टम लॉजिक के लिए करें जो बिल्ट-इन वैलिडेटर्स द्वारा कवर नहीं किया गया हैWithMessageमें प्लेसहोल्डर्स का उपयोग करें:{PropertyName},{PropertyValue}- जटिल वैलिडेशन को पुन: प्रयोज्य एक्सटेंशन मेथड्स में निकालें
- सर्वर-साइड और क्लाइंट-साइड दोनों पर वैलिडेट करें
- वैलिडेशन लॉजिक को डोमेन मॉडल से अलग रखें