Context containing all validation and provides several methods to gather this information.
Inheritance: IValidationContext
            public void AcceptsNullArguments()
            {
                var validationContext = new ValidationContext(null, null);

                Assert.AreEqual(0, validationContext.GetFieldValidationCount());
                Assert.AreEqual(0, validationContext.GetBusinessRuleValidationCount());
            }
        public IValidationContext Validate(string machineIdToValidate)
        {
            Argument.IsNotNullOrWhitespace(() => machineIdToValidate);

            var validationContext = new ValidationContext();

            Log.Debug("Retrieving machine id");

            var machineId = _identificationService.GetMachineId();

            Log.Debug("Validating machine id '{0}' against expected machine id '{1}'", machineId, machineIdToValidate);

            var machineSplitted = machineId.Split(new[] { LicenseElements.IdentificationSeparator }, StringSplitOptions.None);
            var expectedSplitter = machineIdToValidate.Split(new[] { LicenseElements.IdentificationSeparator }, StringSplitOptions.None);

            if (machineSplitted.Length != expectedSplitter.Length)
            {
                var error = "The number of items inside the license differ too much, assuming machine ids do not match";
                Log.Error(error);
                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(error));

                return validationContext;
            }

            var invalidEntries = 0;

            for (var i = 0; i < machineSplitted.Length; i++)
            {
                if (!string.Equals(expectedSplitter[i], machineSplitted[i], StringComparison.OrdinalIgnoreCase))
                {
                    invalidEntries++;
                }
            }

            if (invalidEntries > Threshold)
            {
                var error = string.Format("{0} values are not equal, not accepting the machine id, maximum threshold is '{1}'", invalidEntries, Threshold);
                Log.Error(error);
                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(error));

                return validationContext;
            }

            if (invalidEntries > 0)
            {
                var warning = string.Format("One of the values is not equal, but we have a threshold of {0} so accepting machine id", Threshold);
                Log.Warning(warning);

                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(warning));
            }

            return validationContext;
        }
            public void ReturnsEmptyCollectionForEqualValidationContexts()
            {
                var fieldValidationResults1 = new List<IFieldValidationResult>();
                fieldValidationResults1.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
                fieldValidationResults1.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));

                var businessRuleValidationResults1 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError"));
                var context1 = new ValidationContext(fieldValidationResults1, businessRuleValidationResults1);

                var fieldValidationResults2 = new List<IFieldValidationResult>();
                fieldValidationResults2.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
                fieldValidationResults2.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));

                var businessRuleValidationResults2 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
                businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError"));
                var context2 = new ValidationContext(fieldValidationResults2, businessRuleValidationResults2);

                var changes = ValidationContextHelper.GetChanges(context1, context2);

                Assert.AreEqual(0, changes.Count);
            }
