Table of Contents

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
Error
ValidationError
Implements
IError
IReason
Inherited Members
Error.CausedBy(IError)
Error.ToString()
Error.Message
Error.Metadata
Error.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

string

Severity

Gets the severity of the validation error; Error by default.

public Severity Severity { get; }

Property Value

Severity

Methods

Create(string)

Creates a validation error with the Error severity and an empty code.

public static ValidationError Create(string errorMessage)

Parameters

errorMessage string

The 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

code string

The 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

key string

The metadata key.

value object

The metadata value.

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

severity Severity

The 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

error ValidationError

The error to wrap.

Returns

Result