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.
This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.
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 ;
}
}
}