Esempio n. 4
0
        /// <summary>
        /// Validates the current object for field and business rule errors.
        /// </summary>
        /// <param name="force">If set to <c>true</c>, a validation is forced (even if the object knows it is already validated).</param>
        /// <param name="validateDataAnnotations">If set to <c>true</c>, the data annotations will be checked. This value is only used if <paramref name="force"/> is set to <c>true</c>.</param>
        /// <remarks>
        /// To check whether this object contains any errors, use the <see cref="INotifyDataErrorInfo.HasErrors"/> property.
        /// </remarks>
        internal void Validate(bool force, bool validateDataAnnotations)
        {
            if (SuspendValidation)
            {
                return;
            }

            if (IsValidating)
            {
                return;
            }

            IsValidating = true;

            var existingValidationContext = (ValidationContext)_validationContext;
            bool hasErrors = existingValidationContext.HasErrors;
            bool hasWarnings = existingValidationContext.HasWarnings;

            var validationContext = new ValidationContext();
            var changes = new List<ValidationContextChange>();

            var validator = GetValidator();
            if (validator != null)
            {
                validator.BeforeValidation(this, existingValidationContext.GetFieldValidations(), existingValidationContext.GetBusinessRuleValidations());
            }

            OnValidating(validationContext);

            CatchUpWithSuspendedAnnotationsValidation();

            if (force && validateDataAnnotations)
            {
                var type = GetType();

                var ignoredOrFailedPropertyValidations = _propertyValuesIgnoredOrFailedForValidation[type];

                // In forced mode, validate all registered properties for annotations
                var catelTypeInfo = PropertyDataManager.GetCatelTypeInfo(type);
                foreach (var propertyData in catelTypeInfo.GetCatelProperties())
                {
                    var propertyInfo = propertyData.Value.GetPropertyInfo(type);
                    if (propertyInfo == null || !propertyInfo.HasPublicGetter)
                    {
                        // Note: non-public getter, do not validate
                        ignoredOrFailedPropertyValidations.Add(propertyData.Key);
                        continue;
                    }

                    var propertyValue = GetValue(propertyData.Value);
                    ValidatePropertyUsingAnnotations(propertyData.Key, propertyValue, propertyData.Value);
                }

#if !WINDOWS_PHONE && !NETFX_CORE && !PCL && !NET35
                // Validate non-catel properties as well for attribute validation
                foreach (var propertyInfo in catelTypeInfo.GetNonCatelProperties())
                {
                    if (_firstAnnotationValidation)
                    {
                        if (propertyInfo.Value.IsDecoratedWithAttribute(typeof(ExcludeFromValidationAttribute)))
                        {
                            ignoredOrFailedPropertyValidations.Add(propertyInfo.Key);
                        }
                    }

                    if (!propertyInfo.Value.HasPublicGetter)
                    {
                        // Note: non-public getter, do not validate
                        ignoredOrFailedPropertyValidations.Add(propertyInfo.Key);
                        continue;
                    }

                    // TODO: Should we check for annotations attributes?
                    if (ignoredOrFailedPropertyValidations.Contains(propertyInfo.Key))
                    {
                        continue;
                    }

                    try
                    {
                        var propertyValue = propertyInfo.Value.PropertyInfo.GetValue(this, null);
                        ValidatePropertyUsingAnnotations(propertyInfo.Key, propertyValue, null);
                    }
                    catch (Exception ex)
                    {
                        Log.Warning(ex, "Failed to validate property '{0}.{1}', adding it to the ignore list", type.Name, propertyInfo.Key);
                        ignoredOrFailedPropertyValidations.Add(propertyInfo.Key);
                    }
                }

                _firstAnnotationValidation = false;
#endif
            }

            if (!IsValidated || force)
            {
                lock (_validationLock)
                {
                    var fieldValidationResults = new List<IFieldValidationResult>();
                    var businessRuleValidationResults = new List<IBusinessRuleValidationResult>();

                    #region Fields
                    if (validator != null)
                    {
                        validator.BeforeValidateFields(this, validationContext.GetFieldValidations());
                    }

                    OnValidatingFields(validationContext);

                    if (validator != null)
                    {
                        validator.ValidateFields(this, fieldValidationResults);
                    }

#if !WINDOWS_PHONE && !NETFX_CORE && !PCL && !NET35
                    // Support annotation validation
                    fieldValidationResults.AddRange(from fieldAnnotationValidation in _dataAnnotationValidationResults
                                                    where !string.IsNullOrEmpty(fieldAnnotationValidation.Value)
                                                    select (IFieldValidationResult)FieldValidationResult.CreateError(fieldAnnotationValidation.Key, fieldAnnotationValidation.Value));
#endif

                    ValidateFields(fieldValidationResults);

                    OnValidatedFields(validationContext);

                    if (validator != null)
                    {
                        validator.AfterValidateFields(this, fieldValidationResults);
                    }

                    // As the last step, sync the field validation results with the context
                    foreach (var fieldValidationResult in fieldValidationResults)
                    {
                        validationContext.AddFieldValidationResult(fieldValidationResult);
                    }
                    #endregion

                    #region Business rules
                    if (validator != null)
                    {
                        validator.BeforeValidateBusinessRules(this, validationContext.GetBusinessRuleValidations());
                    }

                    OnValidatingBusinessRules(validationContext);

                    if (validator != null)
                    {
                        validator.ValidateBusinessRules(this, businessRuleValidationResults);
                    }

                    ValidateBusinessRules(businessRuleValidationResults);

                    OnValidatedBusinessRules(validationContext);

                    if (validator != null)
                    {
                        validator.AfterValidateBusinessRules(this, businessRuleValidationResults);
                    }

                    // As the last step, sync the field validation results with the context
                    foreach (var businessRuleValidationResult in businessRuleValidationResults)
                    {
                        validationContext.AddBusinessRuleValidationResult(businessRuleValidationResult);
                    }
                    #endregion

                    if (validator != null)
                    {
                        validator.Validate(this, validationContext);
                    }

                    IsValidated = true;

                    // Manual sync to get the changes
                    changes = existingValidationContext.SynchronizeWithContext(validationContext);
                }
            }

            OnValidated(validationContext);

            if (validator != null)
            {
                validator.AfterValidation(this, validationContext.GetFieldValidations(), validationContext.GetBusinessRuleValidations());
            }

            #region Notify changes
            bool hasNotifiedBusinessWarningsChanged = false;
            bool hasNotifiedBusinessErrorsChanged = false;
            foreach (var change in changes)
            {
                var changeAsFieldValidationResult = change.ValidationResult as IFieldValidationResult;
                var changeAsBusinessRuleValidationResult = change.ValidationResult as IBusinessRuleValidationResult;

                if (changeAsFieldValidationResult != null)
                {
                    switch (change.ValidationResult.ValidationResultType)
                    {
                        case ValidationResultType.Warning:
                            NotifyWarningsChanged(changeAsFieldValidationResult.PropertyName, false);
                            break;

                        case ValidationResultType.Error:
                            NotifyErrorsChanged(changeAsFieldValidationResult.PropertyName, false);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
                else if (changeAsBusinessRuleValidationResult != null)
                {
                    switch (change.ValidationResult.ValidationResultType)
                    {
                        case ValidationResultType.Warning:
                            if (!hasNotifiedBusinessWarningsChanged)
                            {
                                hasNotifiedBusinessWarningsChanged = true;
                                NotifyWarningsChanged(string.Empty, false);
                            }
                            break;

                        case ValidationResultType.Error:
                            if (!hasNotifiedBusinessErrorsChanged)
                            {
                                hasNotifiedBusinessErrorsChanged = true;
                                NotifyErrorsChanged(string.Empty, false);
                            }
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
            }

            if (_validationContext.HasWarnings != hasWarnings)
            {
                RaisePropertyChanged(HasWarningsMessageProperty);
            }

            if (_validationContext.HasErrors != hasErrors)
            {
                RaisePropertyChanged(HasErrorsMessageProperty);
            }
            #endregion

            IsValidating = false;
        }
        /// <summary>
        /// Gets the validation context for a complete object graph by also checking the properties and recursive 
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>The validation context for the whole object graph.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception>
        public static IValidationContext GetValidationContextForObjectGraph(this IModel model)
        {
            Argument.IsNotNull("model", model);

            var validationContext = new ValidationContext();

            validationContext.AddModelValidation(model, new List<IModel>());

            return validationContext;
        }
Esempio n. 6
0
 protected override void Validate(ClassWithValidator instance, Catel.Data.ValidationContext validationContext)
 {
     ValidateCount++;
 }
        /// <summary>
        /// Validates the license.
        /// </summary>
        /// <param name="license">The license key the user has given to be validated.</param>
        /// <returns>The validation context containing all the validation results.</returns>
        public IValidationContext ValidateLicense(string license)
        {
            Argument.IsNotNullOrWhitespace(() => license);

            var validationContext = new ValidationContext();

            Log.Info("Validating license");

            try
            {
                var licenseObject = License.Load(license);
                var failureList = licenseObject.Validate()
                    .Signature(_applicationIdService.ApplicationId)
                    .AssertValidLicense().ToList();

                if (failureList.Count > 0)
                {
                    foreach (var failure in failureList)
                    {
                        var businessRuleValidationResult = BusinessRuleValidationResult.CreateErrorWithTag(failure.Message, failure.HowToResolve);
                        validationContext.AddBusinessRuleValidationResult(businessRuleValidationResult);
                    }
                }

                var licenseAttributes = licenseObject.AdditionalAttributes;
                if (licenseAttributes != null)
                {
                    foreach (var licenseAttribute in licenseAttributes.GetAll())
                    {
                        if (string.Equals(licenseAttribute.Key, LicenseElements.MachineId))
                        {
                            Log.Debug("Validating license using machine ID");

                            var machineLicenseValidationContext = _machineLicenseValidationService.Validate(licenseAttribute.Value);
                            validationContext.SynchronizeWithContext(machineLicenseValidationContext, true);

                            if (machineLicenseValidationContext.HasErrors)
                            {
                                Log.Error("The license can only run on machine with ID '{0}'", licenseAttribute.Value);
                            }
                        }

                        // TODO: add additional attribute checks here
                    }
                }

                // Also validate the xml, very important for expiration date and version
                var xmlValidationContext = ValidateXml(license);
                validationContext.SynchronizeWithContext(xmlValidationContext, true);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "An error occurred while loading the license");

                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("An unknown error occurred while loading the license, please contact support"));
            }
            finally
            {
                if (validationContext.GetErrors().Count > 0)
                {
                    Log.Warning("License is not valid:");
                    Log.Indent();

                    foreach (var error in validationContext.GetErrors())
                    {
                        Log.Warning("- {0}\n{1}", error.Message, error.Tag as string);
                    }

                    Log.Unindent();
                }
                else
                {
                    Log.Info("License is valid");
                }
            }

            return validationContext;
        }
            public void CorrectlyHandlesRemovalOfExistingValidationResult()
            {
                var validationContext = new ValidationContext();
                var fieldValidation = FieldValidationResult.CreateError("MyProperty", "MyError");

                validationContext.AddFieldValidationResult(fieldValidation);

                Assert.AreEqual(1, validationContext.GetFieldValidationCount());

                validationContext.RemoveFieldValidationResult(fieldValidation);

                Assert.AreEqual(0, validationContext.GetFieldValidationCount());
            }
            public void AddsMultipleValidationResultsForField()
            {
                var validationContext = new ValidationContext();
                var fieldValidation = FieldValidationResult.CreateError("MyProperty", "MyError");

                validationContext.AddFieldValidationResult(fieldValidation);

                Assert.AreEqual(1, validationContext.GetFieldValidationCount());

                validationContext.AddFieldValidationResult(fieldValidation);

                Assert.AreEqual(2, validationContext.GetFieldValidationCount());
            }
            public void ReturnsFalseForValidationContextWithoutWarnings()
            {
                var context = new ValidationContext();

                Assert.IsFalse(context.HasWarnings);
            }
            public void ReturnsZeroForContextWithoutErrors()
            {
                var context = new ValidationContext();

                Assert.AreEqual(0, context.GetWarningCount());
            }
            public void ReturnsRightValidationsForEmptyContextWithTag()
            {
                var context = new ValidationContext();

                var validations = context.GetValidations("tag");
                Assert.AreEqual(0, validations.Count);
            }
            public void CorrectlySynchronizesWithRemovedBusinessRuleError()
            {
                var fieldValidationResults1 = new List<IFieldValidationResult>();
                fieldValidationResults1.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
                fieldValidationResults1.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));

                var businessRuleValidationResults1 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError"));
                var context1 = new ValidationContext(fieldValidationResults1, businessRuleValidationResults1);

                var fieldValidationResults2 = new List<IFieldValidationResult>();
                fieldValidationResults2.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
                fieldValidationResults2.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));

                var businessRuleValidationResults2 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
                var context2 = new ValidationContext(fieldValidationResults2, businessRuleValidationResults2);

                context1.SynchronizeWithContext(context2);

                // First context should now equal second context
                int counter;

                var fieldValidations = context1.GetFieldValidations();
                Assert.AreEqual(2, fieldValidations.Count);
                counter = -1;

                counter++;
                Assert.AreEqual("MyProperty", fieldValidations[counter].PropertyName);
                Assert.AreEqual("FieldWarning", fieldValidations[counter].Message);
                Assert.AreEqual(ValidationResultType.Warning, fieldValidations[counter].ValidationResultType);

                counter++;
                Assert.AreEqual("MyProperty", fieldValidations[counter].PropertyName);
                Assert.AreEqual("FieldError", fieldValidations[counter].Message);
                Assert.AreEqual(ValidationResultType.Error, fieldValidations[counter].ValidationResultType);

                var businessRuleValidations = context1.GetBusinessRuleValidations();
                Assert.AreEqual(1, businessRuleValidations.Count);
                counter = -1;

                counter++;
                Assert.AreEqual("BusinessRuleWarning", businessRuleValidations[counter].Message);
                Assert.AreEqual(ValidationResultType.Warning, businessRuleValidations[counter].ValidationResultType);
            }
            public void CorrectlyHandlesRemovalOfNonExistingValidationResult()
            {
                var validationContext = new ValidationContext();
                var businessRuleValidation = BusinessRuleValidationResult.CreateError("MyError");

                Assert.AreEqual(0, validationContext.GetBusinessRuleValidationCount());

                validationContext.RemoveBusinessRuleValidationResult(businessRuleValidation);

                Assert.AreEqual(0, validationContext.GetBusinessRuleValidationCount());
            }
            public void AddsValidationResultTwice()
            {
                var validationContext = new ValidationContext();
                var businessRuleValidation = BusinessRuleValidationResult.CreateError("MyProperty");

                validationContext.AddBusinessRuleValidationResult(businessRuleValidation);

                Assert.AreEqual(1, validationContext.GetBusinessRuleValidationCount());

                validationContext.AddBusinessRuleValidationResult(businessRuleValidation);

                Assert.AreEqual(2, validationContext.GetBusinessRuleValidationCount());
            }
            public void ReturnsRightValidationsForEmptyContextWithTag()
            {
                var context = new ValidationContext();

                var businessRuleValidations = context.GetBusinessRuleErrors("tag");
                Assert.AreEqual(0, businessRuleValidations.Count);
            }
            public void ReturnsRightAmountForEmptyValidationContextWithTag()
            {
                var context = new ValidationContext();

                Assert.AreEqual(0, context.GetFieldWarningCount("tag"));
            }
            public void ThrowsArgumentNullExceptionForNullValidationResult()
            {
                var validationContext = new ValidationContext();

                ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => validationContext.RemoveFieldValidationResult(null));
            }
            public void ReturnsRightValidationsForEmptyContextWithTag()
            {
                var context = new ValidationContext();

                var fieldValidations = context.GetFieldWarnings((object)"tag");
                Assert.AreEqual(0, fieldValidations.Count);
            }
        /// <summary>
        /// Validates the XML
        /// </summary>
        /// <param name="license">The license.</param>
        /// <returns>The validation context containing all the validation results.</returns>
        /// <exception cref="ArgumentException">The <paramref name="license" /> is <c>null</c> or whitespace.</exception>
        /// <exception cref="XmlException">The license text is not valid XML.</exception>
        /// <exception cref="Exception">The root element is not License.</exception>
        /// <exception cref="Exception">There were no inner nodes found.</exception>
        public IValidationContext ValidateXml(string license)
        {
            var validationContext = new ValidationContext();
            if (string.IsNullOrWhiteSpace(license))
            {
                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("No license available"));
            }

            var xmlDataList = new List<XmlDataModel>();

            try
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(license);
                var xmlRoot = xmlDoc.DocumentElement;
                if (!string.Equals(xmlRoot.Name, "License"))
                {
                    validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("Please make sure that you pasted the complete license, including the <License> tags"));
                }

                var xmlNodes = xmlRoot.ChildNodes;
                foreach (XmlNode node in xmlNodes)
                {
                    if (!string.Equals(node.Name, "ProductFeatures"))
                    {
                        xmlDataList.Add(new XmlDataModel
                        {
                            Name = node.Name,
                            Value = node.InnerText
                        });
                    }
                    else
                    {
                        foreach (XmlNode featureNode in node.ChildNodes)
                        {
                            xmlDataList.Add(new XmlDataModel
                            {
                                Name = featureNode.Attributes[0].Value,
                                Value = featureNode.InnerText
                            });
                        }
                    }
                }

                if (xmlDataList.Count == 0)
                {
                    validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("License contains no valid data"));
                }

                var expData = xmlDataList.FirstOrDefault(x => string.Equals(x.Name, LicenseElements.Expiration));
                if (expData != null)
                {
                    DateTime expirationDateTime;
                    if (DateTime.TryParse(expData.Value, out expirationDateTime))
                    {
                        Log.Debug("Using expiration behavior '{0}'", _expirationBehavior.GetType().Name);

                        var portableLicense = License.Load(license);

                        if (_expirationBehavior.IsExpired(portableLicense, expirationDateTime, DateTime.Now))
                        {
                            validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("The license is expired"));
                        }
                    }
                    else
                    {
                        validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("The expiration date was not a valid date / tim value"));
                    }
                }

                var xmlDataVersion = xmlDataList.FirstOrDefault(x => string.Equals(x.Name, LicenseElements.Version));
                if (xmlDataVersion != null)
                {
                    Version licenseVersion;
                    if (Version.TryParse(xmlDataVersion.Value, out licenseVersion))
                    {
                        var productVersion = Assembly.GetExecutingAssembly().GetName().Version;
                        if (productVersion > licenseVersion)
                        {
                            validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("Your license only supports versions up to '{0}' while the current version of this product is '{1}'", licenseVersion, productVersion));
                        }
                    }
                    else
                    {
                        validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("The version was not a valid version value"));
                    }
                }
            }
            catch (XmlException xmlex)
            {
                Log.Debug(xmlex);

                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("The license data is not a license"));
            }
            catch (Exception ex)
            {
                Log.Debug(ex);

                //var innermessage = string.Empty;
                //if (ex.InnerException != null)
                //{
                //    innermessage = ex.InnerException.Message;
                //}

                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(ex.Message));
            }

            if (validationContext.HasErrors || validationContext.HasWarnings)
            {
                Log.Warning("The XML is invalid");
            }
            return validationContext;
        }
            public void ReturnsFalseForValidationContextWithoutErrors()
            {
                var context = new ValidationContext();

                Assert.IsFalse(context.HasErrors);
            }
