Skip to main content

Class ValueObjectRuleBuilderExtensions

Namespace: AdCodicem.ValueObjects.FluentValidation
Assembly: AdCodicem.ValueObjects.FluentValidation.dll

FluentValidation rules built on the rules a value object already enforces.

public static class ValueObjectRuleBuilderExtensions

Inheritance

objectValueObjectRuleBuilderExtensions

Inherited Members

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks

The point is to state a rule once. A command carrying a raw string for an IBAN should not restate the length, the pattern and the check-digit rule in its validator: it should defer to the value object that owns them, and report the same stable error code the rest of the system uses.

Methods

MustParseAs<T>(IRuleBuilder<T, string?>, Type)

Requires the text to be acceptable to a value object type.

public static IRuleBuilderOptions<T, string?> MustParseAs<T>(this IRuleBuilder<T, string?> ruleBuilder, Type valueObjectType)

Parameters

ruleBuilder IRuleBuilder<T, string?>

Rule builder for a text member.

valueObjectType Type

Value object the text must parse into.

Returns

IRuleBuilderOptions<T, string?>

The rule, so it can be configured further.

Type Parameters

T

Validated object.

Remarks

The value object type is passed as a rather than a type argument so that RuleFor(x => x.Iban).MustParseAs(typeof(Iban)) stays readable: C# cannot infer one type argument while another is given explicitly, and spelling out the validated type at every rule is noise.

MustSatisfy<T, TSelf, TValue>(IRuleBuilder<T, TValue>)

Requires an underlying value to satisfy the rules of a value object, without constructing it.

public static IRuleBuilderOptions<T, TValue> MustSatisfy<T, TSelf, TValue>(this IRuleBuilder<T, TValue> ruleBuilder) where TSelf : struct, IValueObject<TSelf, TValue>

Parameters

ruleBuilder IRuleBuilder<T, TValue>

Rule builder for a member holding the underlying value.

Returns

IRuleBuilderOptions<T, TValue>

The rule, so it can be configured further.

Type Parameters

T

Validated object.

TSelf

Value object type.

TValue

Underlying value type.

NotDefault<T, TSelf, TValue>(IRuleBuilder<T, TSelf>)

Requires a value object member to hold a value that actually went through validation.

public static IRuleBuilderOptions<T, TSelf> NotDefault<T, TSelf, TValue>(this IRuleBuilder<T, TSelf> ruleBuilder) where TSelf : struct, IValueObject<TSelf, TValue>

Parameters

ruleBuilder IRuleBuilder<T, TSelf>

Rule builder for a value object member.

Returns

IRuleBuilderOptions<T, TSelf>

The rule, so it can be configured further.

Type Parameters

T

Validated object.

TSelf

Value object type.

TValue

Underlying value type.

Remarks

Catches the one hole a struct value object cannot close by itself: an uninitialized instance, which the CLR always allows and which the analyzer only catches where it can see the code.