/// <summary> /// Validates the supplied value in terms of the parameters constraints. /// </summary> /// <param name="value">Value to validate, may be null in which case no validation is applied.</param> /// <param name="isRequired">Set to true to check that this parameter is non-null.</param> /// <param name="enumPairs">We need to check that the value is found inside this collection</param> /// <returns>ValidationResult indicating whether the supplied value is valid.</returns> protected override ValidationResult ValidateValue(string value, bool isRequired, EnumPairCollection enumPairs) { if (!string.IsNullOrEmpty(value)) { if (MaxLength != null && value.Length > MaxLength) { var constraintText = string.Format(ErrorMessages.ConstraintMaxLengthExceeded, MaxLength); return(new ValidationResult(ValidationResult.ResultType.InvalidConstraint, constraintText, ErrorMessages.MaxLengthExceeded, value, MaxLength)); } if (MinLength != null && value.Length < MinLength) { var constraintText = string.Format(ErrorMessages.ConstraintMinLengthExceeded, MinLength); return(new ValidationResult(ValidationResult.ResultType.InvalidConstraint, constraintText, ErrorMessages.MinLengthExceeded, value, MinLength)); } } else if (isRequired) { return(new ValidationResult(ValidationResult.ResultType.Missing, ErrorMessages.NonOptionalParameterNotSupplied2)); } if (value != null && enumPairs.HasValues && !enumPairs.TryParseWireValue(value, out _)) { var constraintText = string.Format(ErrorMessages.UnrecognisedEnumIdValue, value); return(new ValidationResult(ValidationResult.ResultType.InvalidConstraint, constraintText, ErrorMessages.InvalidControlValueError)); } return(ValidationResult.ValidResult); }
/// <summary> /// Converts the value of this instance to an equivalent EnumState value. /// </summary> /// <returns>A valid EnumState, assuming the source value can be correctly converted.</returns> /// <remarks>This method converts the enum value to a string, looks up the EnumID from the supplied /// EnumPairCollection and then returns a new EnumState. This method may be a little slow for /// very large enumerations.</remarks> public EnumState ToEnumState(EnumPairCollection enumPairs) { EnumState state = new EnumState(enumPairs.EnumIds); string enumId; string wireValue = ToString(null); if (enumPairs.TryParseWireValue(wireValue, out enumId)) { state[enumId] = true; } return(state); }
/// <summary> /// Validates the supplied value in terms of the parameters constraints. This method does nothing because /// is not possible for a char value to be invalid. /// </summary> /// <param name="value">Value to validate, may be null in which case no validation is applied.</param> /// <param name="isRequired">Set to true to check that this parameter is non-null.</param> /// <param name="enumPairs">We need to check that the value is found inside this collection</param> /// <returns>ValidationResult indicating whether the supplied value is valid.</returns> protected override ValidationResult ValidateValue(char?value, bool isRequired, EnumPairCollection enumPairs) { if (isRequired && value == null) { return(new ValidationResult(ValidationResult.ResultType.Missing, ErrorMessages.NonOptionalParameterNotSupplied2)); } if (value != null && enumPairs.HasValues && !enumPairs.TryParseWireValue(value.ToString(), out _)) { var constraintText = string.Format(ErrorMessages.UnrecognisedEnumIdValue, value); return(new ValidationResult(ValidationResult.ResultType.InvalidConstraint, constraintText, ErrorMessages.InvalidControlValueError)); } return(ValidationResult.ValidResult); }
/// <summary> /// Creates a new EnumState from the supplied set of EnumPairs and input FIX string. /// </summary> /// <param name="enumPairs">EnumPairs for this parameter.</param> /// <param name="multiValueString">String containing one or more FIX wire values (space-separated).</param> /// <returns></returns> public static EnumState FromWireValue(EnumPairCollection enumPairs, string multiValueString) { _log.DebugFormat("Converting WireValue '{0}' to EnumState", multiValueString); string[] inputValues = multiValueString.Split(new char[] { ';', ' ', ',' }); EnumState result = new EnumState(enumPairs.EnumIds); foreach (string inputValue in inputValues) { string enumId; if (!enumPairs.TryParseWireValue(inputValue, out enumId)) { throw ThrowHelper.New <ArgumentException>(ExceptionContext, ErrorMessages.UnrecognisedEnumIdValue, inputValue); } result[enumId] = true; } _log.Debug(m => m("Converting EnumState from WireValue; state is {0}", result.ToString())); return(result); }