Skip to main content
Version: 0.3.x (0.3.0)

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 says whether the member must be there, and so does an left at its default, which is how an absent member of that type is left. Empty text is the value object's to judge, as any text is. A is parsed in the invariant culture, as the binders read text, unless says otherwise; each element of an of is parsed in turn, a null element skipped, and the first refused is reported under the member's own name. A member of any other type throws , a misuse VO0039 reports at compile time.

A refusal is a , which carries the code. Its message is the English message of the rule unless the attribute declares one: , or a resource with its name, give DataAnnotations' single message, {0} the member; alone looks the message up by the code of the rule, under the keys of , with the arguments of the rule from {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 , and finds its descriptor the first time a value is validated: registered, or described by reflection where the runtime supports dynamic code. Under native AOT, a value object written by hand and a construction of a generic one are found only once registered by hand, or through the type arguments of , which needs no registry.

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​

ArgumentNullException

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​

bool

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​

Type

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​

string

An instance of the formatted error message.

Remarks​

The message DataAnnotations formats on its own, outside a validation, as does: with alone, which DataAnnotations would refuse, its default message.

Exceptions​

InvalidOperationException

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​

ValidationResult?

An instance of the class.

Exceptions​

InvalidOperationException

The current attribute is malformed.

NotImplementedException

has not been implemented by a derived class.

InvalidOperationException

The member is neither text nor a collection of text, or the type is not a value object the registry describes.