Sage.Data.Validation 1.0.0.2

dotnet add package Sage.Data.Validation --version 1.0.0.2
                    
NuGet\Install-Package Sage.Data.Validation -Version 1.0.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sage.Data.Validation" Version="1.0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sage.Data.Validation" Version="1.0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Sage.Data.Validation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sage.Data.Validation --version 1.0.0.2
                    
#r "nuget: Sage.Data.Validation, 1.0.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sage.Data.Validation@1.0.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sage.Data.Validation&version=1.0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Sage.Data.Validation&version=1.0.0.2
                    
Install as a Cake Tool

Sage.Data.Validation

一个用于数据验证的 .NET 库,提供了丰富的扩展方法和异常处理机制,帮助开发者轻松验证数据的合法性。

功能特点

  • 提供丰富的字符串格式验证方法(邮箱、手机号、URL、IP地址等)
  • 提供中国特定格式验证(身份证号码、邮政编码等)
  • 提供文件路径验证方法
  • 提供参数验证异常处理机制
  • 所有方法均为扩展方法,使用方便
  • 支持 AOT 编译优化

安装

dotnet add package Sage.Data.Validation

使用示例

基础验证

using Sage.Data.Validation;

// 验证是否为数字
string numericValue = "123.45";
bool isNumeric = numericValue.IsNumeric(); // 返回 true

// 验证是否为有效日期
string dateValue = "2023-01-01";
bool isValidDate = dateValue.IsValidDate(); // 返回 true

// 使用正则表达式验证
string value = "abc123";
bool matchesPattern = value.MatchesPattern(@"^[a-z]+\d+$"); // 返回 true

联系方式验证

using Sage.Data.Validation;

// 验证电子邮箱
string email = "user@example.com";
bool isValidEmail = email.IsValidEmail(); // 返回 true

// 验证中国手机号
string phoneNumber = "13812345678";
bool isValidPhone = phoneNumber.IsValidChinesePhoneNumber(); // 返回 true

网络相关验证

using Sage.Data.Validation;

// 验证 URL
string url = "https://www.example.com";
bool isValidUrl = url.IsValidUrl(); // 返回 true

// 验证 IP 地址
string ipv4 = "192.168.1.1";
bool isValidIp = ipv4.IsValidIpAddress(); // 返回 true

// 验证 IPv4 地址
bool isValidIpv4 = ipv4.IsValidIpv4Address(); // 返回 true

// 验证 IPv6 地址
string ipv6 = "2001:db8::1";
bool isValidIpv6 = ipv6.IsValidIpv6Address(); // 返回 true

// 智能验证 IP 地址(支持 IP+端口)
string ipWithPort = "192.168.1.1:8080";
bool isSmartValid = ipWithPort.IsSmartValidIpAddress(); // 返回 true

// 验证私有 IP 地址
bool isPrivate = ipv4.IsPrivateIpv4Address(); // 返回 true

// 验证回环地址
string loopback = "127.0.0.1";
bool isLoopback = loopback.IsLoopbackIpAddress(); // 返回 true

中国特定格式验证

using Sage.Data.Validation;

// 验证中国身份证号码
string idCard = "11010519491231002X";
bool isValidIdCard = idCard.IsValidChineseIdCard(); // 返回 true(如果校验码正确)

// 验证中国邮政编码
string postalCode = "100000";
bool isValidPostalCode = postalCode.IsValidChinesePostalCode(); // 返回 true

安全相关验证

using Sage.Data.Validation;

// 验证密码强度
string password = "MyP@ssw0rd";
bool isStrong = password.IsStrongPassword(
    minLength: 8,
    requireUppercase: true,
    requireLowercase: true,
    requireDigit: true,
    requireSpecialChar: true
); // 返回 true

文件路径验证

using Sage.Data.Validation;

// 验证是否为绝对路径
string absolutePath = @"C:\Users\Username\Documents\file.txt";
bool isAbsolute = absolutePath.IsAbsolutePath(); // 返回 true

// 验证路径是否包含无效字符
string invalidPath = "file<name>.txt";
bool hasInvalidChars = invalidPath.HasInvalidPathChars(); // 返回 true

