Class FloatInRange
A validation rule which passes if the validated floating-point number or System.Nullable<T> floating-point number is within an inclusive Min & Max range. This rule works with all of the CLR-standard floating-point number types.
Inheritance
Implements
Inherited Members
Namespace: CSF.Validation.Rules
Assembly: CSF.Validation.StandardRules.dll
Syntax
public class FloatInRange : IRuleWithMessage<float>, IRule<float>, IGetsFailureMessage<float>, IRuleWithMessage<double>, IRule<double>, IGetsFailureMessage<double>, IRuleWithMessage<float?>, IRule<float?>, IGetsFailureMessage<float?>, IRuleWithMessage<double?>, IRule<double?>, IGetsFailureMessage<double?>
Remarks
Either of Min or Max may be null. If either is null then either the minimum or maximum of the range will not be used (which means that this becomes simply a "greater-then" or "less-than" validation rule). If both ends of the range are null then this validation rule will always pass.
This validation rule may additionally be used with System.Nullable<T> numbers, in which case this rule will pass if the validated value is null. Combine this rule with a NotNull rule if nulls are not permitted.
The logic of this rule does not verify that the minimum is not greater-than the maximum. Thus it is possible to set up scenarios where this rule will always return a failure result for any number, as the pass criteria cannot be satisfied.
This rule supports both non-nullable and nullable instances of System.Single & System.Double.
This rule will always return a synchronous result.
Properties
| Improve this Doc View SourceMax
Gets or sets the (inclusive) maximum for the validated value.
Declaration
public double? Max { get; set; }
Property Value
Type | Description |
---|---|
System.Nullable<System.Double> |
Min
Gets or sets the (inclusive) minimum for the validated value.
Declaration
public double? Min { get; set; }
Property Value
Type | Description |
---|---|
System.Nullable<System.Double> |
Methods
| Improve this Doc View SourceGetFailureMessageAsync(Nullable<Double>, ValidationRuleResult, CancellationToken)
Gets the validation failure message for the specified result.
Declaration
public ValueTask<string> GetFailureMessageAsync(double? value, ValidationRuleResult result, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Double> | value | |
ValidationRuleResult | result | A validation result, typically indicating failure. |
System.Threading.CancellationToken | token | An optional cancellation token. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<System.String> | A human-readable message. |
GetResultAsync(Double, RuleContext, CancellationToken)
Performs the validation logic asynchronously and returns a task of RuleResult.
Declaration
public ValueTask<RuleResult> GetResultAsync(double validated, RuleContext context, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Double | 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(Nullable<Double>, RuleContext, CancellationToken)
Performs the validation logic asynchronously and returns a task of RuleResult.
Declaration
public ValueTask<RuleResult> GetResultAsync(double? validated, RuleContext context, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Double> | 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<Double>.GetFailureMessageAsync(Double, ValidationRuleResult, CancellationToken)
Declaration
ValueTask<string> IGetsFailureMessage<double>.GetFailureMessageAsync(double value, ValidationRuleResult result, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
System.Double | value | |
ValidationRuleResult | result | |
System.Threading.CancellationToken | token |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<System.String> |
IGetsFailureMessage<Nullable<Single>>.GetFailureMessageAsync(Nullable<Single>, ValidationRuleResult, CancellationToken)
Declaration
ValueTask<string> IGetsFailureMessage<float?>.GetFailureMessageAsync(float? value, ValidationRuleResult result, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Single> | value | |
ValidationRuleResult | result | |
System.Threading.CancellationToken | token |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<System.String> |
IGetsFailureMessage<Single>.GetFailureMessageAsync(Single, ValidationRuleResult, CancellationToken)
Declaration
ValueTask<string> IGetsFailureMessage<float>.GetFailureMessageAsync(float value, ValidationRuleResult result, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
System.Single | value | |
ValidationRuleResult | result | |
System.Threading.CancellationToken | token |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<System.String> |
IRule<Nullable<Single>>.GetResultAsync(Nullable<Single>, RuleContext, CancellationToken)
Declaration
ValueTask<RuleResult> IRule<float?>.GetResultAsync(float? validated, RuleContext context, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Single> | validated | |
RuleContext | context | |
System.Threading.CancellationToken | token |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> |
IRule<Single>.GetResultAsync(Single, RuleContext, CancellationToken)
Declaration
ValueTask<RuleResult> IRule<float>.GetResultAsync(float validated, RuleContext context, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
System.Single | validated | |
RuleContext | context | |
System.Threading.CancellationToken | token |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> |