public void ValidatesValueProperty() { MockValidator<object> valueValidator = new MockValidator<object>(false); Validator validator = new PropertyValueValidator<PropertyValueValidatorFixtureTestClass>("PublicProperty", valueValidator); validator.Validate(new PropertyValueValidatorFixtureTestClass()); Assert.AreEqual(PropertyValueValidatorFixtureTestClass.value, valueValidator.ValidatedTargets[0]); }
public void ValidatesValueProperty() { MockValidator <object> valueValidator = new MockValidator <object>(false); Validator validator = new PropertyValueValidator <PropertyValueValidatorFixtureTestClass>("PublicProperty", valueValidator); validator.Validate(new PropertyValueValidatorFixtureTestClass()); Assert.AreEqual(PropertyValueValidatorFixtureTestClass.value, valueValidator.ValidatedTargets[0]); }
static void CreatingAndUsingValidatorsDirectly() { // Create a Contains Characters Validator and use it to validate a String value. Validator charsValidator = new ContainsCharactersValidator("cat", ContainsCharacters.All, "Value must contain {4} of the characters '{3}'."); Console.WriteLine("Validating a string value using a Contains Characters Validator..."); charsValidator.Tag = "Validating the String value 'disconnected'"; // This overload of the Validate method returns a new ValidationResults // instance populated with any/all of the validation errors. ValidationResults valResults = charsValidator.Validate("disconnected"); // Create a Domain Validator and use it to validate an Integer value. Validator integerValidator = new DomainValidator <int>("Value must be in the list 1, 3, 7, 11, 13.", new int[] { 1, 3, 7, 11, 13 }); integerValidator.Tag = "Validating the Integer value '42'"; Console.WriteLine("Validating an integer value using a Domain Validator..."); // This overload of the Validate method takes an existing ValidationResults // instance and adds any/all of the validation errors to it. integerValidator.Validate(42, valResults); // Create an Or Composite Validator containing two validators. // Note that the NotNullValidator is negated to allow NULL values. Validator[] valArray = new Validator[] { new NotNullValidator(true, "Value can be NULL."), new StringLengthValidator(5, RangeBoundaryType.Inclusive, 5, RangeBoundaryType.Inclusive, "Value must be between {3} ({4}) and {5} ({6}) chars.") }; Validator orValidator = new OrCompositeValidator("Value can be NULL or a string of 5 characters.", valArray); // Validate two strings using the Or Composite Validator. Console.WriteLine("Validating a NULL value using an Or Composite Validator..."); orValidator.Validate(null, valResults); // this will not cause a validation error Console.WriteLine("Validating a string value using an Or Composite Validator..."); orValidator.Validate("MoreThan5Chars", valResults); // Validate a single property of an existing class instance. // First create a Product instance with an invalid ID value. IProduct productWithID = new Product(); PopulateInvalidProduct(productWithID); // Create a Property Value Validator that will use a RegexValidator // to validate the property value. Validator propValidator = new PropertyValueValidator <Product>("ID", new RegexValidator("[A-Z]{2}[0-9]{4}", "Product ID must be 2 capital letters and 4 numbers.")); Console.WriteLine("Validating one property of an object using a Property Value Validator..."); propValidator.Validate(productWithID, valResults); // Now display the results of all the previous validation operations. ShowValidationResults(valResults); }
static void CreatingAndUsingValidatorsDirectly() { // Create a Contains Characters Validator and use it to validate a String value. Validator charsValidator = new ContainsCharactersValidator("cat", ContainsCharacters.All, "Value must contain {4} of the characters '{3}'."); Console.WriteLine("Validating a string value using a Contains Characters Validator..."); charsValidator.Tag = "Validating the String value 'disconnected'"; // This overload of the Validate method returns a new ValidationResults // instance populated with any/all of the validation errors. ValidationResults valResults = charsValidator.Validate("disconnected"); // Create a Domain Validator and use it to validate an Integer value. Validator integerValidator = new DomainValidator<int>("Value must be in the list 1, 3, 7, 11, 13.", new int[] { 1, 3, 7, 11, 13 }); integerValidator.Tag = "Validating the Integer value '42'"; Console.WriteLine("Validating an integer value using a Domain Validator..."); // This overload of the Validate method takes an existing ValidationResults // instance and adds any/all of the validation errors to it. integerValidator.Validate(42, valResults); // Create an Or Composite Validator containing two validators. // Note that the NotNullValidator is negated to allow NULL values. Validator[] valArray = new Validator[] { new NotNullValidator(true, "Value can be NULL."), new StringLengthValidator(5, RangeBoundaryType.Inclusive, 5, RangeBoundaryType.Inclusive, "Value must be between {3} ({4}) and {5} ({6}) chars.") }; Validator orValidator = new OrCompositeValidator("Value can be NULL or a string of 5 characters.", valArray); // Validate two strings using the Or Composite Validator. Console.WriteLine("Validating a NULL value using an Or Composite Validator..."); orValidator.Validate(null, valResults); // this will not cause a validation error Console.WriteLine("Validating a string value using an Or Composite Validator..."); orValidator.Validate("MoreThan5Chars", valResults); // Validate a single property of an existing class instance. // First create a Product instance with an invalid ID value. IProduct productWithID = new Product(); PopulateInvalidProduct(productWithID); // Create a Property Value Validator that will use a RegexValidator // to validate the property value. Validator propValidator = new PropertyValueValidator<Product>("ID", new RegexValidator("[A-Z]{2}[0-9]{4}", "Product ID must be 2 capital letters and 4 numbers.")); Console.WriteLine("Validating one property of an object using a Property Value Validator..."); propValidator.Validate(productWithID, valResults); // Now display the results of all the previous validation operations. ShowValidationResults(valResults); }
public PropertyValueValidator(string propertyName, Validator propertyValueValidator) : base(PropertyValueValidator <T> .GetPropertyValueAccess(propertyName), propertyValueValidator) { }