Class ValidationError
- Namespace
- Arc4u.Results.Validation
- Assembly
- Arc4u.Results.dll
An FluentResults.Error describing a business validation failure, with an optional code and a Severity.
public class ValidationError : Error, IError, IReason
- Inheritance
-
ErrorValidationError
- Implements
-
IErrorIReason
- Inherited Members
-
Error.CausedBy(IError)Error.ToString()Error.MessageError.MetadataError.Reasons
Examples
Result result = Result.Fail(ValidationError.Create("The name is required.")
.WithCode("NAME_REQUIRED")
.WithSeverity(Severity.Warning));
Remarks
Create it with Create(string). When a failed result containing validation errors is translated to an HTTP response
by Arc4u.AspNetCore.Results, they are grouped by severity in a validation problem details (status 422).
Properties
Code
Gets the code identifying the validation rule that failed; an empty string when not set.
public string Code { get; }
Property Value
Severity
Gets the severity of the validation error; Error by default.
public Severity Severity { get; }
Property Value
Methods
Create(string)
Creates a validation error with the Error severity and an empty code.
public static ValidationError Create(string errorMessage)
Parameters
errorMessagestringThe message describing the validation failure.
Returns
- ValidationError
The new validation error.
WithCode(string)
Sets the code identifying the validation rule that failed.
public ValidationError WithCode(string code)
Parameters
codestringThe code.
Returns
- ValidationError
This error, to chain calls.
WithMetadata(string, object)
Adds a metadata entry to the error and returns this error.
public ValidationError WithMetadata(string key, object value)
Parameters
Returns
- ValidationError
This error, to chain calls.
Remarks
Unlike WithMetadata(string, object) this returns a ValidationError. An existing key is kept: the call is then ignored.
WithSeverity(Severity)
Sets the severity of the validation error.
public ValidationError WithSeverity(Severity severity)
Parameters
severitySeverityThe severity.
Returns
- ValidationError
This error, to chain calls.
Operators
implicit operator Result(ValidationError)
Converts the validation error to a failed FluentResults.Result containing it.
public static implicit operator Result(ValidationError error)
Parameters
errorValidationErrorThe error to wrap.
Returns
- Result