-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ValidationResult.cs
89 lines (79 loc) · 2.89 KB
/
ValidationResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#region License
// Copyright (c) .NET Foundation and contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at https://github.com/FluentValidation/FluentValidation
#endregion
namespace FluentValidation.Results {
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// The result of running a validator
/// </summary>
[Serializable]
public class ValidationResult {
private List<ValidationFailure> _errors;
/// <summary>
/// Whether validation succeeded
/// </summary>
public virtual bool IsValid => Errors.Count == 0;
/// <summary>
/// A collection of errors
/// </summary>
public List<ValidationFailure> Errors {
get => _errors;
#if NETSTANDARD2_0_OR_GREATER
set => _errors = value ?? throw new ArgumentNullException(nameof(value));
#else
init => _errors = value ?? throw new ArgumentNullException(nameof(value));
#endif
}
public string[] RuleSetsExecuted { get; internal set; }
/// <summary>
/// Creates a new validationResult
/// </summary>
public ValidationResult() {
_errors = new List<ValidationFailure>();
}
/// <summary>
/// Creates a new ValidationResult from a collection of failures
/// </summary>
/// <param name="failures">List of <see cref="ValidationFailure"/> which is later available through <see cref="Errors"/>. This list get's copied.</param>
/// <remarks>
/// Every caller is responsible for not adding <c>null</c> to the list.
/// </remarks>
public ValidationResult(IEnumerable<ValidationFailure> failures) {
_errors = failures.Where(failure => failure != null).ToList();
}
internal ValidationResult(List<ValidationFailure> errors) {
_errors = errors;
}
/// <summary>
/// Generates a string representation of the error messages separated by new lines.
/// </summary>
/// <returns></returns>
public override string ToString() {
return ToString(Environment.NewLine);
}
/// <summary>
/// Generates a string representation of the error messages separated by the specified character.
/// </summary>
/// <param name="separator">The character to separate the error messages.</param>
/// <returns></returns>
public string ToString(string separator) {
return string.Join(separator, _errors.Select(failure => failure.ErrorMessage));
}
}
}