简体   繁体   English

FluentValidation 验证数字最大长度

[英]FluentValidation Validate numbers max length

I Have to validate the max length of an int or long with FluentValidation我必须使用 FluentValidation 验证 int 或 long 的最大长度

I want to use something like this我想用这样的东西

int PortCode = 4444;
RuleFor(x => x.PortCode).NotEmpty().MaximumLength(10);

but FluentValidation does not support it但 FluentValidation 不支持

what is the solution;解决办法是什么;

please use this custom validation请使用此自定义验证

using FluentValidation;使用 FluentValidation;

public static class ValidationHelper
{
    public static IRuleBuilderOptions<T, short> MaximumLength<T>(this IRuleBuilder<T, short> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    public static IRuleBuilderOptions<T, short?> MaximumLength<T>(this IRuleBuilder<T, short?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    public static IRuleBuilderOptions<T, int> MaximumLength<T>(this IRuleBuilder<T, int> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    public static IRuleBuilderOptions<T, int?> MaximumLength<T>(this IRuleBuilder<T, int?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    public static IRuleBuilderOptions<T, long> MaximumLength<T>(this IRuleBuilder<T, long> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    public static IRuleBuilderOptions<T, long?> MaximumLength<T>(this IRuleBuilder<T, long?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    public static IRuleBuilderOptions<T, decimal> MaximumLength<T>(this IRuleBuilder<T, decimal> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    public static IRuleBuilderOptions<T, decimal?> MaximumLength<T>(this IRuleBuilder<T, decimal?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    public static IRuleBuilderOptions<T, double> MaximumLength<T>(this IRuleBuilder<T, double> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs(n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    public static IRuleBuilderOptions<T, double?> MaximumLength<T>(this IRuleBuilder<T, double?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs(n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
}

you don't need custom validation您不需要自定义验证

you can do this instead你可以这样做

RuleFor(a => a.PortCode).NotEmpty().Must(w => w.ToString().Length < 10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM