2019-12-08 19:54:52 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-12-04 18:57:18 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2019-12-08 19:54:52 +01:00
|
|
|
|
namespace Swan.Validators {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Defines a validation result containing all validation errors and their properties.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ObjectValidationResult {
|
|
|
|
|
private readonly List<ValidationError> _errors = new List<ValidationError>();
|
|
|
|
|
|
2019-12-04 18:57:18 +01:00
|
|
|
|
/// <summary>
|
2019-12-08 19:54:52 +01:00
|
|
|
|
/// A list of errors.
|
2019-12-04 18:57:18 +01:00
|
|
|
|
/// </summary>
|
2019-12-08 19:54:52 +01:00
|
|
|
|
public IReadOnlyList<ValidationError> Errors => this._errors;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <c>true</c> if there are no errors; otherwise, <c>false</c>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Boolean IsValid => !this.Errors.Any();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds an error with a specified property name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="propertyName">The property name.</param>
|
|
|
|
|
/// <param name="errorMessage">The error message.</param>
|
|
|
|
|
public void Add(String propertyName, String errorMessage) => this._errors.Add(new ValidationError { ErrorMessage = errorMessage, PropertyName = propertyName });
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Defines a validation error.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ValidationError {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The property name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public String PropertyName {
|
|
|
|
|
get; internal set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The message error.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public String ErrorMessage {
|
|
|
|
|
get; internal set;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-04 18:57:18 +01:00
|
|
|
|
}
|