Esempio n. 22
0
        /// <summary>
        /// Validates the current object for field and business rule errors.
        /// </summary>
        /// <param name="force">If set to <c>true</c>, a validation is forced (even if the object knows it is already validated).</param>
        /// <param name="validateDataAnnotations">If set to <c>true</c>, the data annotations will be checked. This value is only used if <paramref name="force"/> is set to <c>true</c>.</param>
        /// <remarks>
        /// To check whether this object contains any errors, use the <see cref="HasErrors"/> property.
        /// </remarks>
        internal void Validate(bool force, bool validateDataAnnotations)
        {
            if (SuspendValidation)
            {
                return;
            }

            if (IsValidating)
            {
                return;
            }

            IsValidating = true;

            var validationContext = (ValidationContext)ValidationContext;
            var changes = new List<ValidationContextChange>();
            bool hasErrors = HasErrors;
            bool hasWarnings = HasWarnings;

            var validator = Validator;
            if (validator != null)
            {
                validator.BeforeValidation(this, validationContext.GetFieldValidations(), validationContext.GetBusinessRuleValidations());
            }

            OnValidating();

            CatchUpWithSuspendedAnnotationsValidation();

            if (force && validateDataAnnotations)
            {
                var type = GetType();

                // In forced mode, validate all registered properties for annotations
                var catelTypeInfo = PropertyDataManager.GetCatelTypeInfo(type);
                foreach (var propertyData in catelTypeInfo.GetCatelProperties())
                {
                    var propertyValue = GetValue(propertyData.Value);
                    ValidatePropertyUsingAnnotations(propertyData.Key, propertyValue);
                }

#if !WINDOWS_PHONE && !NETFX_CORE && !PCL && !NET35
                // Validate non-catel properties as well for attribute validation
                foreach (var propertyInfo in catelTypeInfo.GetNonCatelProperties())
                {
                    // TODO: Should we check for annotations attributes?
                    var failedPropertyValidations = _propertyValuesFailedForValidation[type];
                    if (failedPropertyValidations.Contains(propertyInfo.Key))
                    {
                        continue;
                    }

                    try
                    {
                        var propertyValue =  propertyInfo.Value.GetValue(this, null);
                        ValidatePropertyUsingAnnotations(propertyInfo.Key, propertyValue);
                    }
                    catch (Exception ex)
                    {
                        Log.Warning(ex, "Failed to validate property '{0}.{1}', adding it to the ignore list", type.Name, propertyInfo.Key);
                        failedPropertyValidations.Add(propertyInfo.Key);
                    }
                }
#endif
            }

            if (!IsValidated || force)
            {
                lock (_validationLock)
                {
                    var fieldValidationResults = new List<IFieldValidationResult>();
                    var businessRuleValidationResults = new List<IBusinessRuleValidationResult>();

                    #region Fields
                    if (validator != null)
                    {
                        validator.BeforeValidateFields(this, validationContext.GetFieldValidations());
                    }

                    OnValidatingFields();

                    if (validator != null)
                    {
                        validator.ValidateFields(this, fieldValidationResults);
                    }

#if !WINDOWS_PHONE && !NETFX_CORE && !PCL && !NET35
                    // Support annotation validation
                    fieldValidationResults.AddRange(from fieldAnnotationValidation in _dataAnnotationValidationResults
                                                    where !string.IsNullOrEmpty(fieldAnnotationValidation.Value)
                                                    select (IFieldValidationResult)FieldValidationResult.CreateError(fieldAnnotationValidation.Key, fieldAnnotationValidation.Value));
#endif

                    ValidateFields(fieldValidationResults);

                    // In-between validations, it might be possible that users used the SetFieldValidationResult
                    if (_internalValidationContext != null)
                    {
                        fieldValidationResults.AddRange(_internalValidationContext.GetFieldValidations());
                    }

                    OnValidatedFields();

                    if (validator != null)
                    {
                        validator.AfterValidateFields(this, fieldValidationResults);
                    }
                    #endregion

                    #region Business rules
                    if (validator != null)
                    {
                        validator.BeforeValidateBusinessRules(this, validationContext.GetBusinessRuleValidations());
                    }

                    OnValidatingBusinessRules();

                    if (validator != null)
                    {
                        validator.ValidateBusinessRules(this, businessRuleValidationResults);
                    }

                    ValidateBusinessRules(businessRuleValidationResults);

                    // In-between validations, it might be possible that users used the SetBusinessRuleValidationResult
                    if (_internalValidationContext != null)
                    {
                        businessRuleValidationResults.AddRange(_internalValidationContext.GetBusinessRuleValidations());
                    }

                    OnValidatedBusinessRules();

                    if (validator != null)
                    {
                        validator.AfterValidateBusinessRules(this, businessRuleValidationResults);
                    }
                    #endregion

                    if (validator != null)
                    {
                        validator.Validate(this, validationContext);
                    }

                    IsValidated = true;

                    // Clear internal validation
                    _internalValidationContext = new ValidationContext();

                    changes = validationContext.SynchronizeWithContext(new ValidationContext(fieldValidationResults, businessRuleValidationResults));
                }
            }

            OnValidated();

            if (validator != null)
            {
                validator.AfterValidation(this, validationContext.GetFieldValidations(), validationContext.GetBusinessRuleValidations());
            }

            #region Notify changes
            bool hasNotifiedBusinessWarningsChanged = false;
            bool hasNotifiedBusinessErrorsChanged = false;
            foreach (var change in changes)
            {
                var changeAsFieldValidationResult = change.ValidationResult as IFieldValidationResult;
                var changeAsBusinessRuleValidationResult = change.ValidationResult as IBusinessRuleValidationResult;

                if (changeAsFieldValidationResult != null)
                {
                    switch (change.ValidationResult.ValidationResultType)
                    {
                        case ValidationResultType.Warning:
                            NotifyWarningsChanged(changeAsFieldValidationResult.PropertyName, false);
                            break;

                        case ValidationResultType.Error:
                            NotifyErrorsChanged(changeAsFieldValidationResult.PropertyName, false);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
                else if (changeAsBusinessRuleValidationResult != null)
                {
                    switch (change.ValidationResult.ValidationResultType)
                    {
                        case ValidationResultType.Warning:
                            if (!hasNotifiedBusinessWarningsChanged)
                            {
                                hasNotifiedBusinessWarningsChanged = true;
                                NotifyWarningsChanged(string.Empty, false);
                            }
                            break;

                        case ValidationResultType.Error:
                            if (!hasNotifiedBusinessErrorsChanged)
                            {
                                hasNotifiedBusinessErrorsChanged = true;
                                NotifyErrorsChanged(string.Empty, false);
                            }
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
            }


            if (HasWarnings != hasWarnings)
            {
                RaisePropertyChanged("HasWarnings");
            }

            if (HasErrors != hasErrors)
            {
                RaisePropertyChanged("HasErrors");
            }
            #endregion

            IsValidating = false;
        }
            public void ReturnsRightValidationsForEmptyContext()
            {
                var context = new ValidationContext();

                var fieldValidations = context.GetFieldErrors();
                Assert.AreEqual(0, fieldValidations.Count);
            }
            public void ReturnsChangesForComplexScenario()
            {
                var fieldValidationResults1 = new List<IFieldValidationResult>();
                fieldValidationResults1.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
                fieldValidationResults1.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));

                var businessRuleValidationResults1 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError"));
                var context1 = new ValidationContext(fieldValidationResults1, businessRuleValidationResults1);

                var fieldValidationResults2 = new List<IFieldValidationResult>();
                fieldValidationResults2.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarningTextHasChanged"));
                fieldValidationResults2.Add(FieldValidationResult.CreateError("NewProperty", "FieldErrorForNewProperty"));

                var businessRuleValidationResults2 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarningTextHasChanged"));
                var context2 = new ValidationContext(fieldValidationResults2, businessRuleValidationResults2);

                var changes = ValidationContextHelper.GetChanges(context1, context2);

                Assert.AreEqual(7, changes.Count);
                int counter;

                // Field warning text has changed, thus removed
                counter = 0;
                Assert.AreEqual("MyProperty", ((IFieldValidationResult) changes[counter].ValidationResult).PropertyName);
                Assert.AreEqual("FieldWarning", changes[counter].ValidationResult.Message);
                Assert.AreEqual(ValidationResultType.Warning, changes[counter].ValidationResult.ValidationResultType);
                Assert.AreEqual(ValidationContextChangeType.Removed, changes[counter].ChangeType);

                // Field error has been removed
                counter++;
                Assert.AreEqual("MyProperty", ((IFieldValidationResult) changes[counter].ValidationResult).PropertyName);
                Assert.AreEqual("FieldError", changes[counter].ValidationResult.Message);
                Assert.AreEqual(ValidationResultType.Error, changes[counter].ValidationResult.ValidationResultType);
                Assert.AreEqual(ValidationContextChangeType.Removed, changes[counter].ChangeType);

                // Field warning text has changed, thus added
                counter++;
                Assert.AreEqual("MyProperty", ((IFieldValidationResult) changes[counter].ValidationResult).PropertyName);
                Assert.AreEqual("FieldWarningTextHasChanged", changes[counter].ValidationResult.Message);
                Assert.AreEqual(ValidationResultType.Warning, changes[counter].ValidationResult.ValidationResultType);
                Assert.AreEqual(ValidationContextChangeType.Added, changes[counter].ChangeType);

                // Field error added
                counter++;
                Assert.AreEqual("NewProperty", ((IFieldValidationResult) changes[counter].ValidationResult).PropertyName);
                Assert.AreEqual("FieldErrorForNewProperty", changes[counter].ValidationResult.Message);
                Assert.AreEqual(ValidationResultType.Error, changes[counter].ValidationResult.ValidationResultType);
                Assert.AreEqual(ValidationContextChangeType.Added, changes[counter].ChangeType);

                // Business rule text has changed, thus removed
                counter++;
                Assert.AreEqual("BusinessRuleWarning", changes[counter].ValidationResult.Message);
                Assert.AreEqual(ValidationResultType.Warning, changes[counter].ValidationResult.ValidationResultType);
                Assert.AreEqual(ValidationContextChangeType.Removed, changes[counter].ChangeType);

                // Business rule error has been removed
                counter++;
                Assert.AreEqual("BusinessRuleError", changes[counter].ValidationResult.Message);
                Assert.AreEqual(ValidationResultType.Error, changes[counter].ValidationResult.ValidationResultType);
                Assert.AreEqual(ValidationContextChangeType.Removed, changes[counter].ChangeType);

                // Business rule text has changed, thus added
                counter++;
                Assert.AreEqual("BusinessRuleWarningTextHasChanged", changes[counter].ValidationResult.Message);
                Assert.AreEqual(ValidationResultType.Warning, changes[counter].ValidationResult.ValidationResultType);
                Assert.AreEqual(ValidationContextChangeType.Added, changes[counter].ChangeType);
            }
            public void ReturnsRightValidationsForEmptyContextWithTagAndPropertyName()
            {
                var context = new ValidationContext();

                var fieldValidations = context.GetFieldErrors("MyProperty");
                Assert.AreEqual(0, fieldValidations.Count);
            }
            public void ReturnsChangesForAddedFieldWarning()
            {
                var fieldValidationResults1 = new List<IFieldValidationResult>();
                fieldValidationResults1.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));

                var businessRuleValidationResults1 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError"));
                var context1 = new ValidationContext(fieldValidationResults1, businessRuleValidationResults1);

                var fieldValidationResults2 = new List<IFieldValidationResult>();
                fieldValidationResults2.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
                fieldValidationResults2.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));

                var businessRuleValidationResults2 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
                businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError"));
                var context2 = new ValidationContext(fieldValidationResults2, businessRuleValidationResults2);

                var changes = ValidationContextHelper.GetChanges(context1, context2);

                Assert.AreEqual(1, changes.Count);
                Assert.AreEqual("MyProperty", ((IFieldValidationResult) changes[0].ValidationResult).PropertyName);
                Assert.AreEqual("FieldWarning", changes[0].ValidationResult.Message);
                Assert.AreEqual(ValidationResultType.Warning, changes[0].ValidationResult.ValidationResultType);
                Assert.AreEqual(ValidationContextChangeType.Added, changes[0].ChangeType);
            }
            public void ReturnsRightValidationsForEmptyContext()
            {
                var context = new ValidationContext();

                var businessRuleValidations = context.GetBusinessRuleWarnings();
                Assert.AreEqual(0, businessRuleValidations.Count);
            }
            public void ReturnsRightAmountForEmptyValidationContextWithTag()
            {
                var context = new ValidationContext();

                Assert.AreEqual(0, context.GetBusinessRuleErrorCount("tag"));
            }
            public void ReturnsRightAmountForEmptyValidationContext()
            {
                var context = new ValidationContext();

                Assert.AreEqual(0, context.GetValidationCount());
            }
Esempio n. 30
0
 /// <summary>
 /// Validates the specified instance and allows the manipulation of the whole validation context.
 /// <para />
 /// This method can be used to manipulate the whole validation context and the implementation of this is enough.
 /// </summary>
 /// <param name="instance">The instance to validate.</param>
 /// <param name="validationContext">The validation context.</param>
 /// <exception cref="ArgumentNullException">The <paramref name="instance"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentNullException">The <paramref name="validationContext"/> is <c>null</c>.</exception>
 public void Validate(object instance, ValidationContext validationContext)
 {
     try
     {
         foreach (IValidator validator in _validators)
         {
             validator.Validate(instance, validationContext);
         }
     }
     catch (Exception)
     {
         _synchronizationContext.Release();
         throw;
     }
 }
        public IValidationContext Parse(List<string> commandLineArguments, IContext targetContext)
        {
            var validationContext = new ValidationContext();

            targetContext.OriginalCommandLine = string.Join(" ", commandLineArguments);

            var isHelp = commandLineArguments.Any(commandLineArgument => commandLineArgument.IsHelp());
            if (isHelp)
            {
                targetContext.IsHelp = true;
                return validationContext;
            }

            var optionDefinitions = _optionDefinitionService.GetOptionDefinitions(targetContext);

            var handledOptions = new HashSet<char>();

            Log.Debug("Parsing command line");

            for (var i = 0; i < commandLineArguments.Count; i++)
            {
                var commandLineArgument = commandLineArguments[i];

                try
                {
                    // Allow the first one to be a non-switch
                    if (i == 0)
                    {
                        if (!commandLineArguments[i].IsSwitch())
                        {
                            var emptyOptionDefinition = (from x in optionDefinitions
                                                         where !x.HasSwitch()
                                                         select x).FirstOrDefault();

                            if (emptyOptionDefinition == null)
                            {
                                var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoEmptySwitch"), commandLineArgument);
                                Log.Error(message);
                                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(message));
                                continue;
                            }

                            UpdateContext(targetContext, emptyOptionDefinition, commandLineArgument);
                            handledOptions.Add(emptyOptionDefinition.ShortName);
                            continue;
                        }
                    }

                    if (!commandLineArgument.IsSwitch())
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoSwitch"), commandLineArgument);
                        Log.Warning(message);
                        validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message));
                        continue;
                    }

                    var optionDefinition = (from x in optionDefinitions
                                            where x.IsSwitch(commandLineArgument)
                                            select x).FirstOrDefault();
                    if (optionDefinition == null)
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseSwitchNotRecognized"), commandLineArgument);
                        Log.Warning(message);
                        validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message));
                        continue;
                    }

                    var value = string.Empty;
                    if (!optionDefinition.AcceptsValue)
                    {
                        // Assume boolean switch
                        value = "true";
                    }
                    else
                    {
                        if (commandLineArguments.Count <= i + 1)
                        {
                            var message = string.Format(_languageService.GetString("CommandLine_CannotParseValueMissing"), commandLineArgument);
                            Log.Warning(message);
                            validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message));
                            continue;
                        }

                        value = commandLineArguments[++i];
                    }

                    UpdateContext(targetContext, optionDefinition, value);
                    handledOptions.Add(optionDefinition.ShortName);
                }
                catch (Exception ex)
                {
                    validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(_languageService.GetString("CommandLine_CannotParseExceptionOccurred"), commandLineArgument, ex.Message));
                }
            }

            Log.Debug("Checking if all required options are specified");

            foreach (var optionDefinition in optionDefinitions)
            {
                if (optionDefinition.IsMandatory)
                {
                    if (!handledOptions.Contains(optionDefinition.ShortName))
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_RequiredSwitchNotSpecified"), optionDefinition);
                        Log.Error(message);
                        validationContext.AddFieldValidationResult(FieldValidationResult.CreateError(optionDefinition.GetSwitchDisplay(), message));
                    }
                }
            }

            Log.Debug("Finishing the context");

            targetContext.Finish();

            return validationContext;
        }