Class CommonResults
Helper class to create instances of RuleResult instances from commonly-used scenarios.
Inheritance
Inherited Members
Namespace: CSF.Validation.Rules
Assembly: CSF.Validation.Abstractions.dll
Syntax
public static class CommonResults
Remarks
For simplicity of usage consider including using static CSF.Validation.Rules.CommonResults;
in
your rule implementations.
This class also provides a small performance optimisation for results which do not return a data dictionary (or exception, in the case of error results). When any of these methods are used without data or exceptions then flyweight validation rule result instances will be used. See https://en.wikipedia.org/wiki/Flyweight_pattern for more information.
You may also use an overload of Result(Boolean, IDictionary<String, Object>) or ResultAsync(Boolean, IDictionary<String, Object>) if you wish to use a boolean to indicate pass or failure.
Methods
| Improve this Doc View SourceError(Exception, Dictionary<String, Object>)
Creates an instance of RuleResult for validation which has encountered an unexpected error.
Declaration
public static RuleResult Error(Exception exception, Dictionary<string, object> data)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | exception | The exception which caused the error. |
System.Collections.Generic.Dictionary<System.String, System.Object> | data | A key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
RuleResult | A RuleResult. |
Remarks
Most validation rules do not need to explicitly return error results. If a rule raises an uncaught exception whilst it is being executed then the validation framework will automatically create an error result for that rule, using the unhandled exception raised by the rule.
In normal operation the only times when a rule should explicitly return an error result is either when it also wants to return a dictionary of data, when there are reasons to catch/manipulate the exception before returning the error result or when the "error" does not involve an uncaught exception.
Error(Exception, IDictionary<String, Object>)
Creates an instance of RuleResult for validation which has encountered an unexpected error.
Declaration
public static RuleResult Error(Exception exception = null, IDictionary<string, object> data = null)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | exception | The exception which caused the error. |
System.Collections.Generic.IDictionary<System.String, System.Object> | data | An optional key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
RuleResult | A RuleResult. |
Remarks
Most validation rules do not need to explicitly return error results. If a rule raises an uncaught exception whilst it is being executed then the validation framework will automatically create an error result for that rule, using the unhandled exception raised by the rule.
In normal operation the only times when a rule should explicitly return an error result is either when it also wants to return a dictionary of data, when there are reasons to catch/manipulate the exception before returning the error result or when the "error" does not involve an uncaught exception.
ErrorAsync(Exception, Dictionary<String, Object>)
Creates an instance of RuleResult for validation which has encountered an unexpected error, returned within a completed task.
Declaration
public static ValueTask<RuleResult> ErrorAsync(Exception exception, Dictionary<string, object> data)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | exception | The exception which caused the error. |
System.Collections.Generic.Dictionary<System.String, System.Object> | data | A key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A completed task of RuleResult. |
Remarks
Most validation rules do not need to explicitly return error results. If a rule raises an uncaught exception whilst it is being executed then the validation framework will automatically create an error result for that rule, using the unhandled exception raised by the rule.
In normal operation the only times when a rule should explicitly return an error result is either when it also wants to return a dictionary of data, when there are reasons to catch/manipulate the exception before returning the error result or when the "error" does not involve an uncaught exception.
ErrorAsync(Exception, IDictionary<String, Object>)
Creates an instance of RuleResult for validation which has encountered an unexpected error, returned within a completed task.
Declaration
public static ValueTask<RuleResult> ErrorAsync(Exception exception = null, IDictionary<string, object> data = null)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | exception | The exception which caused the error. |
System.Collections.Generic.IDictionary<System.String, System.Object> | data | An optional key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A RuleResult. |
Remarks
Most validation rules do not need to explicitly return error results. If a rule raises an uncaught exception whilst it is being executed then the validation framework will automatically create an error result for that rule, using the unhandled exception raised by the rule.
In normal operation the only times when a rule should explicitly return an error result is either when it also wants to return a dictionary of data, when there are reasons to catch/manipulate the exception before returning the error result or when the "error" does not involve an uncaught exception.
Fail(Dictionary<String, Object>)
Creates an instance of RuleResult for failing validation.
Declaration
public static RuleResult Fail(Dictionary<string, object> data)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.Dictionary<System.String, System.Object> | data | A key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
RuleResult | A RuleResult. |
Fail(IDictionary<String, Object>)
Creates an instance of RuleResult for failing validation.
Declaration
public static RuleResult Fail(IDictionary<string, object> data = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<System.String, System.Object> | data | An optional key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
RuleResult | A RuleResult. |
FailAsync(Dictionary<String, Object>)
Creates an instance of RuleResult for failing validation, returned within a completed task.
Declaration
public static ValueTask<RuleResult> FailAsync(Dictionary<string, object> data)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.Dictionary<System.String, System.Object> | data | A key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A completed task of RuleResult. |
FailAsync(IDictionary<String, Object>)
Creates an instance of RuleResult for failing validation, returned within a completed task.
Declaration
public static ValueTask<RuleResult> FailAsync(IDictionary<string, object> data = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<System.String, System.Object> | data | An optional key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A RuleResult. |
Pass(Dictionary<String, Object>)
Creates an instance of RuleResult for passing validation.
Declaration
public static RuleResult Pass(Dictionary<string, object> data)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.Dictionary<System.String, System.Object> | data | A key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
RuleResult | A RuleResult. |
Pass(IDictionary<String, Object>)
Creates an instance of RuleResult for passing validation.
Declaration
public static RuleResult Pass(IDictionary<string, object> data = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<System.String, System.Object> | data | An optional key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
RuleResult | A RuleResult. |
PassAsync(Dictionary<String, Object>)
Creates an instance of RuleResult for passing validation, returned within a completed task.
Declaration
public static ValueTask<RuleResult> PassAsync(Dictionary<string, object> data)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.Dictionary<System.String, System.Object> | data | A key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A completed task of RuleResult. |
PassAsync(IDictionary<String, Object>)
Creates an instance of RuleResult for passing validation, returned within a completed task.
Declaration
public static ValueTask<RuleResult> PassAsync(IDictionary<string, object> data = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<System.String, System.Object> | data | An optional key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A completed task of RuleResult. |
Result(Boolean, Dictionary<String, Object>)
Creates an instance of RuleResult for a validation result, either a pass or a fail determined by a boolean parameter.
Declaration
public static RuleResult Result(bool pass, Dictionary<string, object> data)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | pass | A value which indicates whether or not the result was a pass. true indicates pass, false indicates failure. |
System.Collections.Generic.Dictionary<System.String, System.Object> | data | A key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
RuleResult | A RuleResult. |
Result(Boolean, IDictionary<String, Object>)
Creates an instance of RuleResult for a validation result, either a pass or a fail determined by a boolean parameter.
Declaration
public static RuleResult Result(bool pass, IDictionary<string, object> data = null)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | pass | A value which indicates whether or not the result was a pass. true indicates pass, false indicates failure. |
System.Collections.Generic.IDictionary<System.String, System.Object> | data | An optional key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
RuleResult | A RuleResult. |
ResultAsync(Boolean, Dictionary<String, Object>)
Creates an instance of RuleResult for a validation result, either a pass or a fail determined by a boolean parameter, returned within a completed task.
Declaration
public static ValueTask<RuleResult> ResultAsync(bool pass, Dictionary<string, object> data)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | pass | A value which indicates whether or not the result was a pass. true indicates pass, false indicates failure. |
System.Collections.Generic.Dictionary<System.String, System.Object> | data | A key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A completed task of RuleResult. |
ResultAsync(Boolean, IDictionary<String, Object>)
Creates an instance of RuleResult for a validation result, either a pass or a fail determined by a boolean parameter, returned within a completed task.
Declaration
public static ValueTask<RuleResult> ResultAsync(bool pass, IDictionary<string, object> data = null)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | pass | A value which indicates whether or not the result was a pass. true indicates pass, false indicates failure. |
System.Collections.Generic.IDictionary<System.String, System.Object> | data | An optional key/value collection of arbitrary validation data. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.ValueTask<RuleResult> | A completed task of RuleResult. |