I'm working on Asp.net core and applying back-end validation using Fluent Validation.
The problem is, When I pass unknown property from postman over post request so it gives true response. I want to validate that unknown property.
Fluent Validation Code:
public class ScheduleADemoValidator : AbstractValidator<ScheduleADemo>
{
public ScheduleADemoValidator()
{
RuleFor(x => x.FirstName).NotEmpty().WithMessage("FirstName is required").MaximumLength(20);
RuleFor(x => x.LastName).NotEmpty().WithMessage("LastName is required").MaximumLength(20);
RuleFor(x => x.Iam).NotNull().Must(x => x == false || x == true);
RuleFor(x => x.Iaminterestedin).NotEmpty().NotEqual("-None-").WithMessage("Iaminterestedin is required");
RuleFor(x => x.Email).NotEmpty().WithMessage("Email is required").EmailAddress();
RuleFor(x => x.Phone).NotEmpty().WithMessage("Phone is required").Matches("^([0-9]{3})?[-]([0-9]{3})?[-]([0-9]{4})$");
RuleFor(x => x.NABP).NotNull().Matches("^([0-9]{7})$");
RuleFor(x => x.Pharmacyname).NotEmpty().WithMessage("Pharmacyname is required").MaximumLength(20);
RuleFor(x => x.Legalbusinessname).Length(0, 20);
RuleFor(x => x.Address).MaximumLength(50);
RuleFor(x => x.City).MaximumLength(50);
RuleFor(x => x.State).NotEmpty().NotEqual("-None-").WithMessage("State is required").Length(2);
RuleFor(x => x.ZipCode).Matches("^([0-9]{5})$");
RuleFor(x => x.IheardofPrimeRxfrom).NotEmpty().NotEqual("-None-").WithMessage("IheardofPrimeRxfrom is required").MaximumLength(30);
RuleFor(x => x.Iamlookingtobuywithin).NotEqual("-None-").WithMessage("-None- is not accepted").MaximumLength(30);
RuleFor(x => x.Iwouldliketoseeademofor).NotEmpty().NotEqual("-None-").WithMessage("Iwouldliketoseeademofor is required").MaximumLength(30);
RuleFor(x => x.Message).NotNull().MaximumLength(50);
}
protected override bool PreValidate(ValidationContext<ScheduleADemo> context, ValidationResult result)
{
if (context.InstanceToValidate == null)
{
result.Errors.Add(new ValidationFailure("", "Iam must be true or false"));
return false;
}
return true;
}
}
My controller code:
if (!validationResult.IsValid)
{
return Json(new { failure = validationResult.Errors.First().ErrorMessage });
}
var _savingScheduleADemo = new SavingScheduleADemo(_contentManager, _contentItemDisplayManager, _updateModelAccessor, _contentDefinitionManager, _session, _idGenerator);
var result = await _savingScheduleADemo.CreateScheduleADemo(
scheduleADemo.FirstName,
scheduleADemo.LastName,
scheduleADemo.Iam,
scheduleADemo.Iaminterestedin,
scheduleADemo.Email,
scheduleADemo.Phone,
scheduleADemo.NABP,
scheduleADemo.Pharmacyname,
scheduleADemo.Legalbusinessname,
scheduleADemo.Address,
scheduleADemo.City,
scheduleADemo.State,
scheduleADemo.ZipCode,
scheduleADemo.IheardofPrimeRxfrom,
scheduleADemo.Iamlookingtobuywithin,
scheduleADemo.Iwouldliketoseeademofor,
scheduleADemo.Message);
if (result)
{
return Json(new { success = true });
}
else
{
return Json(new { success = false });
}
}
Response on Postman over unknown field
Please give me your kind advice.