Skip to main content
Version: Preview (0.3.0-preview.205)

Struct ValidationResult

Namespace: AdCodicem.ValueObjects
Assembly: AdCodicem.ValueObjects.Abstractions.dll

Outcome of validating a candidate underlying value for a value object.

public readonly struct ValidationResult : IEquatable<ValidationResult>

Implements​

IEquatable<ValidationResult>

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

This is a readonly struct whose successful state is default, so the success path never allocates. Validation is fail-fast: the first violated rule wins and only that rule is reported. Aggregating several errors across several members is the responsibility of a higher level validator (see the FluentValidation integration package).

Properties​

ErrorCode​

Gets the stable, machine-readable code of the violated rule, or null when valid.

public string? ErrorCode { get; }

Property Value​

string?

Remarks​

Well-known codes are listed on .

ErrorMessage​

Gets the human-readable description of the violated rule, or null when valid.

public string? ErrorMessage { get; }

Property Value​

string?

IsValid​

Gets a value indicating whether the candidate value satisfies every rule.

[MemberNotNullWhen(false, "ErrorCode")]
[MemberNotNullWhen(false, "ErrorMessage")]
public bool IsValid { get; }

Property Value​

bool

Success​

Gets the successful result. Equivalent to default.

public static ValidationResult Success { get; }

Property Value​

ValidationResult

Methods​

Equals(ValidationResult)​

Indicates whether the current object is equal to another object of the same type.

public bool Equals(ValidationResult other)

Parameters​

other ValidationResult

An object to compare with this object.

Returns​

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)​

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters​

obj object?

The object to compare with the current instance.

Returns​

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

Failure(string, string)​

Creates a failed result.

public static ValidationResult Failure(string errorCode, string errorMessage)

Parameters​

errorCode string

Stable, machine-readable code of the violated rule.

errorMessage string

Human-readable description of the violated rule.

Returns​

ValidationResult

A failed .

Exceptions​

ArgumentException

errorCode is null or empty.

GetHashCode()​

Returns the hash code for this instance.

public override int GetHashCode()

Returns​

int

A 32-bit signed integer that is the hash code for this instance.

InvalidFormat(string)​

Creates a failed result carrying the code.

public static ValidationResult InvalidFormat(string errorMessage = "The value has an invalid format.")

Parameters​

errorMessage string

Human-readable description of the violated rule.

Returns​

ValidationResult

A failed .

OutOfRange(string)​

Creates a failed result carrying the code.

public static ValidationResult OutOfRange(string errorMessage = "The value is out of range.")

Parameters​

errorMessage string

Human-readable description of the violated rule.

Returns​

ValidationResult

A failed .

Required(string)​

Creates a failed result carrying the code.

public static ValidationResult Required(string errorMessage = "The value is required.")

Parameters​

errorMessage string

Human-readable description of the violated rule.

Returns​

ValidationResult

A failed .

ThrowIfInvalid(Type, object?)​

Throws a when this result is a failure.

public void ThrowIfInvalid(Type valueObjectType, object? attemptedValue)

Parameters​

valueObjectType Type

Value object type the validation was performed for.

attemptedValue object?

Value that was rejected, surfaced on the exception for diagnostics.

Exceptions​

ValueObjectException

This result is a failure.

ToString()​

Returns the fully qualified type name of this instance.

public override string ToString()

Returns​

string

The fully qualified type name.

Operators​

operator ==(ValidationResult, ValidationResult)​

Determines whether two results are equal.

public static bool operator ==(ValidationResult left, ValidationResult right)

Parameters​

left ValidationResult

Left operand.

right ValidationResult

Right operand.

Returns​

bool

true when both results are equal.

operator !=(ValidationResult, ValidationResult)​

Determines whether two results differ.

public static bool operator !=(ValidationResult left, ValidationResult right)

Parameters​

left ValidationResult

Left operand.

right ValidationResult

Right operand.

Returns​

bool

true when both results differ.