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

Class NotDefaultAttribute

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

Requires a value-object member not to hold the instance equal to default(TSelf), which lets through, since a struct is never null.

[AttributeUsage(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class NotDefaultAttribute : ValidationAttribute

Inheritance​

object ← Attribute ← ValidationAttribute ← NotDefaultAttribute

Inherited Members​

ValidationAttribute.FormatErrorMessage(string), ValidationAttribute.GetValidationResult(object?, ValidationContext), ValidationAttribute.IsValid(object?), ValidationAttribute.Validate(object?, ValidationContext), ValidationAttribute.Validate(object?, string), ValidationAttribute.ErrorMessage, ValidationAttribute.ErrorMessageResourceName, ValidationAttribute.ErrorMessageResourceType, 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.ReferenceEquals(object?, object?), object.ToString()

Remarks​

A null passes, so that an absent TSelf? is 's to judge; a TSelf? holding the default instance is refused. The check reads through the descriptor of the value object's type, so that over a value type a constructed zero, OrderId.Create(Guid.Empty), is refused too, as FluentValidation's NotDefault refuses it. A member that is no value object throws , a misuse VO0041 reports at compile time, and so does a collection of value objects, whose elements are not checked.

A refusal is a carrying , with 's message, The {0} field is required., so that an answer reads the same whichever refused. , or a resource with its name, replace it as on any attribute; alone looks the message up under ValueObject.value_object.required, {0} the member, as FluentValidation's NotDefault does.

The type is found in the registry, 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 registered by hand first.

Constructors​

NotDefaultAttribute()​

Creates the attribute, whose message is 's, The {0} field is required..

public NotDefaultAttribute()

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: with alone, which DataAnnotations would refuse, 's.

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 no value object, or its type is not a value object the registry describes.