diff --git a/Falcon.SugarApi/ModelValidation/IsIntAttribute.cs b/Falcon.SugarApi/ModelValidation/IsIntAttribute.cs new file mode 100644 index 0000000..baf35fc --- /dev/null +++ b/Falcon.SugarApi/ModelValidation/IsIntAttribute.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Falcon.SugarApi.ModelValidation +{ + /// + /// 必须可转换为整数 + /// + public class IsIntAttribute : ValidationAttribute + { + /// + public override bool IsValid(object? value) { + if (value == null) { + return false; + } + return int.TryParse(value.ToString(), out int _); + } + + /// + protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { + if (value == null) { + return new ValidationResult(ErrorMessage = "必须为整数"); + } + return this.IsValid(value) ? null + : new ValidationResult(ErrorMessage = "必须为整数", new List() { validationContext.MemberName }); + } + } +}