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

loresoft / EntityFrameworkCore.Generator / 15303740763

28 May 2025 03:07PM UTC coverage: 54.841% (-0.2%) from 55.036%
15303740763

push

github

pwelter34
add file header support

642 of 1331 branches covered (48.23%)

Branch coverage included in aggregate %.

14 of 33 new or added lines in 14 files covered. (42.42%)

3 existing lines in 3 files now uncovered.

1873 of 3255 relevant lines covered (57.54%)

61.12 hits per line

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

0.0
/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)
×
12
    {
13
        _model = model;
×
14
    }
×
15

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

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

23
        CodeBuilder.AppendLine("using System;");
×
NEW
24
        CodeBuilder.AppendLine();
×
UNCOV
25
        CodeBuilder.AppendLine("using FluentValidation;");
×
26

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

30
        CodeBuilder.AppendLine();
×
31

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

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

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

50
            CodeBuilder.AppendLine("}");
×
51
        }
52

53
        return CodeBuilder.ToString();
×
54
    }
55

56
    private void GenerateClass()
57
    {
58
        var validatorClass = _model.ValidatorClass.ToSafeName();
×
59
        var modelClass = _model.ModelClass.ToSafeName();
×
60

61
        if (Options.Model.Validator.Document)
×
62
        {
63
            CodeBuilder.AppendLine("/// <summary>");
×
64
            CodeBuilder.AppendLine($"/// Validator class for <see cref=\"{modelClass}\"/> .");
×
65
            CodeBuilder.AppendLine("/// </summary>");
×
66
        }
67
        if (Options.Model.Validator.Attributes.HasValue())
×
68
        {
69
            CodeBuilder.AppendLine(Options.Model.Validator.Attributes);
×
70
        }
71
        CodeBuilder.AppendLine($"public partial class {validatorClass}");
×
72

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

80
        CodeBuilder.AppendLine("{");
×
81

82
        using (CodeBuilder.Indent())
×
83
        {
84
            GenerateConstructor();
×
85
        }
×
86

87
        CodeBuilder.AppendLine("}");
×
88
    }
×
89

90
    private void GenerateConstructor()
91
    {
92
        var validatorClass = _model.ValidatorClass.ToSafeName();
×
93

94
        if (Options.Model.Validator.Document)
×
95
        {
96
            CodeBuilder.AppendLine("/// <summary>");
×
97
            CodeBuilder.AppendLine($"/// Initializes a new instance of the <see cref=\"{validatorClass}\"/> class.");
×
98
            CodeBuilder.AppendLine("/// </summary>");
×
99
        }
100

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

104
        using (CodeBuilder.Indent())
×
105
        {
106
            CodeBuilder.AppendLine("#region Generated Constructor");
×
107
            foreach (var property in _model.Properties)
×
108
            {
109
                if (property.ValueGenerated.HasValue)
×
110
                    continue;
111

112
                var propertyName = property.PropertyName.ToSafeName();
×
113

114
                if (property.IsRequired && property.SystemType == typeof(string))
×
115
                    CodeBuilder.AppendLine($"RuleFor(p => p.{propertyName}).NotEmpty();");
×
116
                if (property.Size.HasValue && property.SystemType == typeof(string) && property.Size > 0)
×
117
                    CodeBuilder.AppendLine($"RuleFor(p => p.{propertyName}).MaximumLength({property.Size});");
×
118

119
            }
120
            CodeBuilder.AppendLine("#endregion");
×
121
        }
×
122

123
        CodeBuilder.AppendLine("}");
×
124
        CodeBuilder.AppendLine();
×
125
    }
×
126

127
}
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

© 2025 Coveralls, Inc