如何使用流畅的验证来验证整数列表?
我的模型有:
public List<int> WindowGlassItems { get; set; }
Run Code Online (Sandbox Code Playgroud)
模型验证器有
RuleFor(x => x.WindowGlassItems).SetCollectionValidator(new WindowGlassItemsValidator());
public class WindowGlassItemsValidator : AbstractValidator<int>
{
public WindowGlassItemsValidator()
{
RuleFor(x=>x).NotNull().NotEqual(0).WithMessage("GLASS REQ");
}
}
Run Code Online (Sandbox Code Playgroud)
我越来越:
无法为表达式 x => x 自动确定属性名称。请通过调用“WithName”指定自定义属性名称。
我有一个电子邮件地址字段,有一个客户端验证,不允许少数临时域,如 tempmailder.com 或 dispostable.com。
在服务器端,我使用了流畅的验证来指定验证规则。
web.config 文件中不允许使用逗号分隔字符串的临时域。
有没有一种方法可以指定根据不允许的电子邮件域列表验证用户输入的值。
RuleFor(x=>x.EMail). and something
Run Code Online (Sandbox Code Playgroud)
谢谢
例如:
\n\nRule(x=x.Password)\n .Matches(@"(**?:[^\xe2\x80\x8b\xcb\x9c!@#$]*[\xcb\x9c\xe2\x80\x8b!@#$]**){" + count+ "}")\n .WithMessage("Password should contain at least {0} special character(s)", count);\nRun Code Online (Sandbox Code Playgroud)\n\n上面的内容不匹配所有特殊字符,如点、加号等。
\n有这样一个类:
public class MyRange
{
public int From {get; set;}
public int To {get; set;}
public MyRange(int from, int to)
{
From = from;
To = to;
}
}
Run Code Online (Sandbox Code Playgroud)
并具有List<MyRange>以下值:
var list = new List<MyRange>();
list.Add(new MyRange(1, 1000));
list.Add(new MyRange(1001, 2000));
list.Add(new MyRange(50, 1500));
list.Add(new MyRange(1900, 2900));
Run Code Online (Sandbox Code Playgroud)
是否可以有一个FluentValidation规则来检查上述范围不重叠?(范围之间的差距是有效的)
我有一个流畅的验证器,如下所示
[Validator(typeof(testvalidator))]
public class test
{
.....
}
public class testvalidator: AbstractValidator<test>
{
public testvalidator()
{
Custom(testvalidationmethod);
}
public ValidationFailure testvalidationmethod(test t)
{
return ValidationFailure("","error occurred in test validation method ");
}
}
Run Code Online (Sandbox Code Playgroud)
用法
testvalidator tv = new testvalidator();
var result =tv.(test);
Run Code Online (Sandbox Code Playgroud)
我可以在result.Errors. 但我想在视图上显示错误消息。我试着Html.ValidationSummary()用true和false。但是没有用。需要做什么才能在视图上显示?我正在阅读教程。但它显示了如何在 上显示console,这在实际应用中几乎不需要。我如何使它工作?
我正在尝试掌握 TDD,我已经查看了一些教程,并尝试对我使用 fluent 验证创建的验证类实施测试。
public SomeFormValidator()
{
RuleFor(x => x.MyClass).NotNull()
.WithMessage("MyClass cannot be null");
}
Run Code Online (Sandbox Code Playgroud)
我查看了专门用于流畅验证的 TDD 示例并创建了几个测试
[Test]
public void Should_have_error_when_MyClass_is_null()
{
MyClass myClass = null;
SomeFormValidator.ShouldHaveValidationErrorFor(aup => aup.MyClass, myClass);
}
[Test]
public void Should_not_have_error_when_MyClass_is_not_null()
{
MyClass myClass = new MyClass();
SomeFormValidator.ShouldNotHaveValidationErrorFor(aup => aup.MyClass, myClass);
}
Run Code Online (Sandbox Code Playgroud)
我现在想测试字符串“MyClass 不能为空”在它为空时返回。我无法找到任何涵盖返回消息的内容,也无法解决。
我正在尝试验证 webApi 2 中的输入对象列表。
但它没有验证。Modelstate 始终设置为 true。
示例代码:
public class A
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ClassAValidator : AbstractValidator<A>
{
public classAValidator()
{
RuleSet("ClassA",()=>{
RuleFor(x => x.Id).NotEmpty().WithMessage("The Idcan't be Empty or Zero");
RuleFor(x => x.Name).NotEmpty().Length(10).WithMessage("Name Should be Six Char length");
});
}
}
Run Code Online (Sandbox Code Playgroud)
注意:字符串字段必须是 6 个字符。
应用程序编程接口:
[HttpPut]
public async Task<IHttpActionResult> Put([FromBody] List<A> alist)
{
if(!ModelState.IsValid) throw new InvalidDataException(ModelState,"Data Validation Failed for Upload Class A");
// Model …Run Code Online (Sandbox Code Playgroud) 如何使用 FluentValidation 验证上传的文件?
<input type="file" asp-for="Files" multiple />
Run Code Online (Sandbox Code Playgroud) model-view-controller file-upload fluentvalidation asp.net-core
我有一个抽象验证器,具有以下结构:
public abstract class RiskAssessmentServiceCreateRequestValidator<T>
: AbstractValidator<T> where T
: IRiskAssessmentServiceCreateRequest
{
public RiskAssessmentServiceCreateRequestValidator(ApplicationContext context)
{
RuleSet("modelBinding", () =>
{
RuleFor(x => x.ServiceProviderId).NotNull().GreaterThan(0);
});
RuleSet("handler", () =>
{
//....
});
}
}
Run Code Online (Sandbox Code Playgroud)
在我的请求处理程序中,我像这样调用此类的派生实例:
var validationResult = _validator.Validate(request, ruleSet: "handler");
Run Code Online (Sandbox Code Playgroud)
如何在单元测试中模拟对 Validate 的特定调用?如果我不使用规则集,我的设置将如下所示:
_validator.Setup(x => x.Validate(It.IsAny<CreateRequest>()))
.Returns(validationResult);
Run Code Online (Sandbox Code Playgroud)
不允许以下调用,因为表达式树中不允许使用可选参数:
_validator.Setup(x => x.Validate(
It.IsAny<CreateRequest>(),
ruleSet: It.IsAny<string>()))
.Returns(validationResult);
Run Code Online (Sandbox Code Playgroud)
理论上我可以这样设置:
_validator.Setup(x => x.Validate(
It.IsAny<CreateRequest>(),
(IValidatorSelector)null,
It.IsAny<string>()))
.Returns(validationResult);
Run Code Online (Sandbox Code Playgroud)
但这会导致:
System.NotSupportedException : Unsupported expression: x => x.Validate<CreateRequest>(It.IsAny<CreateRequest>(), null, It.IsAny<string>())
Extension methods (here: DefaultValidatorExtensions.Validate) may not be used …Run Code Online (Sandbox Code Playgroud) 我正在努力弄清楚如何定义允许集合属性为空但不为空的规则。为集合属性提供 null 是一种有效用例,但是当提供集合时,集合需要至少有一个条目。因此:
// Valid
{
"codes": null
}
// Invalid
{
"codes": []
}
// Valid
{
"codes": ["Pass"]
}
Run Code Online (Sandbox Code Playgroud)
我一直在玩,似乎找不到任何有用的东西:
public class UpdateCodesRequest
{
public IEnumerable<string> Codes { get; set; }
}
public class UpdateCodesRequestValidator : AbstractValidator<UpdateCodesRequest>
{
public UpdateCodesRequestValidator()
{
// none of these will work if Codes is null
RuleFor(x => x.Codes.Count()).GreaterThan(0).When(x => x != null);
RuleFor(x => x.Codes).Must(x => x.Any()).When(x => x != null);
RuleFor(x => x.Codes).Must(x => x != null && x.Any()).When(x => x …Run Code Online (Sandbox Code Playgroud) fluentvalidation ×10
c# ×6
.net ×2
asp.net ×1
asp.net-core ×1
asp.net-mvc ×1
c#-5.0 ×1
file-upload ×1
moq ×1
tdd ×1
validation ×1