// 验证文件名是否包含无效字符
string fileName = "document.pdf";
bool hasInvalidFileNameChars = fileName.HasInvalidFileNameChars(); // 返回 false

参数验证异常处理

基础验证
using Sage.Data.Validation;

// 验证参数不为 null(引用类型)
public void ProcessUser(User user)
{
    ArgumentCustomException.ThrowIfNull(user, nameof(user));
    // 处理用户...
}

// 验证参数不为 null(可空值类型)
public void ProcessDate(DateTime? date)
{
    DateTime actualDate = ArgumentCustomException.ThrowIfNull(date, nameof(date), "日期不能为空");
    // 处理日期...
}

// 验证参数不为默认值
public void ProcessId(Guid id)
{
    ArgumentCustomException.ThrowIfDefault(id, nameof(id), "ID不能为默认值");
    // 处理ID...
}
字符串验证
using Sage.Data.Validation;

// 验证字符串不为 null 或空
public void ProcessName(string name)
{
    ArgumentCustomException.ThrowIfNullOrEmpty(name, nameof(name), "名称不能为空");
    // 处理名称...
}

// 验证字符串不为 null、空或只包含空白字符
public void ProcessDescription(string description)
{
    ArgumentCustomException.ThrowIfNullOrWhiteSpace(description, nameof(description), "描述不能为空白");
    // 处理描述...
}

// 验证字符串长度在指定范围内
public void ProcessUsername(string username)
{
    ArgumentCustomException.ThrowIfLengthOutOfRange(username, 3, 20, nameof(username), "用户名长度必须在3-20个字符之间");
    // 处理用户名...
}

// 验证字符串匹配指定的正则表达式模式
public void ProcessPhoneNumber(string phoneNumber)
{
    ArgumentCustomException.ThrowIfNotMatch(phoneNumber, @"^1[3-9]\d{9}$", nameof(phoneNumber), "手机号格式不正确");
    // 处理手机号...
}
数值范围验证
using Sage.Data.Validation;

// 验证数值在指定范围内
public void ProcessAge(int age)
{
    ArgumentCustomException.ThrowIfOutOfRange(age, 0, 150, nameof(age), "年龄必须在0到150之间");
    // 处理年龄...
}

// 验证数值为正数
public void ProcessQuantity(int quantity)
{
    ArgumentCustomException.ThrowIfNotPositive(quantity, nameof(quantity), "数量必须为正数");
    // 处理数量...
}

// 验证数值不为负数
public void ProcessBalance(decimal balance)
{
    ArgumentCustomException.ThrowIfNegative(balance, nameof(balance), "余额不能为负数");
    // 处理余额...
}

// 验证数值不为零
public void ProcessDivisor(double divisor)
{
    ArgumentCustomException.ThrowIfZero(divisor, nameof(divisor), "除数不能为零");
    // 处理除数...
}

// 验证数值大于指定值
public void ProcessScore(int score)
{
    ArgumentCustomException.ThrowIfNotGreaterThan(score, 60, nameof(score), "分数必须大于60分");
    // 处理分数...
}

// 验证数值大于等于指定值
public void ProcessTemperature(double temperature)
{
    ArgumentCustomException.ThrowIfLessThan(temperature, 0, nameof(temperature), "温度不能低于0度");
    // 处理温度...
}

// 验证数值小于指定值
public void ProcessPercentage(double percentage)
{
    ArgumentCustomException.ThrowIfNotLessThan(percentage, 100, nameof(percentage), "百分比必须小于100");
    // 处理百分比...
}

// 验证数值小于等于指定值
public void ProcessDiscount(decimal discount)
{
    ArgumentCustomException.ThrowIfGreaterThan(discount, 1, nameof(discount), "折扣不能大于1");
    // 处理折扣...
}
集合验证
using Sage.Data.Validation;

// 验证集合不为 null 且不为空
public void ProcessItems<T>(IEnumerable<T> items)
{
    ArgumentCustomException.ThrowIfNullOrEmpty(items, nameof(items), "项目列表不能为空");
    // 处理项目列表...
}

// 验证集合元素数量在指定范围内
public void ProcessTags(List<string> tags)
{
    ArgumentCustomException.ThrowIfCountOutOfRange(tags, 1, 10, nameof(tags), "标签数量必须在1-10个之间");
    // 处理标签...
}

