gpt4 book ai didi

.net - 如何使用 FluentValidation 将字符串验证为 DateTime

转载 作者:行者123 更新时间:2023-12-02 21:40:04 25 4
gpt4 key购买 nike

使用 FluentValidation,是否可以将 string 验证为可解析的 DateTime,而无需指定 Custom() 委托(delegate)?

理想情况下,我想说类似 EmailAddress 函数的内容,例如:

RuleFor(s => s.EmailAddress).EmailAddress().WithMessage("Invalid email address");

所以像这样:

RuleFor(s => s.DepartureDateTime).DateTime().WithMessage("Invalid date/time");

最佳答案

RuleFor(s => s.DepartureDateTime)
.Must(BeAValidDate)
.WithMessage("Invalid date/time");

和:

private bool BeAValidDate(string value)
{
DateTime date;
return DateTime.TryParse(value, out date);
}

或者你可以写一个custom extension method .

关于.net - 如何使用 FluentValidation 将字符串验证为 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2560829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com