30 lines
1.1 KiB
C#
30 lines
1.1 KiB
C#
namespace SimpleAdmin.Application;
|
|
|
|
/// <summary>
|
|
/// 统一校验入职业务中的可编辑编码,避免非法字符或超长内容落到数据库后才报 500。
|
|
/// </summary>
|
|
internal static class OnboardingCodeValidator
|
|
{
|
|
/// <summary>
|
|
/// 规范化并校验业务编码。编码必须以字母开头,可包含字母、数字、点、下划线和短横线。
|
|
/// </summary>
|
|
public static string Normalize(string code, string fieldName, int maxLength)
|
|
{
|
|
var normalized = code?.Trim();
|
|
if (string.IsNullOrWhiteSpace(normalized))
|
|
throw Oops.Bah($"{fieldName}不能为空");
|
|
|
|
if (normalized.Length > maxLength)
|
|
throw Oops.Bah($"{fieldName}长度不能超过 {maxLength} 个字符");
|
|
|
|
if (!global::System.Text.RegularExpressions.Regex.IsMatch(
|
|
normalized,
|
|
@"^[A-Za-z][A-Za-z0-9._-]*$"))
|
|
{
|
|
throw Oops.Bah($"{fieldName}必须以字母开头,只能包含字母、数字、点、下划线和短横线");
|
|
}
|
|
|
|
return normalized;
|
|
}
|
|
}
|