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

AndreuCodina / CrossValidation / 5304511320

18 Jun 2023 03:52PM UTC coverage: 83.752% (-11.7%) from 95.463%
5304511320

Pull #40

github

web-flow
Merge ef6355e4d into d173638cd
Pull Request #40: Update coverage badge in README with pull requests

331 of 394 branches covered (84.01%)

Branch coverage included in aggregate %.

1035 of 1237 relevant lines covered (83.67%)

130.23 hits per line

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

92.06
/src/CrossValidation/ModelValidator.cs
1
using System.Diagnostics.Contracts;
2
using System.Net;
3
using System.Runtime.CompilerServices;
4
using CrossValidation.Errors;
5
using CrossValidation.Exceptions;
6
using CrossValidation.Validations;
7

8
namespace CrossValidation;
9

10
public abstract record ModelValidator<TModel>
11
{
12
    internal ValidationMode _validationMode = ValidationMode.StopOnFirstError;
13
    internal IValidation<TModel>? ScopeCreatorValidation { private get; set; }
393✔
14
    public TModel Model { get; set; } = default!;
42✔
15

16
    public ValidationMode ValidationMode
17
    {
18
        get => _validationMode;
×
19
        set
20
        {
21
            if (ScopeCreatorValidation!.Context!.IsChildContext)
39!
22
            {
23
                throw new InvalidOperationException("Cannot change the validation mode in a child model validator");
×
24
            }
25

26
            _validationMode = value;
39✔
27
            ScopeCreatorValidation.Context!.ValidationMode = _validationMode;
39✔
28
        }
39✔
29
    }
30

31
    [Pure]
32
    public IValidation<TField> Field<TField>(
33
        TField field,
34
        ICrossError? error = null,
35
        string? message = null,
36
        string? code = null,
37
        string? details = null,
38
        HttpStatusCode? httpStatusCode = null,
39
        string? fieldDisplayName = null,
40
        [CallerArgumentExpression(nameof(field))] string fieldName = default!)
41
    {
42
        var fieldPath = fieldName.Contains('.')
30!
43
            ? fieldName.Substring(fieldName.IndexOf('.') + 1)
30✔
44
            : fieldName;
30✔
45
        ScopeCreatorValidation!.FieldPath = fieldPath;
30✔
46
        var getFieldValue = () => field;
92✔
47
        var scopeValidation = ScopeCreatorValidation.CreateScopeValidation(
30✔
48
            getFieldValue: getFieldValue,
30✔
49
            index: null,
30✔
50
            fieldPathToOverride: null);
30✔
51
        scopeValidation.HasFailed = false;
30✔
52
        scopeValidation.GeneralizeError = false;
30✔
53
        return scopeValidation;
30✔
54
    }
55

56
    [Pure]
57
    public IValidation<TField> That<TField>(
58
        TField field,
59
        ICrossError? error = null,
60
        string? message = null,
61
        string? code = null,
62
        string? details = null,
63
        HttpStatusCode? httpStatusCode = null,
64
        string? fieldDisplayName = null)
65
    {
66
        var getFieldValue = () => field;
61✔
67
        var scopeValidation = ScopeCreatorValidation!.CreateScopeValidation(
21✔
68
            getFieldValue: getFieldValue,
21✔
69
            index: null,
21✔
70
            fieldPathToOverride: null);
21✔
71
        scopeValidation.HasFailed = false;
21✔
72
        scopeValidation.GeneralizeError = true;
21✔
73
        return scopeValidation;
21✔
74
    }
75

76
    public abstract void CreateValidations();
77

78
    public void Validate(TModel model)
79
    {
80
        InternalValidateAsync(model, useAsync: false)
26✔
81
            .GetAwaiter()
26✔
82
            .GetResult();
26✔
83
    }
1✔
84
    
85
    public async Task ValidateAsync(TModel model)
86
    {
87
        await InternalValidateAsync(model, useAsync: true);
14✔
88
    }
×
89

90
    private async ValueTask InternalValidateAsync(TModel model, bool useAsync)
91
    {
92
        CrossValidation.Validate.ModelNullability(model);
40✔
93
        Model = model;
40✔
94
        ScopeCreatorValidation = CrossValidation.Validate
40✔
95
                .That(model);
40✔
96
        ScopeCreatorValidation.IsScopeCreator = true;
40✔
97
        ScopeCreatorValidation.SetScope(CreateValidations, ScopeType.ModelValidator);
40✔
98

99
        if (useAsync)
33✔
100
        {
101
            await ScopeCreatorValidation.ValidateAsync();
14✔
102
        }
103
        
104
        if (ScopeCreatorValidation.Context!.ErrorsCollected.Any())
29✔
105
        {
106
            if (ScopeCreatorValidation.Context.ErrorsCollected.Count == 1)
28✔
107
            {
108
                throw ScopeCreatorValidation.Context
17✔
109
                    .ErrorsCollected[0]
17✔
110
                    .ToException();
17✔
111
            }
112
            else
113
            {
114
                foreach (var errorCollected in ScopeCreatorValidation.Context.ErrorsCollected)
76✔
115
                {
116
                    errorCollected.AddPlaceholderValues();
27✔
117
                }
118
                
119
                throw new ValidationListException(ScopeCreatorValidation.Context.ErrorsCollected);
11✔
120
            }
121
        }
122
    }
1✔
123
}
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