public void ConfigurationModelValidator_TryValidateObject__when__no_errors__then__returns_false() { ConfigurationModelValidator v = new ConfigurationModelValidator(); IEnumerable <ConfigurationModelValidationError> errors; bool isValid = v.TryValidateModel( new MyModel() { Max5Characters = "1234" }, out errors); Assert.IsTrue(isValid); Assert.IsNotNull(errors); Assert.AreEqual(0, errors.Count()); }
public void ConfigurationModelValidator_TryValidateObject__when__single_error__then__returns_false_and_well_formed_error() { ConfigurationModelValidator v = new ConfigurationModelValidator(); IEnumerable <ConfigurationModelValidationError> errors; bool isValid = v.TryValidateModel( new MyModel() { Max5Characters = "123456789" }, out errors); Assert.IsFalse(isValid); // ! Assert.IsNotNull(errors); Assert.AreEqual(1, errors.Count()); Assert.AreEqual("The field Max5Characters must be a string with a maximum length of 5.", errors.First().ErrorMessage); Assert.AreEqual(nameof(MyModel.Max5Characters), errors.First().MemberNames.First()); }
public void ConfigurationModelValidator_TryValidateObject__when__a_nested_object_is_invalid__then__returns_false_and_well_formed_error() { ConfigurationModelValidator v = new ConfigurationModelValidator(); IEnumerable <ConfigurationModelValidationError> errors; bool isValid = v.TryValidateModel( new MyModel() { Max5Characters = "12345", SingleLetter = "a", AnotherModel = new AnotherModel() { SingleLetter = "Oops" } }, out errors); Assert.IsFalse(isValid); // ! Assert.IsNotNull(errors); Assert.AreEqual(1, errors.Count()); Assert.AreEqual("The field AnotherModel references an object that failed validation.", errors.First().ErrorMessage); Assert.AreEqual(nameof(MyModel.AnotherModel), errors.First().MemberNames.First()); }
/// <summary> /// Initializes a new instance of the <see cref="ModelReader"/> class. /// </summary> /// <param name="modelValidator">The model validator.</param> public ModelReader(ConfigurationModelValidator modelValidator) { this.ModelValidator = modelValidator ?? new ConfigurationModelValidator(); }
/// <summary> /// Creates a new <see cref="DictionaryModelReader"/>. /// </summary> /// <param name="sourceValues">A dictionary or collection of keyed-values.</param> /// <param name="modelValidator">The model validator.</param> public DictionaryModelReader(IEnumerable <KeyValuePair <string, object> > sourceValues, ConfigurationModelValidator modelValidator) : base(modelValidator) { this.SourceValues = sourceValues ?? throw new ArgumentNullException(nameof(sourceValues)); if (!this.SourceValues.Any()) { throw new ArgumentException("The collection of source values is empty.", nameof(sourceValues)); } this.SourceValuesAsDictionary = ConvertToDictionary(this.SourceValues); }