Class ParsesAsAttribute
Namespace: AdCodicem.ValueObjects.Annotations
Assembly: AdCodicem.ValueObjects.Abstractions.dll
Requires text to be accepted by a value object, and reports the message and the code of the rule that refuses it.
[AttributeUsage(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter, AllowMultiple = false)]
public class ParsesAsAttribute : ValidationAttribute
Inheritance
object ← Attribute ← ValidationAttribute ← ParsesAsAttribute
Derived
ParsesAsAttribute<TSelf, TValue>
Inherited Members
ValidationAttribute.FormatErrorMessage(string), ValidationAttribute.GetValidationResult(object?, ValidationContext), ValidationAttribute.IsValid(object?), ValidationAttribute.IsValid(object?, ValidationContext), ValidationAttribute.Validate(object?, ValidationContext), ValidationAttribute.Validate(object?, string), ValidationAttribute.ErrorMessage, ValidationAttribute.ErrorMessageResourceName, ValidationAttribute.ErrorMessageResourceType, ValidationAttribute.ErrorMessageString, ValidationAttribute.RequiresValidationContext, Attribute.Equals(object?), Attribute.GetCustomAttribute(Assembly, Type), Attribute.GetCustomAttribute(Assembly, Type, bool), Attribute.GetCustomAttribute(MemberInfo, Type), Attribute.GetCustomAttribute(MemberInfo, Type, bool), Attribute.GetCustomAttribute(Module, Type), Attribute.GetCustomAttribute(Module, Type, bool), Attribute.GetCustomAttribute(ParameterInfo, Type), Attribute.GetCustomAttribute(ParameterInfo, Type, bool), Attribute.GetCustomAttributes(Assembly), Attribute.GetCustomAttributes(Assembly, bool), Attribute.GetCustomAttributes(Assembly, Type), Attribute.GetCustomAttributes(Assembly, Type, bool), Attribute.GetCustomAttributes(MemberInfo), Attribute.GetCustomAttributes(MemberInfo, bool), Attribute.GetCustomAttributes(MemberInfo, Type), Attribute.GetCustomAttributes(MemberInfo, Type, bool), Attribute.GetCustomAttributes(Module), Attribute.GetCustomAttributes(Module, bool), Attribute.GetCustomAttributes(Module, Type), Attribute.GetCustomAttributes(Module, Type, bool), Attribute.GetCustomAttributes(ParameterInfo), Attribute.GetCustomAttributes(ParameterInfo, bool), Attribute.GetCustomAttributes(ParameterInfo, Type), Attribute.GetCustomAttributes(ParameterInfo, Type, bool), Attribute.GetHashCode(), Attribute.IsDefaultAttribute(), Attribute.IsDefined(Assembly, Type), Attribute.IsDefined(Assembly, Type, bool), Attribute.IsDefined(MemberInfo, Type), Attribute.IsDefined(MemberInfo, Type, bool), Attribute.IsDefined(Module, Type), Attribute.IsDefined(Module, Type, bool), Attribute.IsDefined(ParameterInfo, Type), Attribute.IsDefined(ParameterInfo, Type, bool), Attribute.Match(object?), Attribute.TypeId, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()
Remarks
A DTO, an options class or a form model holding the text a value object is made from later states no rule of its own:
[ParsesAs(typeof(Iban))] public string? Account { get; set; } holds the text to the rules the type declares,
its normalization included, wherever DataAnnotations runs: Validator, MVC, AddValidation(), options
validation and ObservableValidator. It validates and changes nothing: the member keeps the text as it was sent.
A null passes, so that VO0039 reports at compile time.
A refusal is a {0} the member; {1},
in the current UI culture, and keeps the English message for a key the resource lacks. The message never quotes the
text.
This form names the value object by its
Constructors
ParsesAsAttribute(Type)
Names the value object the text must be accepted by, which the registry describes the first time a value is validated.
public ParsesAsAttribute(Type valueObjectType)
Parameters
valueObjectType Type
The value object type, or its nullable type, which names the same value object.
Exceptions
valueObjectType is null.
Properties
UseCurrentCulture
Gets or sets whether the text is read in the current culture rather than the invariant one.
public bool UseCurrentCulture { get; set; }
Property Value
Remarks
The binders, the configuration binder and MustParseAs read the invariant culture, which refuses
12,5. A form a person types in, in French, may set it, and the application then converts the text in the
same culture when it reads it.
ValueObjectType
Gets the value object the text must be accepted by: the type named, or the value object a nullable type names,
Iban for typeof(Iban?), as a refusal reports it.
public Type ValueObjectType { get; }
Property Value
Methods
FormatErrorMessage(string)
Applies formatting to an error message, based on the data field where the error occurred.
public override string FormatErrorMessage(string name)
Parameters
name string
The name to include in the formatted message.
Returns
An instance of the formatted error message.
Remarks
The message DataAnnotations formats on its own, outside a validation, as
Exceptions
The current attribute is malformed.
IsValid(object?, ValidationContext)
Validates the specified value with respect to the current validation attribute.
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
Parameters
value object?
The value to validate.
validationContext ValidationContext
The context information about the validation operation.
Returns
An instance of the
Exceptions
The current attribute is malformed.
The member is neither text nor a collection of text, or the type is not a value object the registry describes.