void FindAndAddChildrenToOpenList(ValidatedValueBasis currentBasis, ValidatedValue currentValue, Queue <ValidatedValueBasis> openList, ResolvedValidationOptions options) { if (!currentValue.ValueResponse.IsSuccess) { return; } var actualValue = currentValue.GetActualValue(); if (!(currentBasis.ManifestValue.CollectionItemValue is null || actualValue is null)) { openList.Enqueue(new ValidatedValueBasis(currentBasis.ManifestValue.CollectionItemValue, currentValue.ValueResponse, currentValue)); } foreach (var childManifestValue in currentBasis.GetChildManifestValues()) { var valueResponse = valueProvider.GetValueToBeValidated(childManifestValue, actualValue, options); openList.Enqueue(new ValidatedValueBasis(childManifestValue, valueResponse, currentValue)); } }
/// <summary> /// Gets a flattened collection of executable validation rules from a manifest value and object to be validated. /// </summary> /// <param name="manifestValue">The manifest value.</param> /// <param name="objectToBeValidated">The object to be validated.</param> /// <param name="options">The validation options.</param> /// <returns>A flattened collection of executable rules from the manifest value and the value's descendents.</returns> public IReadOnlyList <ExecutableRule> GetExecutableRules(ManifestValue manifestValue, object objectToBeValidated, ResolvedValidationOptions options) { var validatedValue = validatedValueProvider.GetValidatedValue(manifestValue, objectToBeValidated, options); return(GetFlattenedExecutableRules(validatedValue).ToList()); }
IExecutesAllRules GetPrimaryRuleExecutor(ResolvedValidationOptions options) { var singleRuleExecutor = GetSingleRuleExecutor(options); return(options.EnableRuleParallelization ? (IExecutesAllRules) new ParallelRuleExecutor(singleRuleExecutor) : (IExecutesAllRules) new SerialRuleExecutor(singleRuleExecutor)); }
/// <summary> /// Gets the rule-execution service using an async API. /// </summary> /// <param name="options">The validation options.</param> /// <param name="token">An optional cancellation token.</param> /// <returns>A task which contains the rule-execution service implementation.</returns> public Task <IExecutesAllRules> GetRuleExecutorAsync(ResolvedValidationOptions options, CancellationToken token = default) { var result = GetPrimaryRuleExecutor(options); result = WrapWithFailedDependenciesDecorator(result); result = WrapWithErroredValuesDecorator(result); return(Task.FromResult(result)); }
public void GetBehaviourShouldReturnManifestBehaviourIfItIsNotNull(ValueAccessExceptionBehaviourProvider sut, [ManifestModel] ManifestValue manifestValue, ResolvedValidationOptions validationOptions, ValueAccessExceptionBehaviour valueBehaviour, ValueAccessExceptionBehaviour optionsBehaviour) { manifestValue.AccessorExceptionBehaviour = valueBehaviour; validationOptions.AccessorExceptionBehaviour = optionsBehaviour; Assert.That(() => sut.GetBehaviour(manifestValue, validationOptions), Is.EqualTo(valueBehaviour)); }
public void GetValueToBeValidatedShouldExposeTheCorrectValueWhenItIsReadable(ValueToBeValidatedProvider sut, [ManifestModel] ManifestValue manifestValue, object parentValue, ResolvedValidationOptions validationOptions, object expected) { manifestValue.AccessorFromParent = obj => expected; Assert.That(() => sut.GetValueToBeValidated(manifestValue, parentValue, validationOptions), Has.Property(nameof(SuccessfulGetValueToBeValidatedResponse.Value)).EqualTo(expected)); }
/// <summary> /// Gets the validated value from the specified manifest value and object to be validated. /// </summary> /// <param name="manifestValue">The manifest value.</param> /// <param name="objectToBeValidated">The object to be validated.</param> /// <param name="options">The validation options.</param> /// <returns>A validated value, including a hierarchy of descendent values and /// the rules which may be executed upon those values.</returns> public ValidatedValue GetValidatedValue(ManifestValue manifestValue, object objectToBeValidated, ResolvedValidationOptions options) { if (manifestValue is null) { throw new ArgumentNullException(nameof(manifestValue)); } if (options is null) { throw new ArgumentNullException(nameof(options)); } var openList = new Queue <ValidatedValueBasis>(new [] { new ValidatedValueBasis(manifestValue, new SuccessfulGetValueToBeValidatedResponse(objectToBeValidated), null) }); ValidatedValue rootValidatedValue = null; while (openList.Any()) { var currentBasis = openList.Dequeue(); if (currentBasis.IsCircularReference()) { continue; } if (currentBasis.ValidatedValueResponse is IgnoredGetValueToBeValidatedResponse) { continue; } var currentValues = GetValidatedValues(currentBasis); if (rootValidatedValue is null && currentValues.Any()) { rootValidatedValue = currentValues.First(); } foreach (var value in currentValues) { FindAndAddChildrenToOpenList(currentBasis, value, openList, options); } } return(rootValidatedValue); }
public void GetValueToBeValidatedShouldReturnSuccessResponseIfTheValueIsReadable(ValueToBeValidatedProvider sut, [ManifestModel] ManifestValue manifestValue, object parentValue, ResolvedValidationOptions validationOptions, object value) { manifestValue.AccessorFromParent = obj => value; Assert.That(() => sut.GetValueToBeValidated(manifestValue, parentValue, validationOptions), Is.InstanceOf <SuccessfulGetValueToBeValidatedResponse>()); }
public void GetRulesWithDependenciesShouldReturnAnObjectWithDependencyExecutableRulesWhereItHasADependencyUponAParentValue([Frozen] IGetsAllExecutableRules executableRulesProvider, [ManifestModel] ManifestValue manifestValue, object objectToBeValidated, IValidationLogic logic, ManifestRule manifestRule, ManifestRule manifestDependency, [ExecutableModel] ValidatedValue validatedValue, [ExecutableModel] ValidatedValue parentValue, ExecutableRulesAndDependenciesProvider sut, ResolvedValidationOptions validationOptions) { var rule = new ExecutableRule { ValidatedValue = validatedValue, ManifestRule = manifestRule, RuleLogic = logic }; validatedValue.ParentValue = parentValue; var dependency = new ExecutableRule { ValidatedValue = parentValue, ManifestRule = manifestDependency, RuleLogic = logic }; parentValue.ManifestValue = manifestDependency.Identifier.ManifestValue; parentValue.Rules.Add(dependency); Mock.Get(executableRulesProvider) .Setup(x => x.GetExecutableRules(manifestValue, objectToBeValidated, validationOptions)) .Returns(new [] { rule, dependency }); rule.ManifestRule.DependencyRules.Clear(); rule.ManifestRule.DependencyRules.Add(manifestDependency.Identifier); var result = sut.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions); Assert.That(result.First(x => x.ExecutableRule == rule).Dependencies, Is.EqualTo(new[] { dependency })); }
/// <summary> /// Gets a collection of the executable rules and their dependencies from the specified /// manifest value and an object to be validated. /// </summary> /// <param name="manifestValue">The manifest value.</param> /// <param name="objectToBeValidated">The object to be validated.</param> /// <param name="validationOptions">The validation options.</param> /// <returns>A collection of all of the executable rules and the dependencies for each rule.</returns> public IReadOnlyList <ExecutableRuleAndDependencies> GetRulesWithDependencies(ManifestValue manifestValue, object objectToBeValidated, ResolvedValidationOptions validationOptions) { var result = wrapped.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions); AssertNoCircularDependencies(result); return(result); }
public void GetRuleExecutorAsyncShouldReturnANonNullRuleExecutorIfParallelisationIsEnabled([Frozen] IServiceProvider resolver, RuleExecutorFactory sut, ResolvedValidationOptions options, IGetsRuleExecutionContext dependencyTrackerFactory, IGetsSingleRuleExecutor ruleExecutorFactory, IGetsRuleContext contextFactory) { Mock.Get(resolver) .Setup(x => x.GetService(typeof(IGetsRuleExecutionContext))) .Returns(dependencyTrackerFactory); Mock.Get(resolver) .Setup(x => x.GetService(typeof(IGetsSingleRuleExecutor))) .Returns(ruleExecutorFactory); Mock.Get(resolver) .Setup(x => x.GetService(typeof(IGetsRuleContext))) .Returns(contextFactory); options.EnableRuleParallelization = true; Assert.That(async() => await sut.GetRuleExecutorAsync(options), Is.Not.Null); }
public void GetRulesWithDependenciesShouldNotThrowIfThereAreNoCircularDependencies([Frozen] IGetsAllExecutableRulesWithDependencies wrapped, [Frozen] IDetectsCircularDependencies circularDependencyDetector, CircularDependencyPreventingRulesWithDependenciesDecorator sut, [ManifestModel] ManifestValue manifestValue, object objectToBeValidated, ResolvedValidationOptions validationOptions) { Mock.Get(wrapped) .Setup(x => x.GetRulesWithDependencies(It.IsAny <ManifestValue>(), It.IsAny <object>(), validationOptions)) .Returns(new ExecutableRuleAndDependencies[0]); Mock.Get(circularDependencyDetector) .Setup(x => x.GetCircularDependencies(It.IsAny <IEnumerable <ExecutableRuleAndDependencies> >())) .Returns(Enumerable.Empty <CircularDependency>()); Assert.That(() => sut.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions), Throws.Nothing); }
public void GetValueToBeValidatedShouldReturnErrorResultIfAccessorThrowsAndExceptionBehaviourIsError([Frozen] IGetsAccessorExceptionBehaviour behaviourProvider, ValueToBeValidatedProvider sut, [ManifestModel] ManifestValue manifestValue, object parentValue, ResolvedValidationOptions validationOptions, Exception exception) { Mock.Get(behaviourProvider).Setup(x => x.GetBehaviour(manifestValue, validationOptions)).Returns(ValueAccessExceptionBehaviour.TreatAsError); manifestValue.AccessorFromParent = obj => throw exception; Assert.That(() => sut.GetValueToBeValidated(manifestValue, parentValue, validationOptions), Is.InstanceOf <ErrorGetValueToBeValidatedResponse>()); }
public void GetValidatedValueShouldCreateCollectionValueFromFactoryIfManifestHasACollectionItemWithinAChildItem([Frozen] IGetsValidatedValueFromBasis valueFromBasisFactory, [Frozen] IGetsEnumerableItemsToBeValidated enumerableProvider, ValidatedValueFactory sut, IEnumerable <object> validatedValue, ResolvedValidationOptions validationOptions, [ExecutableModel] ValidatedValue value, [ExecutableModel] ValidatedValue childValue, [ExecutableModel] ValidatedValue collectionValue, object item) { var manifestValue = new ManifestValue { ValidatedType = typeof(object), Children = new[] { new ManifestValue { ValidatedType = typeof(IEnumerable <object>), CollectionItemValue = new ManifestCollectionItem { ValidatedType = typeof(object) }, } } }; Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue.Children.First()))) .Returns(childValue); Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue))) .Returns(value); value.ManifestValue = manifestValue; childValue.ManifestValue = manifestValue.Children.First(); collectionValue.ManifestValue = manifestValue.Children.First().CollectionItemValue; childValue.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue); Mock.Get(enumerableProvider) .Setup(x => x.GetEnumerableItems(validatedValue, manifestValue.Children.First().CollectionItemValue.ValidatedType)) .Returns(new[] { item }); Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue.Children.First().CollectionItemValue&& b.GetActualValue() == item))) .Returns(collectionValue); var result = sut.GetValidatedValue(manifestValue, validatedValue, validationOptions); Assert.Multiple(() => { Assert.That(result.CollectionItems, Is.Empty, "Root value has no collection items"); Assert.That(result.ChildValues, Has.Count.EqualTo(1), "Root value has one child value"); Assert.That(result.ChildValues.FirstOrDefault()?.CollectionItems, Has.Count.EqualTo(1), "Root value has one collection item"); }); }
public void GetValidatedValueShouldCreateCollectionValueFromFactoryIfManifestHasACollectionItem([Frozen] IGetsValidatedValueFromBasis valueFromBasisFactory, [Frozen] IGetsEnumerableItemsToBeValidated enumerableProvider, ValidatedValueFactory sut, IEnumerable <object> validatedValue, ResolvedValidationOptions validationOptions, [ExecutableModel] ValidatedValue value, [ExecutableModel] ValidatedValue collectionValue, object item) { var manifestValue = new ManifestValue { ValidatedType = typeof(IEnumerable <object>), CollectionItemValue = new ManifestCollectionItem { ValidatedType = typeof(object) }, }; Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue))) .Returns(value); value.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue); Mock.Get(enumerableProvider) .Setup(x => x.GetEnumerableItems(validatedValue, manifestValue.CollectionItemValue.ValidatedType)) .Returns(new[] { item }); Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue.CollectionItemValue && b.GetActualValue() == item))) .Returns(collectionValue); sut.GetValidatedValue(manifestValue, validatedValue, validationOptions); Mock.Get(valueFromBasisFactory) .Verify(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue.CollectionItemValue)), Times.Once); }
public void GetValidatedValueShouldBeAbleToProcessACollectionWithinACollection([Frozen] IGetsValidatedValueFromBasis valueFromBasisFactory, [Frozen] IGetsValueToBeValidated valueProvider, [Frozen] IGetsEnumerableItemsToBeValidated enumerableProvider, ValidatedValueFactory sut, [NoAutoProperties] ComplexObject validatedValue, ResolvedValidationOptions validationOptions, [ExecutableModel] ValidatedValue val, [ExecutableModel] ValidatedValue firstCollection, [ExecutableModel] ValidatedValue secondCollection, [ExecutableModel] ValidatedValue item) { var manifestValue = new ManifestValue { ValidatedType = typeof(ComplexObject), Children = new [] { new ManifestValue { ValidatedType = typeof(ICollection <List <ComplexObject> >), MemberName = nameof(ComplexObject.DoubleCollection), AccessorFromParent = obj => ((ComplexObject)obj).DoubleCollection, CollectionItemValue = new ManifestCollectionItem { ValidatedType = typeof(List <ComplexObject>), CollectionItemValue = new ManifestCollectionItem { ValidatedType = typeof(ComplexObject), } }, }, }, }; var child = (ManifestValue)manifestValue.Children.Single(); child.Parent = manifestValue; child.CollectionItemValue.Parent = manifestValue; child.CollectionItemValue.CollectionItemValue.Parent = manifestValue; validatedValue.DoubleCollection = new List <List <ComplexObject> > { new List <ComplexObject> { new ComplexObject() } }; Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue))) .Returns(val); val.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue); Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == child))) .Returns(firstCollection); firstCollection.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue.DoubleCollection); Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == child.CollectionItemValue))) .Returns(secondCollection); secondCollection.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue.DoubleCollection.First()); Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == child.CollectionItemValue.CollectionItemValue))) .Returns(item); item.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue.DoubleCollection.First().First()); object validatedVal = validatedValue; Mock.Get(valueProvider) .Setup(x => x.GetValueToBeValidated(manifestValue, validatedValue, validationOptions)) .Returns(new SuccessfulGetValueToBeValidatedResponse(validatedValue)); object doubleCollection = firstCollection.ValueResponse; Mock.Get(valueProvider) .Setup(x => x.GetValueToBeValidated(child, validatedValue, validationOptions)) .Returns(new SuccessfulGetValueToBeValidatedResponse(doubleCollection)); Mock.Get(enumerableProvider) .Setup(x => x.GetEnumerableItems(validatedValue.DoubleCollection, child.CollectionItemValue.ValidatedType)) .Returns(validatedValue.DoubleCollection); Mock.Get(enumerableProvider) .Setup(x => x.GetEnumerableItems(validatedValue.DoubleCollection.First(), child.CollectionItemValue.CollectionItemValue.ValidatedType)) .Returns(validatedValue.DoubleCollection.First()); var result = sut.GetValidatedValue(manifestValue, validatedValue, validationOptions); Mock.Get(valueFromBasisFactory) .Verify(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == child.CollectionItemValue.CollectionItemValue)), Times.Once); }
public void GetValidatedValueShouldBeAbleToProcessAGrandchildManifestValue([Frozen] IGetsValidatedValueFromBasis valueFromBasisFactory, [Frozen] IGetsValueToBeValidated valueProvider, ValidatedValueFactory sut, [NoAutoProperties] ComplexObject validatedValue, [NoAutoProperties] ComplexObject childValue, string grandchildValue, ResolvedValidationOptions validationOptions, [ExecutableModel] ValidatedValue val, [ExecutableModel] ValidatedValue childVal, [ExecutableModel] ValidatedValue grandchildVal) { var manifestValue = new ManifestValue { ValidatedType = typeof(ComplexObject), }; var childManifest = new ManifestValue { Parent = manifestValue, ValidatedType = typeof(ComplexObject), }; var grandchildManifest = new ManifestValue { Parent = childManifest, ValidatedType = typeof(string), }; manifestValue.Children.Add(childManifest); childManifest.Children.Add(grandchildManifest); Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue))) .Returns(val); val.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue); val.ManifestValue = manifestValue; Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == childManifest))) .Returns(childVal); childVal.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(childValue); childVal.ManifestValue = childManifest;; Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == grandchildManifest))) .Returns(grandchildVal); grandchildVal.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(grandchildValue); grandchildVal.ManifestValue = grandchildManifest; object child = childValue; Mock.Get(valueProvider) .Setup(x => x.GetValueToBeValidated(childManifest, validatedValue, validationOptions)) .Returns(new SuccessfulGetValueToBeValidatedResponse(child)); object grandchild = grandchildValue; Mock.Get(valueProvider) .Setup(x => x.GetValueToBeValidated(grandchildManifest, childValue, validationOptions)) .Returns(new SuccessfulGetValueToBeValidatedResponse(grandchild)); var result = sut.GetValidatedValue(manifestValue, validatedValue, validationOptions); Mock.Get(valueFromBasisFactory) .Verify(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == childManifest && b.GetActualValue() == child)), Times.Once); Mock.Get(valueFromBasisFactory) .Verify(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == grandchildManifest && b.GetActualValue() == grandchild)), Times.Once); }
public void GetValidatedValueShouldNotProcessAChildManifestValueIfTheActualValueCannotBeRetrieved([Frozen] IGetsValidatedValueFromBasis valueFromBasisFactory, [Frozen] IGetsValueToBeValidated valueProvider, ValidatedValueFactory sut, [NoAutoProperties] ComplexObject validatedValue, string childValue, ResolvedValidationOptions validationOptions, [ExecutableModel] ValidatedValue val, [ExecutableModel] ValidatedValue childVal) { var manifestValue = new ManifestValue { ValidatedType = typeof(ComplexObject), }; var childManifest = new ManifestValue { Parent = manifestValue, ValidatedType = typeof(string), }; manifestValue.Children.Add(childManifest); Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue))) .Returns(val); Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == childManifest))) .Returns(childVal); object child = childValue; Mock.Get(valueProvider) .Setup(x => x.GetValueToBeValidated(childManifest, validatedValue, validationOptions)) .Returns(new IgnoredGetValueToBeValidatedResponse()); var result = sut.GetValidatedValue(manifestValue, validatedValue, validationOptions); Mock.Get(valueFromBasisFactory) .Verify(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == childManifest && b.ValidatedValueResponse == child)), Times.Never); }
public void GetValidatedValueShouldReturnSingleValueForManifestValueWithNoParentOrChildrenOrRules([Frozen] IGetsValidatedValueFromBasis valueFromBasisFactory, ValidatedValueFactory sut, object validatedValue, ResolvedValidationOptions validationOptions, [ExecutableModel] ValidatedValue value) { var manifestValue = new ManifestValue { ValidatedType = typeof(object) }; Mock.Get(valueFromBasisFactory) .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue))) .Returns(value); var result = sut.GetValidatedValue(manifestValue, validatedValue, validationOptions); Assert.That(result, Is.SameAs(value)); }
public void GetValueToBeValidatedShouldReturnIgnoredResultIfTheParentValueIsNull(ValueToBeValidatedProvider sut, [ManifestModel] ManifestValue manifestValue, ResolvedValidationOptions validationOptions) { Assert.That(() => sut.GetValueToBeValidated(manifestValue, null, validationOptions), Is.InstanceOf <IgnoredGetValueToBeValidatedResponse>()); }
public void GetValueToBeValidatedShouldThrowIfTheAccessorThrowsAndExceptionBehaviourIsThrow([Frozen] IGetsAccessorExceptionBehaviour behaviourProvider, ValueToBeValidatedProvider sut, [ManifestModel] ManifestValue manifestValue, object parentValue, ResolvedValidationOptions validationOptions, Exception exception) { Mock.Get(behaviourProvider).Setup(x => x.GetBehaviour(manifestValue, validationOptions)).Returns(ValueAccessExceptionBehaviour.Throw); manifestValue.AccessorFromParent = obj => throw exception; Assert.That(() => sut.GetValueToBeValidated(manifestValue, parentValue, validationOptions), Throws.InstanceOf <ValidationException>().And.InnerException.SameAs(exception)); }
IExeucutesSingleRule GetSingleRuleExecutor(ResolvedValidationOptions options) => resolver.GetRequiredService <IGetsSingleRuleExecutor>().GetRuleExecutor(options);
/// <summary> /// Gets a collection of the executable rules and their dependencies from the specified /// manifest value and an object to be validated. /// </summary> /// <param name="manifestValue">The manifest value.</param> /// <param name="objectToBeValidated">The object to be validated.</param> /// <param name="validationOptions">The validation options.</param> /// <returns>A collection of all of the executable rules and the dependencies for each rule.</returns> public IReadOnlyList <ExecutableRuleAndDependencies> GetRulesWithDependencies(ManifestValue manifestValue, object objectToBeValidated, ResolvedValidationOptions validationOptions) { var executableRules = executableRulesProvider.GetExecutableRules(manifestValue, objectToBeValidated, validationOptions); return(GetRulesWithDependencies(executableRules).ToList()); }
/// <inheritdoc/> public ValueAccessExceptionBehaviour GetBehaviour(IManifestValue manifestValue, ResolvedValidationOptions validationOptions) { return(manifestValue.AccessorExceptionBehaviour.HasValue ? manifestValue.AccessorExceptionBehaviour.Value : validationOptions.AccessorExceptionBehaviour); }
public void GetRulesWithDependenciesShouldThrowValidationExceptionWithCorrectMessageIfThereAreCircularDependencies([Frozen] IGetsAllExecutableRulesWithDependencies wrapped, [Frozen] IDetectsCircularDependencies circularDependencyDetector, CircularDependencyPreventingRulesWithDependenciesDecorator sut, [ManifestModel] ManifestValue manifestValue, object objectToBeValidated, ResolvedValidationOptions validationOptions) { var circularDependencies = GetSomeCircularDependencies(manifestValue); var expectedMessage = @"Validation rules may not have circular dependencies. Following is a list of the circular dependencies found, to a maximum of the first 10. Type = System.String Name = Foo Validated type = System.Int32 Validated identity = Identity 1 -> Type = System.DateTime Validated type = System.Int64 -> Type = System.String Name = Foo Validated type = System.Int32 Validated identity = Identity 1 Type = System.String Name = Bar Validated type = System.Int32 Validated identity = Identity 2 -> Type = System.Object Validated type = System.Int64 -> Type = System.String Name = Bar Validated type = System.Int32 Validated identity = Identity 2 "; Mock.Get(wrapped) .Setup(x => x.GetRulesWithDependencies(It.IsAny <ManifestValue>(), It.IsAny <object>(), validationOptions)) .Returns(new ExecutableRuleAndDependencies[0]); Mock.Get(circularDependencyDetector) .Setup(x => x.GetCircularDependencies(It.IsAny <IEnumerable <ExecutableRuleAndDependencies> >())) .Returns(circularDependencies); Assert.That(() => sut.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions), Throws.InstanceOf <ValidationException>().And.Message.EqualTo(expectedMessage)); }
public void GetRulesWithDependenciesShouldThrowIfTheMatchingValueDoesNotHaveTheSpecifiedRule([Frozen] IGetsAllExecutableRules executableRulesProvider, [ManifestModel] ManifestValue manifestValue, object objectToBeValidated, IValidationLogic logic, ManifestRule manifestRule, ManifestRule manifestDependency, [ExecutableModel] ValidatedValue validatedValue, [ExecutableModel] ValidatedValue parentValue, ExecutableRulesAndDependenciesProvider sut, ResolvedValidationOptions validationOptions) { var rule = new ExecutableRule { ValidatedValue = validatedValue, ManifestRule = manifestRule, RuleLogic = logic }; validatedValue.ParentValue = parentValue; var dependency = new ExecutableRule { ValidatedValue = parentValue, ManifestRule = manifestDependency, RuleLogic = logic }; parentValue.ManifestValue = manifestDependency.Identifier.ManifestValue; Mock.Get(executableRulesProvider) .Setup(x => x.GetExecutableRules(manifestValue, objectToBeValidated, validationOptions)) .Returns(new [] { rule, dependency }); rule.ManifestRule.DependencyRules.Clear(); rule.ManifestRule.DependencyRules.Add(manifestDependency.Identifier); Assert.That(() => sut.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions), Throws.InstanceOf <ValidationException>()); }
public void GetExecutableRulesShouldReturnAFlattenedListOfRulesIncludingCollectionItemRules([Frozen] IGetsValidatedValue validatedValueProvider, [ExecutableModel] ValidatedValue rootValue, [ExecutableModel] ValidatedValue childVal1, [ExecutableModel] ValidatedValue childVal2, [ExecutableModel] ValidatedValue collectionValue1, [ExecutableModel] ValidatedValue collectionValue2, [ExecutableModel] ExecutableRule rule1, [ExecutableModel] ExecutableRule rule2, [ExecutableModel] ExecutableRule rule3, [ExecutableModel] ExecutableRule rule4, [ExecutableModel] ExecutableRule rule5, [ExecutableModel] ExecutableRule rule6, ExecutableRulesFromValidatedValueProvider sut, [ManifestModel] ManifestValue manifestValue, object valueToBeValidated, ResolvedValidationOptions validationOptions) { rootValue.ChildValues.Add(childVal1); rootValue.ChildValues.Add(childVal2); childVal2.CollectionItems.Add(collectionValue1); childVal2.CollectionItems.Add(collectionValue2); rootValue.Rules.Add(rule1); childVal1.Rules.Add(rule2); childVal1.Rules.Add(rule3); childVal2.Rules.Add(rule4); collectionValue1.Rules.Add(rule5); collectionValue2.Rules.Add(rule6); Mock.Get(validatedValueProvider).Setup(x => x.GetValidatedValue(manifestValue, valueToBeValidated, validationOptions)).Returns(rootValue); var expected = new[] { rule1, rule2, rule3, rule4, rule5, rule6 }; Assert.That(() => sut.GetExecutableRules(manifestValue, valueToBeValidated, validationOptions), Is.EquivalentTo(expected)); }
/// <summary> /// Gets the service which may be used for executing validation rules. /// </summary> /// <param name="options">The validation options.</param> /// <returns>A single-rule execution service instance.</returns> public IExeucutesSingleRule GetRuleExecutor(ResolvedValidationOptions options) => new SingleRuleExecutor(contextFactory);
public void GetRulesWithDependenciesShouldNotReturnAnyDependenciesForARuleWhichHasNone([Frozen] IGetsAllExecutableRules executableRulesProvider, [ManifestModel] ManifestValue manifestValue, object objectToBeValidated, [ExecutableModel] ExecutableRule rule, ExecutableRulesAndDependenciesProvider sut, ResolvedValidationOptions validationOptions) { Mock.Get(executableRulesProvider) .Setup(x => x.GetExecutableRules(manifestValue, objectToBeValidated, validationOptions)) .Returns(new [] { rule }); var result = sut.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions); Assert.That(result.Single().Dependencies, Is.Empty); }
/// <inheritdoc/> public GetValueToBeValidatedResponse GetValueToBeValidated(IManifestValue manifestValue, object parentValue, ResolvedValidationOptions validationOptions) { if (parentValue is null) { return(IgnoredGetValueToBeValidatedResponse.Default); } try { var valueToBeValidated = manifestValue.AccessorFromParent(parentValue); return(new SuccessfulGetValueToBeValidatedResponse(valueToBeValidated)); } catch (Exception e) { var behaviour = behaviourProvider.GetBehaviour(manifestValue, validationOptions); if (behaviour == ValueAccessExceptionBehaviour.Throw) { var message = String.Format(Resources.ExceptionMessages.GetExceptionMessage("ErrorAccessingValue"), manifestValue, parentValue); throw new ValidationException(message, e); } else if (behaviour == ValueAccessExceptionBehaviour.TreatAsError) { return(new ErrorGetValueToBeValidatedResponse(e)); } return(IgnoredGetValueToBeValidatedResponse.Default); } }