Class NotNullOrEmpty<T>
A validation rule which passes if the object being validated is both not null and not an empty collection. The rule fails if it is either.
Inheritance
Implements
Inherited Members
Namespace: CSF.Validation.Rules
Assembly: CSF.Validation.StandardRules.dll
Syntax
public class NotNullOrEmpty<T> : IRuleWithMessage<ICollection<T>>, IRule<ICollection<T>>, IGetsFailureMessage<ICollection<T>>, IRuleWithMessage<IReadOnlyCollection<T>>, IRule<IReadOnlyCollection<T>>, IGetsFailureMessage<IReadOnlyCollection<T>>, IRuleWithMessage<IQueryable<T>>, IRule<IQueryable<T>>, IGetsFailureMessage<IQueryable<T>>
Type Parameters
Name | Description |
---|---|
T |
Remarks
Internally, this rule combines the NotNull & NotEmpty<T> rules into one. See the remarks to the NotEmpty<T> rule for information on why this rule is often the superior choice when working with System.Linq.IQueryable<T>, for example.
This rule will always return a synchronous result.
Constructors
| Improve this Doc View SourceNotNullOrEmpty(NotNull, NotEmpty<T>)
Initialises a new instance of NotNullOrEmpty<T>.
Declaration
public NotNullOrEmpty(NotNull notNull, NotEmpty<T> notEmpty)
Parameters
Type | Name | Description |
---|---|---|
NotNull | notNull | A not-null rule. |
NotEmpty<T> | notEmpty | A not-empty rule. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If either constructor parameter is null. |
Methods
| Improve this Doc View SourceGetResultAsync(ICollection<T>, RuleContext, CancellationToken)
Performs the validation logic asynchronously and returns a task of RuleResult.
Declaration
public async ValueTask<RuleResult> GetResultAsync(ICollection<T> validated, RuleContext context, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.ICollection<T> | validated | |
RuleContext | context | Contextual information about the validation |
System.Threading.CancellationToken | token | An object which may be used to cancel the process |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A task which provides a result object, indicating the result of validation |
Remarks
This method receives the value to be validated as well as an object which represents the context in which this rule is running. It should return a task of RuleResult.
In order to create the result object, particularly if your rule logic will run synchronously,
consider using the CommonResults class via using static CSF.Validation.Rules.CommonResults;
in your
rule logic.
The common results class has helper methods such as PassAsync(IDictionary<String, Object>)
and FailAsync(IDictionary<String, Object>)
which include optimisations for flyweight task instances that avoid allocating additional resources
needlessly.
It is acceptable to throw an uncaught exception from this method, as the validation framework will catch it and automatically convert it into an error result. Generally, developers do not need to manually return a result of outcome Errored manually. This would be appropriate only in an unusual scenario that is considered an error, but which does not involve the throwing of an exception. Error results are generally harder for the consumer to deal with than failure results.
The context
parameter may be used, amongst other things, to access 'ancestor'
values.
However, if this rule only needs access to an immediate parent value then consider using
IRule<TValue, TParent> instead.
Exceptions
Type | Condition |
---|---|
System.Exception | This method may raise any exception type |
GetResultAsync(IReadOnlyCollection<T>, RuleContext, CancellationToken)
Performs the validation logic asynchronously and returns a task of RuleResult.
Declaration
public async ValueTask<RuleResult> GetResultAsync(IReadOnlyCollection<T> validated, RuleContext context, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IReadOnlyCollection<T> | validated | |
RuleContext | context | Contextual information about the validation |
System.Threading.CancellationToken | token | An object which may be used to cancel the process |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A task which provides a result object, indicating the result of validation |
Remarks
This method receives the value to be validated as well as an object which represents the context in which this rule is running. It should return a task of RuleResult.
In order to create the result object, particularly if your rule logic will run synchronously,
consider using the CommonResults class via using static CSF.Validation.Rules.CommonResults;
in your
rule logic.
The common results class has helper methods such as PassAsync(IDictionary<String, Object>)
and FailAsync(IDictionary<String, Object>)
which include optimisations for flyweight task instances that avoid allocating additional resources
needlessly.
It is acceptable to throw an uncaught exception from this method, as the validation framework will catch it and automatically convert it into an error result. Generally, developers do not need to manually return a result of outcome Errored manually. This would be appropriate only in an unusual scenario that is considered an error, but which does not involve the throwing of an exception. Error results are generally harder for the consumer to deal with than failure results.
The context
parameter may be used, amongst other things, to access 'ancestor'
values.
However, if this rule only needs access to an immediate parent value then consider using
IRule<TValue, TParent> instead.
Exceptions
Type | Condition |
---|---|
System.Exception | This method may raise any exception type |
GetResultAsync(IQueryable<T>, RuleContext, CancellationToken)
Performs the validation logic asynchronously and returns a task of RuleResult.
Declaration
public async ValueTask<RuleResult> GetResultAsync(IQueryable<T> validated, RuleContext context, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Linq.IQueryable<T> | validated | |
RuleContext | context | Contextual information about the validation |
System.Threading.CancellationToken | token | An object which may be used to cancel the process |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A task which provides a result object, indicating the result of validation |
Remarks
This method receives the value to be validated as well as an object which represents the context in which this rule is running. It should return a task of RuleResult.
In order to create the result object, particularly if your rule logic will run synchronously,
consider using the CommonResults class via using static CSF.Validation.Rules.CommonResults;
in your
rule logic.
The common results class has helper methods such as PassAsync(IDictionary<String, Object>)
and FailAsync(IDictionary<String, Object>)
which include optimisations for flyweight task instances that avoid allocating additional resources
needlessly.
It is acceptable to throw an uncaught exception from this method, as the validation framework will catch it and automatically convert it into an error result. Generally, developers do not need to manually return a result of outcome Errored manually. This would be appropriate only in an unusual scenario that is considered an error, but which does not involve the throwing of an exception. Error results are generally harder for the consumer to deal with than failure results.
The context
parameter may be used, amongst other things, to access 'ancestor'
values.
However, if this rule only needs access to an immediate parent value then consider using
IRule<TValue, TParent> instead.
Exceptions
Type | Condition |
---|---|
System.Exception | This method may raise any exception type |
Explicit Interface Implementations
| Improve this Doc View SourceIGetsFailureMessage<ICollection<T>>.GetFailureMessageAsync(ICollection<T>, ValidationRuleResult, CancellationToken)
Declaration
ValueTask<string> IGetsFailureMessage<ICollection<T>>.GetFailureMessageAsync(ICollection<T> value, ValidationRuleResult result, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.ICollection<T> | value | |
ValidationRuleResult | result | |
System.Threading.CancellationToken | token |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<System.String> |
IGetsFailureMessage<IReadOnlyCollection<T>>.GetFailureMessageAsync(IReadOnlyCollection<T>, ValidationRuleResult, CancellationToken)
Declaration
ValueTask<string> IGetsFailureMessage<IReadOnlyCollection<T>>.GetFailureMessageAsync(IReadOnlyCollection<T> value, ValidationRuleResult result, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IReadOnlyCollection<T> | value | |
ValidationRuleResult | result | |
System.Threading.CancellationToken | token |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<System.String> |
IGetsFailureMessage<IQueryable<T>>.GetFailureMessageAsync(IQueryable<T>, ValidationRuleResult, CancellationToken)
Declaration
ValueTask<string> IGetsFailureMessage<IQueryable<T>>.GetFailureMessageAsync(IQueryable<T> value, ValidationRuleResult result, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.IQueryable<T> | value | |
ValidationRuleResult | result | |
System.Threading.CancellationToken | token |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<System.String> |