Class ValidationError
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.RailwayOrientedProgramming.dll
Represents validation errors for one or more fields. Used when input data fails business rules or constraints.
public sealed class ValidationError : Error, IEquatable<Error>, IEquatable<ValidationError>
- Inheritance
-
ValidationError
- Implements
- Inherited Members
- Extension Methods
Examples
// Single field validation
var error = Error.Validation("Email is required", "email");
// Multiple field validation using fluent API
var multiError = ValidationError.For("email", "Email is required")
.And("password", "Password must be at least 8 characters")
.And("age", "Age must be 18 or older");
// Multiple errors for same field
var complexError = ValidationError.For("password", "Must be at least 8 characters")
.And("password", "Must contain a number")
.And("password", "Must contain a special character");
Remarks
ValidationError can hold errors for multiple fields, making it ideal for form validation scenarios. Use the fluent And(string, string) method to add multiple field errors.
Maps to HTTP 400 Bad Request when converted to HTTP responses.
Constructors
ValidationError(IEnumerable<FieldError>, string, string, string?)
Creates a validation error with multiple field errors.
public ValidationError(IEnumerable<ValidationError.FieldError> fieldErrors, string code, string detail = "", string? instance = null)
Parameters
fieldErrorsIEnumerable<ValidationError.FieldError>Collection of field-specific errors.
codestringMachine-readable error code.
detailstringOverall error description.
instancestringOptional identifier for the instance being validated.
Exceptions
- ArgumentException
Thrown when no field errors are provided.
ValidationError(string, string, string, string?, string?)
Creates a validation error for a single field.
public ValidationError(string fieldDetail, string fieldName, string code, string? detail = null, string? instance = null)
Parameters
fieldDetailstringError message describing what's wrong with the field.
fieldNamestringName of the field that failed validation.
codestringMachine-readable error code (defaults to "validation.error").
detailstringOptional overall error detail. If null, uses fieldDetail.
instancestringOptional identifier for the instance being validated.
Exceptions
- ArgumentException
Thrown when fieldDetail is null or whitespace.
Properties
FieldErrors
Gets the collection of field-specific validation errors.
public ImmutableArray<ValidationError.FieldError> FieldErrors { get; }
Property Value
- ImmutableArray<ValidationError.FieldError>
An immutable array of ValidationError.FieldError instances.
Methods
And(string, string)
Adds another field error to this validation error (fluent API).
public ValidationError And(string fieldName, string message)
Parameters
Returns
- ValidationError
A new ValidationError containing all field errors.
Remarks
This method creates a new instance; it does not mutate the original.
And(string, params string[])
Adds multiple error messages for a single field (fluent API).
public ValidationError And(string fieldName, params string[] messages)
Parameters
fieldNamestringName of the field.
messagesstring[]Multiple validation error messages for this field.
Returns
- ValidationError
A new ValidationError containing all field errors.
Remarks
This method creates a new instance; it does not mutate the original.
Equals(ValidationError?)
Determines whether the specified ValidationError is equal to the current instance.
public bool Equals(ValidationError? other)
Parameters
otherValidationErrorThe ValidationError to compare with the current instance.
Returns
- bool
trueif the specified object is equal to the current instance; otherwise,false.
Equals(object?)
Determines whether the specified object is equal to the current instance.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
trueif the specified object is equal to the current instance; otherwise,false.
For(string, string, string, string?, string?)
Creates a new validation error for a single field (fluent factory method).
public static ValidationError For(string fieldName, string message, string code = "validation.error", string? detail = null, string? instance = null)
Parameters
fieldNamestringName of the field.
messagestringValidation error message.
codestringError code (defaults to "validation.error").
detailstringOptional overall detail message.
instancestringOptional instance identifier.
Returns
- ValidationError
A new ValidationError instance.
Remarks
Use this as a starting point, then chain with And(string, string) to add more field errors.
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer hash code.
Merge(ValidationError)
Merges this validation error with another, combining field errors and details.
public ValidationError Merge(ValidationError other)
Parameters
otherValidationErrorThe validation error to merge with.
Returns
- ValidationError
A new ValidationError containing errors from both instances.
Remarks
Errors for the same field are combined without duplicates. Detail messages are concatenated if they differ. Error codes are combined if they differ.
This method creates a new instance; it does not mutate the original.
ToString()
Returns a string representation of this validation error including all field errors.
public override string ToString()
Returns
- string
A formatted string containing the base error information and all field-specific error details.