public bool IsValid(object instance, out IEnumerable<ValidationEngineMessage> results) { var validator = new RecursiveValidator(true, _container); var valid = validator.TryValidateObject(instance); results = valid ? new List<ValidationEngineMessage>() : _mapper.Map(validator.Results); return valid; }
public void Validate(object instance) { var validator = new RecursiveValidator(true, _container); var valid = validator.TryValidateObject(instance); if (!valid) { throw new ValidationEngineException( _mapper.Map(validator.Results)); } }
public void Validate_TypeWithCustomNestedTypeInvalid_Fails() { var instance = new TypeWithCustomNestedType { Property2 = new NestedType() }; var result = RecursiveValidator.Validate(instance); result.Should().BeOfType <FailedResult>(); var errorDetails = ((FailedResult)result).Details.ToArray(); errorDetails[0].Source.Should().Be($"{nameof(TypeWithCustomNestedType)}.{nameof(TypeWithCustomNestedType.Property1)}"); errorDetails[0].CodeInfo.Code.Should().BeEquivalentTo(AnnotationErrorCodes.Required.Code); errorDetails[1].Source.Should().Be($"{nameof(TypeWithCustomNestedType)}.{nameof(TypeWithCustomNestedType.Property2)}.{nameof(NestedType.NestedProperty)}"); errorDetails[1].CodeInfo.Code.Should().BeEquivalentTo(AnnotationErrorCodes.Required.Code); }
public void Validate_IsValid_DoesNotThrows() { var descriptor = new PathCellDescriptor(); descriptor.Figure = "Price"; descriptor.Path = "123"; descriptor.Column = new AbsolutePositionLocator { HeaderSeriesPosition = 0, SeriesPosition = 4 }; descriptor.Row = new AbsolutePositionLocator { HeaderSeriesPosition = 0, SeriesPosition = 23 }; descriptor.ValueFormat = new ValueFormat(typeof(double), "0.00"); RecursiveValidator.Validate(descriptor); }
public void Validate_MisingRow_Throws() { var descriptor = new PathCellDescriptor(); descriptor.Figure = "Price"; descriptor.Path = "123"; descriptor.Column = new AbsolutePositionLocator { HeaderSeriesPosition = 0, SeriesPosition = 4 }; descriptor.Row = null; descriptor.ValueFormat = new ValueFormat(typeof(double), "0.00"); var ex = Assert.Throws <ValidationException>(() => RecursiveValidator.Validate(descriptor)); Assert.That(ex.Message, Does.Contain("The Row field is required")); }
public void Validate_TypeWithArrayOfIntegersOverheadsRange_Fails() { var instance = new TypeWithArrayOfIntegers { IntegersArray = new[] { 11, 111 } }; var result = RecursiveValidator.Validate(instance); result.Should().BeOfType <FailedResult>() .Which.Code.Should().Be(CoreErrorCodes.ValidationFailed.Code); var errorDetails = ((FailedResult)result).Details.ToArray(); errorDetails.Length.Should().Be(1); errorDetails[0].Source.Should().Be($"{nameof(TypeWithArrayOfIntegers)}.{nameof(TypeWithArrayOfIntegers.IntegersArray)}.1"); errorDetails[0].CodeInfo.Code.Should().BeEquivalentTo(AnnotationErrorCodes.Range.Code); }
public void ValidatesObjectRecursively() { var root = new RootClass(); var validationContext = new ValidationContext(root); var validationResults = new List <ValidationResult>(); // Validate recursively Assert.False(RecursiveValidator.TryValidateObject(root, validationContext, validationResults)); Assert.Equal(6, validationResults.Count); Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == nameof(RootClass.SomeString)); Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.SingleChild)}.{nameof(ChildClass.SomeOtherString)}"); Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.SingleChild)}.{nameof(ChildClass.NestedItems)}[0].{nameof(ChildItem.SomeOtherOtherString)}"); Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.SingleChild)}.{nameof(ChildClass.NestedItems)}[1].{nameof(ChildItem.SomeOtherOtherString)}"); Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.Items)}[0].{nameof(ChildItem.SomeOtherOtherString)}"); Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.Items)}[1].{nameof(ChildItem.SomeOtherOtherString)}"); }
public void Validate_IsValid_DoesNotThrows() { var dataSource = new DataSource(); dataSource.Vendor = "vendor"; dataSource.Name = "name"; dataSource.DocumentType = DocumentType.Html; dataSource.Location = new DocumentLocator(new Request("http://test1.org")); var descriptor = new CsvDescriptor(); descriptor.Figure = "dummy.csv"; descriptor.Separator = ";"; descriptor.Columns.Add(new FormatColumn("c1", typeof(double), "0.00")); dataSource.Figures.Add(descriptor); RecursiveValidator.Validate(dataSource); }
public void Complete() { TextBoxBinding.ForceSourceUpdate(); var ctx = myProjectHost.Project.GetAssetsContext(); // validate currency consistancy foreach (FigureSeries series in myFigures.ToList()) { // we need to remove those items which have no value because those might // have "wrong" default currency foreach (var figure in series.OfType <AbstractFigure>().Where(d => !d.Value.HasValue).ToList()) { series.Remove(figure); if (figure.Id != 0) { // TODO: remove from stock/company relationship to remove it finally from DB //Dynamics.Remove( } } RecursiveValidator.Validate(series); series.EnableCurrencyCheck = true; series.VerifyCurrencyConsistency(); } foreach (FigureSeries series in myFigures.ToList()) { var figures = (IList)Dynamics.GetRelationship(Stock, series.FigureType); foreach (AbstractFigure figure in series) { if (figure.Id == 0 && figure.Value.HasValue) { figures.Add(figure); } } } ctx.SaveChanges(); }
public void Validate_TypeWithArrayOfCustomNestedTypes_SuccessTest() { var instance = new TypeWithArrayOfCustomNestedTypes { Property = "Some property", NestedTypeArray = new[] { new NestedType { NestedProperty = "Some value" }, new NestedType { NestedProperty = "Some another value" } // Should fail coz of regular expression verification. } }; var result = RecursiveValidator.Validate(instance); result.Success.Should().BeTrue(); }
public void Validate_TypeWithArrayOfCustomNestedTypesWithStringsArrayIsInvalid_Fails() { var instance = new TypeWithArrayOfCustomNestedTypes { Property = "Some property", NestedTypeArray = new[] { new NestedType { NestedProperty = "Some value" }, new NestedType { NestedProperty = "Some another value", StringsArray = new[] { "123", // String is too short. "some valid value", "я люблю українську мову" // String contsins not English letters. } }, } }; var result = RecursiveValidator.Validate(instance); result.Should().BeOfType <FailedResult>() .Which.Code.Should().Be(CoreErrorCodes.ValidationFailed.Code); var errorDetails = ((FailedResult)result).Details.ToArray(); errorDetails.Length.Should().Be(2); errorDetails[0].Source.Should().Be($"{nameof(TypeWithArrayOfCustomNestedTypes)}.{nameof(TypeWithArrayOfCustomNestedTypes.NestedTypeArray)}.1.{nameof(NestedType.StringsArray)}.0"); errorDetails[0].CodeInfo.Code.Should().BeEquivalentTo(AnnotationErrorCodes.MinLength.Code); errorDetails[1].Source.Should().Be($"{nameof(TypeWithArrayOfCustomNestedTypes)}.{nameof(TypeWithArrayOfCustomNestedTypes.NestedTypeArray)}.1.{nameof(NestedType.StringsArray)}.2"); errorDetails[1].CodeInfo.Code.Should().BeEquivalentTo(AnnotationErrorCodes.RegularExpression.Code); }
public void NestedCollectionFieldMissingRequiredValue() { BodyWithNestedRequiredUrlField body = new BodyWithNestedRequiredUrlField() { UrlParam = new BodyWithUrlField() { Param1 = "https://google.com" }, Items = new BodyWithRequiredFields[] { new BodyWithRequiredFields() { Param1 = null, Param2 = "blah" } } }; List <ValidationResult> validationResults = new List <ValidationResult>(); var result = RecursiveValidator.TryValidateObject(body, validationResults, true); result.ShouldBeFalse(); validationResults.ShouldContain(x => x.MemberNames.Contains("Items[0].Param1") && x.ErrorMessage.Contains("Param1 is required")); }
public void GivenInvalidNullablePrimitives_WhenValidateRecursively_ThenCorrectValidationResults() { RecursiveValidator validator = new RecursiveValidator(null); List <ValidationResult> results = validator.ValidateObjectRecursively(new NullablePrimitives()); Assert.Equal(17, results.Count); ValidationResultUtility.AssertValidationResultEquals(results[0], "The RequiredString field is required.", "RequiredString"); ValidationResultUtility.AssertValidationResultEquals(results[1], "The RequiredLong field is required.", "RequiredLong"); ValidationResultUtility.AssertValidationResultEquals(results[2], "The RequiredUnsignedLong field is required.", "RequiredUnsignedLong"); ValidationResultUtility.AssertValidationResultEquals(results[3], "The RequiredInt field is required.", "RequiredInt"); ValidationResultUtility.AssertValidationResultEquals(results[4], "The RequiredUnsignedInt field is required.", "RequiredUnsignedInt"); ValidationResultUtility.AssertValidationResultEquals(results[5], "The RequiredShort field is required.", "RequiredShort"); ValidationResultUtility.AssertValidationResultEquals(results[6], "The RequiredUnsignedShort field is required.", "RequiredUnsignedShort"); ValidationResultUtility.AssertValidationResultEquals(results[7], "The RequiredByte field is required.", "RequiredByte"); ValidationResultUtility.AssertValidationResultEquals(results[8], "The RequiredChar field is required.", "RequiredChar"); ValidationResultUtility.AssertValidationResultEquals(results[9], "The RequiredBool field is required.", "RequiredBool"); ValidationResultUtility.AssertValidationResultEquals(results[10], "The RequiredFloat field is required.", "RequiredFloat"); ValidationResultUtility.AssertValidationResultEquals(results[11], "The RequiredDouble field is required.", "RequiredDouble"); ValidationResultUtility.AssertValidationResultEquals(results[12], "The RequiredDecimal field is required.", "RequiredDecimal"); ValidationResultUtility.AssertValidationResultEquals(results[13], "The RequiredDateTime field is required.", "RequiredDateTime"); ValidationResultUtility.AssertValidationResultEquals(results[14], "The RequiredDateTimeOffset field is required.", "RequiredDateTimeOffset"); ValidationResultUtility.AssertValidationResultEquals(results[15], "The RequiredTimeSpan field is required.", "RequiredTimeSpan"); ValidationResultUtility.AssertValidationResultEquals(results[16], "The RequiredGuid field is required.", "RequiredGuid"); }
public void Validate_IsValid_DoesNotThrows() { var locator = new Dummy("http://www.me.com"); RecursiveValidator.Validate(locator); }
public void ThrowsArgumentNullException() { List <ValidationResult> validationResults = new List <ValidationResult>(); var exception = ShouldThrowExtensions.ShouldThrow <ArgumentNullException>(() => RecursiveValidator.TryValidateObject(null, validationResults, true)); exception.ParamName.ShouldBe("instance"); }
public void Validate_IsValid_DoesNotThrows() { var form = new Formular("dummy.f1"); RecursiveValidator.Validate(form); }
public void Validate_IsValid_DoesNotThrows() { var fragment = new Response("http://www.me.com"); RecursiveValidator.Validate(fragment); }
public void Validate_IsValid_DoesNotThrows() { var fragment = new SubmitFormular("http://test1.org", new Formular("dummy.form")); RecursiveValidator.Validate(fragment); }
public void Validate_IsValid_DoesNotThrows() { var col = new FormatColumn("c1", typeof(string)); RecursiveValidator.Validate(col); }
public static void doit(string targetPath, Func<string[], string, bool> validator, string description) { RecursiveValidator x = new RecursiveValidator(); x.doitWorker(targetPath, validator, description); WriteLine(); foreach (string s in x.badFiles) { WriteLine(s); } WriteLine(); WriteLine(x.goodCounter + " out of " + x.totalCounter + " pass the " + description + " test"); foreach (var xx in x.badFiles.Select(p => new { ext = p.Split('.').Reverse().First(), name = p }).GroupBy(p => p.ext)) { WriteLine(xx.Key); foreach (var yy in xx) { WriteLine(" " + yy.name); } } }
public bool IsValid(object instance) { var validator = new RecursiveValidator(true, _container); return validator.TryValidateObject(instance); }
/// <inheritdoc /> public IExecutionResult Validate() { return(RecursiveValidator.Validate(this)); }
public void Validate_NoErrors_ValidationSucceeds() { var validatableObject = new FakeValidatableObject(Array.Empty <ExecutionError>()); RecursiveValidator.Validate(validatableObject).Success.Should().BeTrue(); }
public void Validate_InstanceIsNull_Succeeds() { var result = RecursiveValidator.Validate(null); result.Should().BeOfType <SuccessfulResult>(); }