public static bool Validate(Composition composition, OperationalTemplate template, AcceptValidationError acceptErrorDelegate) { Check.Require(composition != null, "composition must not be null."); Check.Require(template != null, "template must not be null."); template.Definition.SetValidationContext(new ValidationContext(acceptErrorDelegate, null)); return(template.Definition.ValidValue(composition)); }
private bool IsOccurrencesValid(AssumedTypes.IList dataChildren) { Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "dataChildren")); bool result = true; System.Collections.Generic.List <MatchingItems> matches = new System.Collections.Generic.List <MatchingItems>(); AcceptValidationError previousErrorDelegate = ValidationContext.AcceptError; try { ValidationContext.AcceptError = null; foreach (CObject constraint in Children) { if (constraint.Occurrences.Lower > 0 || !constraint.Occurrences.UpperUnbounded) { matches.Add(new MatchingItems(dataChildren, constraint)); } } } finally { ValidationContext.AcceptError = previousErrorDelegate; } foreach (MatchingItems match in matches) { match.RemoveItemsAlreadyMatchedByNameAndNodeId(matches); CObject constraint = match.Constraint; int lower = match.Lower; long upper = match.Upper; int actual = match.Count; if (actual < lower) { result = false; ValidationContext.AcceptValidationError(constraint, string.Format(AmValidationStrings.NotEnoughOccurrences, constraint.NodeId, lower, actual)); } if (actual > upper) { result = false; ValidationContext.AcceptValidationError(constraint, string.Format(AmValidationStrings.TooManyOccurrences, constraint.NodeId, upper, actual)); } } return(result); }
private bool IsOrderedChildrenValid(AssumedTypes.IList dataChildren) { Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "children")); int n = 0; bool result = true; foreach (object dataItem in dataChildren) { int startingPoint = n; CObject matchedCObject = null; string dataItemRmType = ((IRmType)dataItem).GetRmTypeName(); ILocatable locatable = dataItem as ILocatable; while (n < Children.Count) { CObject eachChild = Children[n]; if (locatable == null || locatable.ArchetypeNodeId == eachChild.ArchetypeNodeId) { if (eachChild.IsSameRmType(dataItem as IRmType)) { matchedCObject = eachChild; CComplexObject complexObject = eachChild as CComplexObject; if (complexObject != null) { CAttribute nameAttribute = NameAttributeConstraint(complexObject); if (nameAttribute != null) { bool nameMatched = false; AcceptValidationError previousErrorDelegate = ValidationContext.AcceptError; try { ValidationContext.AcceptError = null; nameMatched = nameAttribute.ValidValue(locatable.Name); } finally { ValidationContext.AcceptError = previousErrorDelegate; } if (nameMatched) { break; } else { n++; continue; } } } break; } } n++; } if (matchedCObject == null) { n = startingPoint; bool validationResult = true; if (MatchedWithSlot(locatable, out validationResult)) { result = validationResult; } else { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.YNotAllowedByAttributeXConstraint, RmAttributeName, dataItemRmType)); } } else if (!matchedCObject.ValidValue(dataItem)) { result = false; } } return(result); }
private bool IsUnorderedChildrenValid(AssumedTypes.IList dataChildren) { Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "children")); bool result = true; foreach (object dataItem in dataChildren) { System.Collections.Generic.List <CObject> matchedChildren = new System.Collections.Generic.List <CObject>(); System.Collections.Generic.List <ArchetypeSlot> slots = new System.Collections.Generic.List <ArchetypeSlot>(); IRmType rmType = dataItem as IRmType; Check.Assert(rmType != null, string.Format(AmValidationStrings.XMustImplementY, dataItem.GetType().ToString(), "IRmType")); ILocatable locatable = dataItem as ILocatable; // get all child constraint objects with this data item's node_id foreach (CObject eachChild in Children) { if (locatable == null || locatable.ArchetypeNodeId == eachChild.ArchetypeNodeId) { if (eachChild.IsSameRmType(rmType)) { matchedChildren.Add(eachChild); } } } bool matchedWithSlot = false; if (matchedChildren.Count == 0) { bool validationResult = true; matchedWithSlot = MatchedWithSlot(locatable, out validationResult); if (matchedWithSlot) { result &= validationResult; } else { // child constraint object not found for this data item result = false; string errorRmTypeName = rmType.GetRmTypeName(); ILocatable locatableDataItem = dataItem as ILocatable; if (locatableDataItem != null) { ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ItemXWithIdYNotAllowedByAttributeZ, errorRmTypeName, locatableDataItem.ArchetypeNodeId, RmAttributeName)); } else { ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ItemXNotAllowedByAttributeY, errorRmTypeName, RmAttributeName)); } } } CObject unnamedMatchedObject = null; bool validResult = false; // attempt to match data item against child constraint objects with a name attribute constraint foreach (CObject matchedObject in matchedChildren) { CComplexObject complexObject = matchedObject as CComplexObject; if (complexObject == null) { throw new ApplicationException(AmValidationStrings.MultiAttributeChildNotComplexObj); } CAttribute nameAttribute = NameAttributeConstraint(complexObject); if (nameAttribute != null) { bool nameAttributeFound = false; AcceptValidationError previousErrorDelegate = ValidationContext.AcceptError; try { ValidationContext.AcceptError = null; nameAttributeFound = nameAttribute.ValidValue(locatable.Name); } finally { ValidationContext.AcceptError = previousErrorDelegate; } if (nameAttributeFound) { validResult = matchedObject.ValidValue(dataItem); if (validResult) { break; } else { result = false; } } } else { // keep child constraint object with no name attribute constraint for later if (unnamedMatchedObject != null) { throw new ApplicationException(AmValidationStrings.ExpectingOnlyOneUnnamedChild); } unnamedMatchedObject = matchedObject; } } // no matching named object constraint, so attempt to validate against unnamed object constraint if (!validResult && !matchedWithSlot) { if (unnamedMatchedObject == null) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.NotAllowedByAttributeXConstraint, RmAttributeName)); } else if (!unnamedMatchedObject.ValidValue(dataItem)) { result = false; } } } return(result); }
public ValidationContext(AcceptValidationError acceptErrorDelegate, FetchOperationalObject fetchObjectDelegate) { AcceptError = acceptErrorDelegate; FetchObject = fetchObjectDelegate; }
public ValidationContext(AcceptValidationError acceptErrorDelegate, FetchOperationalObject fetchObjectDelegate, ITerminologyService terminologyService) : this(acceptErrorDelegate, fetchObjectDelegate) { Check.Require(terminologyService != null, "terminologyService must not be null."); this.terminologyService = terminologyService; }