• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / EntityFrameworkCore.Generator / 27760980405

18 Jun 2026 12:56PM UTC coverage: 75.418% (+0.7%) from 74.693%
27760980405

push

github

pwelter34
fix build

1007 of 1723 branches covered (58.44%)

Branch coverage included in aggregate %.

4221 of 5209 relevant lines covered (81.03%)

1242.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.03
/src/EntityFrameworkCore.Generator.Core/Templates/ValidatorClassTemplate.cs
1
using EntityFrameworkCore.Generator.Extensions;
2
using EntityFrameworkCore.Generator.Metadata.Generation;
3
using EntityFrameworkCore.Generator.Options;
4

5
namespace EntityFrameworkCore.Generator.Templates;
6

7
public class ValidatorClassTemplate : CodeTemplateBase
8
{
9
    private readonly Model _model;
10

11
    public ValidatorClassTemplate(Model model, GeneratorOptions options) : base(options)
257✔
12
    {
13
        _model = model;
257✔
14
    }
257✔
15

16
    public override string WriteCode()
17
    {
18
        CodeBuilder.Clear();
257✔
19

20
        if (Options.Model.Validator.Header.HasValue())
257!
21
            CodeBuilder.AppendLine(Options.Model.Validator.Header).AppendLine();
×
22

23
        CodeBuilder.AppendLine("using System;");
257✔
24
        CodeBuilder.AppendLine();
257✔
25
        CodeBuilder.AppendLine("using FluentValidation;");
257✔
26

27
        if (_model.ModelNamespace != _model.ValidatorNamespace)
257!
28
            CodeBuilder.AppendLine($"using {_model.ModelNamespace};");
257✔
29

30
        CodeBuilder.AppendLine();
257✔
31

32
        CodeBuilder.Append($"namespace {_model.ValidatorNamespace}");
257✔
33

34
        if (Options.Project.FileScopedNamespace)
257✔
35
        {
36
            CodeBuilder.AppendLine(";");
256✔
37
            CodeBuilder.AppendLine();
256✔
38
            GenerateClass();
256✔
39
        }
40
        else
41
        {
42
            CodeBuilder.AppendLine();
1✔
43
            CodeBuilder.AppendLine("{");
1✔
44

45
            using (CodeBuilder.Indent())
1✔
46
            {
47
                GenerateClass();
1✔
48
            }
1✔
49

50
            CodeBuilder.AppendLine("}");
1✔
51
        }
52

53
        return CodeBuilder.ToString();
257✔
54
    }
55

56
    private void GenerateClass()
57
    {
58
        var validatorClass = _model.ValidatorClass.ToSafeName();
257✔
59
        var modelClass = _model.ModelClass.ToSafeName();
257✔
60
        var modelFullName = $"{_model.ModelNamespace}.{modelClass}";
257✔
61

62
        if (Options.Model.Validator.Document)
257!
63
        {
64
            GenerateClassDocumentation(modelFullName);
257✔
65
        }
66
        if (Options.Model.Validator.Attributes.HasValue())
257!
67
        {
68
            CodeBuilder.AppendLine(Options.Model.Validator.Attributes);
×
69
        }
70
        CodeBuilder.AppendLine($"public partial class {validatorClass}");
257✔
71

72
        if (_model.ValidatorBaseClass.HasValue())
257✔
73
        {
74
            var validatorBase = _model.ValidatorBaseClass.ToSafeName();
256✔
75
            using (CodeBuilder.Indent())
256✔
76
                CodeBuilder.AppendLine($": {validatorBase}");
256✔
77
        }
78

79
        CodeBuilder.AppendLine("{");
257✔
80

81
        using (CodeBuilder.Indent())
257✔
82
        {
83
            GenerateConstructor();
257✔
84
        }
257✔
85

86
        CodeBuilder.AppendLine("}");
257✔
87
    }
257✔
88

89
    private void GenerateConstructor()
90
    {
91
        var validatorClass = _model.ValidatorClass.ToSafeName();
257✔
92
        var validatorFullName = $"{_model.ValidatorNamespace}.{validatorClass}";
257✔
93
        var modelClass = _model.ModelClass.ToSafeName();
257✔
94
        var modelFullName = $"{_model.ModelNamespace}.{modelClass}";
257✔
95

96
        if (Options.Model.Validator.Document)
257!
97
        {
98
            GenerateConstructorDocumentation(validatorFullName, modelFullName);
257✔
99
        }
100

101
        CodeBuilder.AppendLine($"public {validatorClass}()");
257✔
102
        CodeBuilder.AppendLine("{");
257✔
103

104
        using (CodeBuilder.Indent())
257✔
105
        {
106
            CodeBuilder.AppendLine("#region Generated Constructor");
257✔
107
            foreach (var property in _model.Properties)
4,410✔
108
            {
109
                if (property.IsComputed == true || property.IsIdentity == true || property.IsRowVersion == true)
1,948✔
110
                    continue;
111

112
                var propertyName = property.PropertyName.ToSafeName();
1,780✔
113

114
                if (property.IsRequired && property.SystemType == typeof(string))
1,780✔
115
                    CodeBuilder.AppendLine($"RuleFor(p => p.{propertyName}).NotEmpty();");
262✔
116

117
                if (property.Size.HasValue && property.SystemType == typeof(string) && property.Size > 0)
1,780✔
118
                    CodeBuilder.AppendLine($"RuleFor(p => p.{propertyName}).MaximumLength({property.Size});");
530✔
119

120
            }
121
            CodeBuilder.AppendLine("#endregion");
257✔
122
        }
257✔
123

124
        CodeBuilder.AppendLine("}");
257✔
125
        CodeBuilder.AppendLine();
257✔
126
    }
257✔
127

128
    private void GenerateClassDocumentation(string modelClass)
129
    {
130
        var modelType = _model.ModelType switch
257!
131
        {
257✔
132
            ModelType.Create => "create",
129✔
133
            ModelType.Update => "update",
128✔
134
            _ => "read"
×
135
        };
257✔
136

137
        var entityName = ToXmlText(_model.Entity?.EntityClass);
257!
138
        var sourceName = ToXmlText(GetQualifiedTableName());
257✔
139
        var sourceType = _model.Entity?.IsView == true ? "view" : "table";
257!
140

141
        CodeBuilder.AppendLine("/// <summary>");
257✔
142

143
        if (sourceName.HasValue())
257!
144
            CodeBuilder.AppendLine($"/// Defines FluentValidation rules for the <see cref=\"{modelClass}\" /> {modelType} model for the <c>{entityName}</c> entity mapped to the <c>{sourceName}</c> {sourceType}.");
257✔
145
        else if (entityName.HasValue())
×
146
            CodeBuilder.AppendLine($"/// Defines FluentValidation rules for the <see cref=\"{modelClass}\" /> {modelType} model for the <c>{entityName}</c> entity.");
×
147
        else
148
            CodeBuilder.AppendLine($"/// Defines FluentValidation rules for the <see cref=\"{modelClass}\" /> {modelType} model.");
×
149

150
        CodeBuilder.AppendLine("/// </summary>");
257✔
151
    }
257✔
152

153
    private void GenerateConstructorDocumentation(string validatorClass, string modelClass)
154
    {
155
        CodeBuilder.AppendLine("/// <summary>");
257✔
156
        CodeBuilder.AppendLine($"/// Initializes a new instance of the <see cref=\"{validatorClass}\"/> class and configures validation rules for <see cref=\"{modelClass}\" />.");
257✔
157
        CodeBuilder.AppendLine("/// </summary>");
257✔
158
    }
257✔
159

160
    private string? GetQualifiedTableName()
161
    {
162
        if (_model.Entity?.TableName.IsNullOrEmpty() != false)
257!
163
            return _model.Entity?.TableName;
×
164

165
        return _model.Entity.TableSchema.HasValue()
257✔
166
            ? $"{_model.Entity.TableSchema}.{_model.Entity.TableName}"
257✔
167
            : _model.Entity.TableName;
257✔
168
    }
169

170
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc