using System.ComponentModel.DataAnnotations;
namespace Masuit.Tools.Core.Validator;
///
/// 验证企业的统一社会信用代码是否合法
///
public class UnifiedSocialCreditCodeAttribute : ValidationAttribute
{
///
/// 是否允许为空
///
public bool AllowEmpty { get; set; }
private readonly string _customMessage;
///
///
///
/// 自定义错误消息
public UnifiedSocialCreditCodeAttribute(string customMessage = null)
{
_customMessage = customMessage;
}
///
/// 验证手机号码是否合法
///
///
///
public override bool IsValid(object value)
{
if (AllowEmpty)
{
switch (value)
{
case null:
case string s when string.IsNullOrEmpty(s):
return true;
}
}
if (value is null)
{
ErrorMessage = _customMessage ?? "企业统一社会信用代码不能为空";
return false;
}
string phone = value as string;
if (phone.MatchUSCC())
{
return true;
}
ErrorMessage = _customMessage ?? "企业统一社会信用代码格式不正确,请输入有效的企业统一社会信用代码!";
return false;
}
}