// 验证集合中不包含 null 元素
public void ProcessNames(List<string> names)
{
    ArgumentCustomException.ThrowIfContainsNull(names, nameof(names), "名称列表不能包含空项");
    // 处理名称列表...
}

// 验证数组或列表的索引在有效范围内
public void ProcessIndex(int index, List<string> items)
{
    ArgumentCustomException.ThrowIfIndexOutOfRange(index, items.Count, nameof(index), "索引超出范围");
    // 处理索引...
}
业务特定验证
using Sage.Data.Validation;

// 验证邮箱地址格式的有效性
public void ProcessEmail(string email)
{
    ArgumentCustomException.ThrowIfInvalidEmail(email, nameof(email), "邮箱格式不正确");
    // 处理邮箱...
}

// 验证 URL 地址格式的有效性
public void ProcessWebsite(string url)
{
    ArgumentCustomException.ThrowIfInvalidUrl(url, nameof(url), "网址格式不正确");
    // 处理网址...
}

// 验证相对 URL 地址
public void ProcessRelativeUrl(string relativeUrl)
{
    ArgumentCustomException.ThrowIfInvalidUrl(relativeUrl, nameof(relativeUrl), "URL格式不正确", allowRelative: true);
    // 处理相对URL...
}

// 验证 GUID 不为空值
public void ProcessUserId(Guid userId)
{
    ArgumentCustomException.ThrowIfEmptyGuid(userId, nameof(userId), "用户ID不能为空");
    // 处理用户ID...
}

// 验证枚举值是否为已定义的值
public void ProcessStatus(UserStatus status)
{
    ArgumentCustomException.ThrowIfEnumNotDefined(status, nameof(status), "用户状态值无效");
    // 处理用户状态...
}
链式调用示例
using Sage.Data.Validation;

// 链式调用多个验证方法
public void ProcessUserData(string username, int age, List<string> roles)
{
    // 链式调用验证方法
    ArgumentCustomException.ThrowIfNullOrEmpty(username, nameof(username))
        .ThrowIfLengthOutOfRange(3, 20, nameof(username));
        
    ArgumentCustomException.ThrowIfOutOfRange(age, 18, 100, nameof(age));
    
    ArgumentCustomException.ThrowIfNullOrEmpty(roles, nameof(roles))
        .ThrowIfContainsNull(nameof(roles));
        
    // 处理用户数据...
}

许可证

本项目采用 Apache 2.0 许可证。详情请参阅 LICENSE 文件。

贡献

欢迎提交问题报告和改进建议。如果您想贡献代码,请提交拉取请求。

作者

  • LiuPengLai - 甲壳虫科技 欢迎提交问题和功能请求。 QQ Group: 1054304346
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Sage.Data.Validation:

Package Downloads
Sage.CloudStorage.Qiniu

Sage.CloudStorage.Qiniu 是一个基于 .NET 平台的现代化七牛云存储 SDK,采用完全异步设计,提供了对七牛云对象存储、CDN 等服务的简单易用的 API 封装。该库基于 Sage.Http 构建,具有高性能、可扩展的七牛云服务访问能力,特别适合企业级应用和大文件处理场景。 ## 核心优势 - **现代化API设计**:完全异步,符合.NET最佳实践 - **模块化架构**:各组件职责明确,易于扩展和维护 - **丰富的事件机制**:提供上传进度通知和完成事件 - **智能上传策略**:自动选择最佳上传方式和分片大小 - **完善的错误处理**:提供详细的错误信息和恢复机制 ## 功能特性 - **完整的对象存储支持**:上传、下载、管理、删除等操作 - **高级上传功能**: - 智能分片上传(自动优化分片大小) - 断点续传支持 - 并发控制 - 实时进度监控 - **CDN管理**:刷新、预取、带宽查询、日志下载 - **数据处理**:图片处理、音视频转码等 - **批量操作**:批量上传、删除等

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0.2 274 8/25/2025
1.0.0.1 102 7/28/2025
1.0.0 121 7/16/2025

新增了ArgumentCustomException类,提供了验证的方法,支持自定义错误。