public void Test_validate_with_valid_array_returns_true(string[] data) { // Arrange. // Act. var result = _arrayValidator.Validate(data); // Assert. result.IsValid.Should().BeTrue("the data provided should produce a true result."); }
/// <summary> /// Checks the data array to ensure it is valid for processing. /// </summary> /// <param name="data"> The collection of data rows. </param> /// <returns> If the data is valid or not. </returns> public static bool IsValid(this string[] data) { var validator = new StringArrayValidator(); var result = validator.Validate(data); return(result.IsValid); }
public override ValidationState Validate(PluginPropertiesInput pluginPropertiesInput) { if (pluginPropertiesInput == null) { throw new ArgumentNullException("pluginPropertiesInput"); } ValidationState validationState = new ValidationState(); foreach (string key in pluginPropertiesInput.PropertyDefinitions.Keys) { IDictionary <string, object> propertyDefinitions = pluginPropertiesInput.PropertyDefinitions[key]; Type propertyType = pluginPropertiesInput.Properties[key].Key; object propertyValue = pluginPropertiesInput.Properties[key].Value; //TODO: (erikpo) Need to move all the else/if into Visitor classes if (propertyValue != null && propertyValue is string) { propertyValue = ((string)propertyValue).Trim(); } if (propertyType == typeof(string)) { if (isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, () => propertyValue != null && (string)propertyValue != "")) { executePropertyValidator <StringValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } else if (propertyType == typeof(string[])) { StringArrayValidator validator = propertyDefinitions.GetPluginPropertyValidator <StringArrayValidator>(); if (isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, () => propertyValue != null && (propertyValue is string[] && ((string)propertyValue).Length > 0)) || (propertyValue is string && ((string)propertyValue).Split(new string[] { validator != null ? validator.Separator : "," }, StringSplitOptions.RemoveEmptyEntries).Length > 0)) { if (validator != null) { validationState.Errors.AddRange(validator.Validate(key, propertyType, propertyValue)); } } } else if (propertyType == typeof(bool) || propertyType == typeof(bool?)) { if (propertyType == typeof(bool) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { bool boolValue; if (propertyValue is string && !bool.TryParse((string)propertyValue, out boolValue)) { validationState.Errors.Add(createUnableToParseValidationError(key, "boolean", propertyValue)); } } } else if (propertyType == typeof(byte) || propertyType == typeof(byte?)) { if (propertyType == typeof(byte) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { byte byteValue; if (propertyValue is string && !byte.TryParse((string)propertyValue, out byteValue)) { validationState.Errors.Add(createUnableToParseValidationError(key, string.Format("number between {0} and {1}", byte.MinValue, byte.MaxValue), propertyValue)); } else { executePropertyValidator <NumberRangeValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } } else if (propertyType == typeof(short) || propertyType == typeof(short?)) { if (propertyType == typeof(short) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { short shortValue; if (propertyValue is string && !short.TryParse((string)propertyValue, out shortValue)) { validationState.Errors.Add(createUnableToParseValidationError(key, string.Format("number between {0} and {1}", short.MinValue, short.MaxValue), propertyValue)); } else { executePropertyValidator <NumberRangeValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } } else if (propertyType == typeof(int) || propertyType == typeof(int?)) { if (propertyType == typeof(int) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { int intValue; if (propertyValue is string && !int.TryParse((string)propertyValue, out intValue)) { validationState.Errors.Add(createUnableToParseValidationError(key, string.Format("number between {0} and {1}", int.MinValue, int.MaxValue), propertyValue)); } else { executePropertyValidator <NumberRangeValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } } else if (propertyType == typeof(long) || propertyType == typeof(long)) { if (propertyType == typeof(long) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { long longValue; if (propertyValue is string && !long.TryParse((string)propertyValue, out longValue)) { validationState.Errors.Add(createUnableToParseValidationError(key, string.Format("number between {0} and {1}", long.MinValue, long.MaxValue), propertyValue)); } else { executePropertyValidator <NumberRangeValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } } else if (propertyType == typeof(float) || propertyType == typeof(float?)) { if (propertyType == typeof(float) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { float floatValue; if (propertyValue is string && !float.TryParse((string)propertyValue, out floatValue)) { validationState.Errors.Add(createUnableToParseValidationError(key, "decimal", propertyValue)); } else { executePropertyValidator <NumberRangeValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } } else if (propertyType == typeof(double) || propertyType == typeof(double?)) { if (propertyType == typeof(double) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { double doubleValue; if (propertyValue is string && !double.TryParse((string)propertyValue, out doubleValue)) { validationState.Errors.Add(createUnableToParseValidationError(key, "decimal", propertyValue)); } else { executePropertyValidator <NumberRangeValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } } else if (propertyType == typeof(decimal) || propertyType == typeof(decimal?)) { if (propertyType == typeof(decimal) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { decimal decimalValue; if (propertyValue is string && !decimal.TryParse((string)propertyValue, out decimalValue)) { validationState.Errors.Add(createUnableToParseValidationError(key, "decimal", propertyValue)); } else { executePropertyValidator <NumberRangeValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } } else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?)) { if (propertyType == typeof(DateTime) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { DateTime dateTimeValue; if (propertyValue is string && !DateTime.TryParse((string)propertyValue, out dateTimeValue)) { validationState.Errors.Add(createUnableToParseValidationError(key, "date/time", propertyValue)); } else { executePropertyValidator <DateRangeValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } } else if (propertyType == typeof(Guid) || propertyType == typeof(Guid?)) { if (propertyType == typeof(Guid) || isValidFromRequiredCheck(key, propertyValue, validationState, propertyDefinitions, checkProperty(propertyValue))) { if (propertyValue is string) { try { new Guid((string)propertyValue); } catch { validationState.Errors.Add(createUnableToParseValidationError(key, "globally unique identifier (GUID)", propertyValue)); } } } } else { executePropertyValidator <IPluginPropertyValidator>(key, propertyType, propertyValue, propertyDefinitions, validationState); } } executeValidator <IPluginValidator>(pluginPropertiesInput, validationState); return(validationState); }