Skip to content

Commit

Permalink
Expand validator generator to handle nullable strings and non-strings…
Browse files Browse the repository at this point in the history
… with Notempty on strings
  • Loading branch information
prom3theu5 committed Jan 9, 2024
1 parent 83a8f3b commit 22e1123
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,25 @@ protected override string GenClass(TableInfo properties, string className, Gener
_sourceBuilder.AppendLine();
_sourceBuilder.AppendLine($"namespace {config.Namespace.Pascalize()}.{properties.Schema.Pascalize()}.Validators;");
_sourceBuilder.AppendLine();
_sourceBuilder.AppendLine($"public class {className} : AbstractValidator<{GetPrefixedSuffixed(config, properties.CleanName)}>");
_sourceBuilder.AppendLine($"public class {className} : AbstractValidator<{GetPrefixedSuffixed(config, properties.CleanName)}Entity>");
_sourceBuilder.AppendLine("{");
_sourceBuilder.AppendLine($"\tpublic {className}()");
_sourceBuilder.AppendLine("\t{");
foreach (var column in properties.Columns)
{
if (!column.IsNullable)
if (!column.IsNullable && column.Type.Equals("string", StringComparison.OrdinalIgnoreCase))
{
_sourceBuilder.AppendLine($"\t\tRuleFor(entity => entity.{column.CleanName}).NotEmpty();");
_sourceBuilder.AppendLine($"\t\tRuleFor(entity => entity.{column.CleanName}).NotNull().NotEmpty();");
_sourceBuilder.AppendLine();
}

if (column.MaximumLength > 0 && column.Type != "byte[]")
if (!column.IsNullable && !column.Type.Equals("string", StringComparison.OrdinalIgnoreCase))
{
_sourceBuilder.AppendLine($"\t\tRuleFor(entity => entity.{column.CleanName}).NotNull();");
_sourceBuilder.AppendLine();
}

if (column.MaximumLength > 0 && column.Type.Equals("byte[]", StringComparison.OrdinalIgnoreCase))
{
_sourceBuilder.AppendLine($"\t\tRuleFor(entity => entity.{column.CleanName}).MaximumLength({column.MaximumLength});");
_sourceBuilder.AppendLine();
Expand Down

0 comments on commit 22e1123

Please sign in to comment.