Interface IConfiguresValidator<TValidated>
A builder service which may be used to configure a validator as it is being built.
Namespace: CSF.Validation.ValidatorBuilding
Assembly: CSF.Validation.Abstractions.dll
Syntax
public interface IConfiguresValidator<TValidated>
Type Parameters
Name | Description |
---|---|
TValidated | The validated object type. |
Methods
| Improve this Doc View SourceAddBaseRules<TBase, TBuilder>()
Adds/imports rules from an object that implements IBuildsValidator<TValidated>. These rules should be for validation
of a type from which TValidated
derives.
Declaration
IConfiguresValidator<TValidated> AddBaseRules<TBase, TBuilder>()
where TBuilder : IBuildsValidator<TBase>
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Type Parameters
Name | Description |
---|---|
TBase | The base type, for which validation rules are to be imported. The type |
TBuilder | The type of a class implementing IBuildsValidator<TValidated>, specifying how a validator for the base type should be built. |
Remarks
This allows composition of validators and use of rules declared to validate a base type, reusing validation rules across differing validation scenarios. All of the rules specified in the selected builder-type will be imported and added to the current validator.
Exceptions
Type | Condition |
---|---|
System.InvalidCastException | If the type specified as |
AddRule<TRule>(Action<IConfiguresRule<TRule>>)
Adds a validation rule to validate the validated object instance.
The rule type must be a class that implements IRule<TValidated> for the same
(or a compatible contravariant) generic type TValidated
.
Declaration
IConfiguresValidator<TValidated> AddRule<TRule>(Action<IConfiguresRule<TRule>> ruleDefinition = null)
where TRule : IRule<TValidated>
Parameters
Type | Name | Description |
---|---|---|
System.Action<IConfiguresRule<TRule>> | ruleDefinition | An optional action which defines & configures the validation rule. |
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Type Parameters
Name | Description |
---|---|
TRule | The concrete type of the validation rule. |
AddRules<TBuilder>()
Adds/imports rules from an object that implements IBuildsValidator<TValidated>.
Declaration
IConfiguresValidator<TValidated> AddRules<TBuilder>()
where TBuilder : IBuildsValidator<TValidated>
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Type Parameters
Name | Description |
---|---|
TBuilder | The type of a class implementing IBuildsValidator<TValidated>, specifying how a validator should be built. |
Remarks
This allows composition of validators and reuse of validation rules across differing validation scenarios. All of the rules specified in the selected builder-type will be imported and added to the current validator.
ForMember<TValue>(Expression<Func<TValidated, TValue>>, Action<IConfiguresValueAccessor<TValidated, TValue>>)
Allows addition of validation which will work upon the value of a specific member of the validated object.
Declaration
IConfiguresValidator<TValidated> ForMember<TValue>(Expression<Func<TValidated, TValue>> memberAccessor, Action<IConfiguresValueAccessor<TValidated, TValue>> valueConfig)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<TValidated, TValue>> | memberAccessor | An expression which describes the accessor for the member. |
System.Action<IConfiguresValueAccessor<TValidated, TValue>> | valueConfig | Configuration which indicates what validation will be performed upon the member's value. |
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Type Parameters
Name | Description |
---|---|
TValue | The type (property/field/method-return-type) of the member which is being validated. |
Remarks
Most commonly the member is a property, but it may also be a field or parameterless method (which has a non-void return value).
When it is possible to describe a validated value by a simple member access then this mechanism is preferred over ForValue<TValue>(Func<TValidated, TValue>, Action<IConfiguresValueAccessor<TValidated, TValue>>) because the member name will form a part of the validation rule's identifier.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If either |
ForMemberItems<TValue>(Expression<Func<TValidated, IEnumerable<TValue>>>, Action<IConfiguresValueAccessor<TValidated, TValue>>)
Allows addition of validation which will work upon each of the items exposed by a specific member of the validated object, where that member's type implements System.Collections.Generic.IEnumerable<T>.
Declaration
IConfiguresValidator<TValidated> ForMemberItems<TValue>(Expression<Func<TValidated, IEnumerable<TValue>>> memberAccessor, Action<IConfiguresValueAccessor<TValidated, TValue>> valueConfig)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<TValidated, System.Collections.Generic.IEnumerable<TValue>>> | memberAccessor | An expression which describes the accessor for the collection member. |
System.Action<IConfiguresValueAccessor<TValidated, TValue>> | valueConfig | Configuration which indicates what validation will be performed upon the collection items. |
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Type Parameters
Name | Description |
---|---|
TValue | The type of the items within the collection which shall be validated, where the type (property/field/method-return-type) of the member implements System.Collections.Generic.IEnumerable<T> of this generic type. |
Remarks
Most commonly the member is a property, but it may also be a field or parameterless method (which has a non-void return value).
When it is possible to describe the collection which should have its items validated by using a simple member access then this mechanism is preferred over ForValues<TValue>(Func<TValidated, IEnumerable<TValue>>, Action<IConfiguresValueAccessor<TValidated, TValue>>) because the member name will form a part of the validation rule's identifier.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If either |
ForValue<TValue>(Func<TValidated, TValue>, Action<IConfiguresValueAccessor<TValidated, TValue>>)
Allows addition of validation which will work upon an arbitrary value derived from the validated object.
Declaration
IConfiguresValidator<TValidated> ForValue<TValue>(Func<TValidated, TValue> valueAccessor, Action<IConfiguresValueAccessor<TValidated, TValue>> valueConfig)
Parameters
Type | Name | Description |
---|---|---|
System.Func<TValidated, TValue> | valueAccessor | A function which gets the value to be validated. |
System.Action<IConfiguresValueAccessor<TValidated, TValue>> | valueConfig | Configuration which indicates what validation will be performed upon the value. |
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Type Parameters
Name | Description |
---|---|
TValue | The type of the value which is to be validated. |
Remarks
When it is possible to describe/acces the derived value by a simple member access then please prefer ForMember<TValue>(Expression<Func<TValidated, TValue>>, Action<IConfiguresValueAccessor<TValidated, TValue>>) over this method.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If either |
ForValues<TValue>(Func<TValidated, IEnumerable<TValue>>, Action<IConfiguresValueAccessor<TValidated, TValue>>)
Allows addition of validation which will work upon each of the items exposed by an arbitrary collection, derived from the validated object, which implements System.Collections.Generic.IEnumerable<T>.
Declaration
IConfiguresValidator<TValidated> ForValues<TValue>(Func<TValidated, IEnumerable<TValue>> valuesAccessor, Action<IConfiguresValueAccessor<TValidated, TValue>> valueConfig)
Parameters
Type | Name | Description |
---|---|---|
System.Func<TValidated, System.Collections.Generic.IEnumerable<TValue>> | valuesAccessor | A function which gets the collection, for which its items should be validated. |
System.Action<IConfiguresValueAccessor<TValidated, TValue>> | valueConfig | Configuration which indicates what validation will be performed upon the collection items. |
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Type Parameters
Name | Description |
---|---|
TValue | The type of the items within the collection which shall be validated, where the collection itself implements System.Collections.Generic.IEnumerable<T> of this generic type. |
Remarks
When it is possible to describe/access the collection by a simple member access then please prefer ForMemberItems<TValue>(Expression<Func<TValidated, IEnumerable<TValue>>>, Action<IConfiguresValueAccessor<TValidated, TValue>>) over this method.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If either |
UseObjectIdentity(Func<TValidated, Object>)
Specifies an accessor function which should be used to get the identity of the validated object.
Declaration
IConfiguresValidator<TValidated> UseObjectIdentity(Func<TValidated, object> identityAccessor)
Parameters
Type | Name | Description |
---|---|---|
System.Func<TValidated, System.Object> | identityAccessor | The accessor function. |
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Remarks
Object identities are useful & important when validating complex object models which include object graphs (tree-structures) of related/connected objects. This is particularly true where collections are child objects are involved. The identity is used to differentiate object instances. For example, so that the validation rule results may be matched back to the object to which they relate.
Examples
For example, if validating an instance of the following object:
using System;
using System.Collections.Generic;
public class HumanBeing
{
public Guid Identity { get; set; } = new Guid();
public string Name { get; set; }
public List<HumanBeing> Children { get; } = new List<HumanBeing>();
}
Without any kind of identity, if we had a validation rule that validated the Name
property is not null
or whitespace-only, then what if more than one child failed this same rule? In our feedback (for example some
user interface), how would we indicate which children failed the validation and which children did not.
By using (for example) the Identity
property to uniquely identity each instance of HumanBeing
we will recieve the related identities back with the validation rule results, allowing us to associate the
results back to the appropriate object within the object graph.
ValidateAsAncestor(Int32)
Configures the current value to be validated in the same manner as an ancestor value which already has validation configuration.
Declaration
IConfiguresValidator<TValidated> ValidateAsAncestor(int depth)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | depth | The ancestor number to use when validating the current value. |
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Remarks
If this method is used then the current value will be configured for recursive (or re-entrant) validation. It will be converted as a ManifestItem that is recursive rather than a normal value.
The numeric value of depth
must be a positive integer & indicates the level of ancestor
value is used to provide the recursive validation. For example a value of 1 indicates that the immediate
parent value should be used as the wrapped value for the recursive manifest value. A value of 2 would indicate the
grandparent value.
When recursive validation is configured, no other methods upon this builder may be called. Doing so will raise an exception. The validation of the current value will be treated in the same way as the indicated ancestor value.
WhenValueIs<TDerived>(Action<IConfiguresValidator<TDerived>>)
Adds validation configuration for polymorphic validatation when the runtime type
of the validated value is TDerived
.
Declaration
IConfiguresValidator<TValidated> WhenValueIs<TDerived>(Action<IConfiguresValidator<TDerived>> derivedConfig)
where TDerived : TValidated
Parameters
Type | Name | Description |
---|---|---|
System.Action<IConfiguresValidator<TDerived>> | derivedConfig | Configuration which indicates what validation will be performed upon the value if its runtime type is |
Returns
Type | Description |
---|---|
IConfiguresValidator<TValidated> | A reference to the same builder object, enabling chaining of calls if desired. |
Type Parameters
Name | Description |
---|---|
TDerived | The derived (child) type to be configured for validation in this polymorphic validation configuration. |
Remarks
Where the current validator configuration works to validate an object of type TValidated
,
the value being validated might be a derived type of TValidated
at runtime.
Polymorphic validation allows a developer to specify how a derived type should be validated, if that is the runtime
type of the value to be validated.
This method may be used multiple times, each time for a different type that is derived from TValidated
.