You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kev/Drawer/DataValidator/Rule/Common/NotNullValidationRule.cs

50 lines
1.5 KiB
C#

1 month ago
using System.Data;
using Validation.Core;
namespace Validation.Rule.Common
{
/// <summary>
/// 非空校验规则
/// </summary>
public class NotNullValidationRule : IValidationRule
{
public string RuleName => "NotNull";
public string Description => "检查数据是否包含空值";
/// <inheritdoc />
public ValidationResult Validate(DataTable dataTable)
{
var result = new ValidationResult { IsValid = true };
foreach (DataRow row in dataTable.Rows)
{
foreach (DataColumn col in dataTable.Columns)
{
if (row.IsNull(col))
{
int rowIndex = dataTable.Rows.IndexOf(row);
result.AddError(rowIndex, $"第{rowIndex + 1}行,列'{col.ColumnName}'不能为空");
}
}
}
return result;
}
/// <inheritdoc />
public ValidationResult Validate(string[] headers, int rowIndex, object[] values)
{
var result = new ValidationResult { IsValid = true };
for (int i = 0; i < headers.Length && i < values.Length; i++)
{
if (values[i] == null || values[i] == DBNull.Value)
{
result.AddError(rowIndex, $"第{rowIndex + 1}行,列'{headers[i]}'不能为空");
}
}
return result;
}
}
}