Skip to content

Commit 22e1123

Browse files
committed
Expand validator generator to handle nullable strings and non-strings with Notempty on strings
1 parent 83a8f3b commit 22e1123

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/SimCube.DapperGenerator/Generators/DapperValidatorSourceGenerator.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,25 @@ protected override string GenClass(TableInfo properties, string className, Gener
2222
_sourceBuilder.AppendLine();
2323
_sourceBuilder.AppendLine($"namespace {config.Namespace.Pascalize()}.{properties.Schema.Pascalize()}.Validators;");
2424
_sourceBuilder.AppendLine();
25-
_sourceBuilder.AppendLine($"public class {className} : AbstractValidator<{GetPrefixedSuffixed(config, properties.CleanName)}>");
25+
_sourceBuilder.AppendLine($"public class {className} : AbstractValidator<{GetPrefixedSuffixed(config, properties.CleanName)}Entity>");
2626
_sourceBuilder.AppendLine("{");
2727
_sourceBuilder.AppendLine($"\tpublic {className}()");
2828
_sourceBuilder.AppendLine("\t{");
2929
foreach (var column in properties.Columns)
3030
{
31-
if (!column.IsNullable)
31+
if (!column.IsNullable && column.Type.Equals("string", StringComparison.OrdinalIgnoreCase))
3232
{
33-
_sourceBuilder.AppendLine($"\t\tRuleFor(entity => entity.{column.CleanName}).NotEmpty();");
33+
_sourceBuilder.AppendLine($"\t\tRuleFor(entity => entity.{column.CleanName}).NotNull().NotEmpty();");
3434
_sourceBuilder.AppendLine();
3535
}
3636

37-
if (column.MaximumLength > 0 && column.Type != "byte[]")
37+
if (!column.IsNullable && !column.Type.Equals("string", StringComparison.OrdinalIgnoreCase))
38+
{
39+
_sourceBuilder.AppendLine($"\t\tRuleFor(entity => entity.{column.CleanName}).NotNull();");
40+
_sourceBuilder.AppendLine();
41+
}
42+
43+
if (column.MaximumLength > 0 && column.Type.Equals("byte[]", StringComparison.OrdinalIgnoreCase))
3844
{
3945
_sourceBuilder.AppendLine($"\t\tRuleFor(entity => entity.{column.CleanName}).MaximumLength({column.MaximumLength});");
4046
_sourceBuilder.AppendLine();

0 commit comments

Comments
 (0)