Show / Hide Table of Contents

Class MustBeDefinedEnumConstant

A validation rule which asserts that the validated value (which may be nullable) is a defined member of the specified enumerated type.

Inheritance
System.Object
MustBeDefinedEnumConstant
Implements
IRuleWithMessage<System.Object>
IRule<System.Object>
IGetsFailureMessage<System.Object>
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: CSF.Validation.Rules
Assembly: CSF.Validation.StandardRules.dll
Syntax
public class MustBeDefinedEnumConstant : IRuleWithMessage<object>, IRule<object>, IGetsFailureMessage<object>
Remarks

In order to use this validation rule, either of EnumType or EnumTypeName must be set to a not-null value. If EnumType is not null then this will be used as the type of the enum against which to validate the validated value, and EnumTypeName will not be used. If EnumType is null then the EnumTypeName must not be null and it must correspond to a type that could be loaded via System.Type.GetType(System.String).

This rule will raise an exception if both EnumType & EnumTypeName are null or if System.Type.GetType(System.String) raises any exception when EnumTypeName is used as its parameter, or if System.Type.GetType(System.String) returns a null result.

This validation rule is also compatible with nullable values of enum types, and will return a passing result if the validated value is null.

This rule can return Errored results if:

  • EnumType is null and EnumTypeName is not set to a value that yields a type when used with System.Type.GetType(System.String)
  • If the type found via either of EnumType or EnumTypeName is not an enum type.
  • If the validated value is an enumeration but of a different enumeration type to that which is found from either of EnumType or EnumTypeName.
  • If the validated value is a primitive which could be the underlying type for an enum, but it is not the same underlying type as the enumeration found from either of EnumType or EnumTypeName.

Properties

| Improve this Doc View Source

EnumType

Gets or sets a System.Type of an enum type for which to validate the validated value is a defined member.

Declaration
public Type EnumType { get; set; }
Property Value
Type Description
System.Type
Remarks

If this value is non-null then EnumTypeName will not be used.

| Improve this Doc View Source

EnumTypeName

Gets or sets a string which used to get a System.Type of an enum type for which to validate the validated value is a defined member.

Declaration
public string EnumTypeName { get; set; }
Property Value
Type Description
System.String
Remarks

This value is only used if EnumType is null. When using this mechanism of indicating the enum type, typically an Assembly Qualified Type Name will be required in order to locate the correct type.

Methods

| Improve this Doc View Source

GetFailureMessageAsync(Object, ValidationRuleResult, CancellationToken)

Gets the validation failure message for the specified result.

Declaration
public ValueTask<string> GetFailureMessageAsync(object value, ValidationRuleResult result, CancellationToken token = default(CancellationToken))
Parameters
Type Name Description
System.Object 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.

| Improve this Doc View Source

GetResultAsync(Object, RuleContext, CancellationToken)

Performs the validation logic asynchronously and returns a task of RuleResult.

Declaration
public ValueTask<RuleResult> GetResultAsync(object validated, RuleContext context, CancellationToken token = default(CancellationToken))
Parameters
Type Name Description
System.Object 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

Implements

IRuleWithMessage<TValidated>
IRule<TValidated>
IGetsFailureMessage<TValidated>
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX