I'm trying to create some validation rules using static extension methods for IRuleBuilderOptions
so I don't have to keep repeating myself when creating validators for Creating and Updating my objects.
For some reason I keep getting CS1061 errors for the country.Id
in the MustAsync
unique checks. I have tried replacing arrow functions with brackets and return
, tried async
/await
. They all result in the same error. Intellisense shows that country
is of type Country
, and Id
is a public property with both public getter and setter, so I'm not sure what I'm missing? I tried compiling in case it was just an Intellisense issue, but compilation fails with the same error.
Note: Using VS2022 / .Net6
public class Country
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Code2 { get; set; } = string.Empty;
public string Code3 { get; set; } = string.Empty;
public int DisplayOrder { get; set; } = 1000;
public bool Enabled { get; set; } = true;
}
public class CountryCreateValidator : AbstractValidator<Country>
{
public CountryCreateValidator(IApplicationDbContext dbContext)
{
RuleFor(x => x.Name).Name(dbContext);
RuleFor(x => x.Code2).Code2(dbContext);
RuleFor(x => x.Code3).Code3(dbContext);
}
}
public class CountryUpdateValidator : AbstractValidator<Country>
{
public CountryUpdateValidator(IApplicationDbContext dbContext)
{
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).Name(dbContext);
RuleFor(x => x.Code2).Code2(dbContext);
RuleFor(x => x.Code3).Code3(dbContext);
}
}
public static class CountryRules
{
public static IRuleBuilderOptions<Country, string> Name<Country>(this IRuleBuilder<Country, string> ruleBuilder, IApplicationDbContext dbContext) =>
ruleBuilder.NotNull().NotEmpty().MaximumLength(64)
.MustAsync((country, name, cancellationToken) =>
{
return dbContext.Countries.AllAsync(x => x.Id == country.Id /* error here */ || x.Name != name, cancellationToken);
}).WithMessage(FvConstants.UNIQUE);
public static IRuleBuilderOptions<Country, string> Code2<Country>(this IRuleBuilder<Country, string> ruleBuilder, IApplicationDbContext dbContext) =>
ruleBuilder.NotEmpty().Length(2).Matches("^[A-Z]{2}$").WithMessage("{PropertyName} must be two uppercase letters")
.MustAsync((country, code2, cancellationToken) =>
{
return dbContext.Countries.AllAsync(x => x.Id == country.Id || x.Code2 != code2, cancellationToken);
}).WithMessage(FvConstants.UNIQUE);
public static IRuleBuilderOptions<Country, string> Code3<Country>(this IRuleBuilder<Country, string> ruleBuilder, IApplicationDbContext dbContext) =>
ruleBuilder.NotEmpty().Length(3).Matches("^[A-Z]{3}$").WithMessage("{PropertyName} must be three uppercase letters")
.MustAsync((country, code3, cancellationToken) =>
{
return dbContext.Countries.AllAsync(x => x.Id == country.Id || x.Code3 != code3, cancellationToken);
}).WithMessage(FvConstants.UNIQUE);
}
public class FvConstants
{
public const string UNIQUE = "{PropertyName} must be unique";
}
Here is a screenshot showing proper typeing of the country
parameter and the error I am getting...
CodePudding user response:
Because Country
is the name of generic parameter of your methods, not a type name. Just remove <Country>
from the signatures:
public static IRuleBuilderOptions<Country, string> Name(...
public static IRuleBuilderOptions<Country, string> Code2(...
public static IRuleBuilderOptions<Country, string> Code3(...