<commit_before>﻿namespace Caliburn.FluentValidation
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Reflection;
    using Core.Validation;
    using global::FluentValidation;
    using global::FluentValidation.Validators;
    using Microsoft.Practices.ServiceLocation;
    using IValidator = global::FluentValidation.IValidator;

    /// <summary>
    /// An implementation of <see cref="Core.Validation.IValidator"/> that uses FluentValidation.
    /// </summary>
    public class FluentValidationValidator : ValidatorFactoryBase, Core.Validation.IValidator
    {
        private readonly IServiceLocator _serviceLocator;

        /// <summary>
        /// Initializes a new instance of the <see cref="FluentValidationValidator"/> class.
        /// </summary>
        /// <param name="serviceLocator">The service locator.</param>
        public FluentValidationValidator(IServiceLocator serviceLocator)
        {
            _serviceLocator = serviceLocator;
        }

        /// <summary>
        /// Creates the instance.
        /// </summary>
        /// <param name="validatorType">Type of the validator.</param>
        /// <returns></returns>
        public override IValidator CreateInstance(Type validatorType)
        {
            return _serviceLocator.GetAllInstances(validatorType)
                .OfType<IValidator>()
                .FirstOrDefault();
        }

        /// <summary>
        /// Inidcates whether the specified property should be validated.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <returns>
        /// true if should be validated; otherwise false
        /// </returns>
        public bool ShouldValidate(PropertyInfo property)
        {
            var validator = GetValidator(property.DeclaringType);

            return validator != null && validator.CreateDescriptor().GetValidatorsForMember(property.Name).Any();
        }

        /// <summary>
        /// Validates the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>The validation errors.</returns>
        public IEnumerable<IError> Validate(object instance)
        {
            var validator = CreateValidatorForInstance(instance);
            if (validator == null)
                yield break;

            var result = validator.Validate(instance);

            foreach(var failure in result.Errors)
            {
                yield return new ErrorAdapter(instance, failure);
            }
        }

        /// <summary>
        /// Validates the specified property on the instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>The validation errors.</returns>
        public IEnumerable<IError> Validate(object instance, string propertyName)
        {
            var instanceValidator = CreateValidatorForInstance(instance);
            if(instanceValidator == null)
                yield break;

            var descriptor = instanceValidator.CreateDescriptor();
            var propertyValue = instance.GetType().GetProperty(propertyName).GetValue(instance, null);
            var validators = descriptor.GetValidatorsForMember(propertyName);
            var context = new PropertyValidatorContext(propertyName, instance, propertyValue, propertyName);

            foreach(var validator in validators)
            {
                var failures = validator.Validate(context);

                foreach(var failure in failures)
                {
                    yield return new ErrorAdapter(instance, failure);
                }
            }
        }

        /// <summary>
        /// Creates the validator for instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>The validator or null if none is found.</returns>
        protected IValidator CreateValidatorForInstance(object instance)
        {
            return instance == null ? null : GetValidator(instance.GetType());
        }
    }
}<commit_msg>v2.0: Partial application of a patch from Katokay which adds the IProxy check to the FluentValidationValidator.<commit_after>﻿namespace Caliburn.FluentValidation
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Reflection;
    using Core.Validation;
    using global::FluentValidation;
    using global::FluentValidation.Validators;
    using Microsoft.Practices.ServiceLocation;
    using IValidator = global::FluentValidation.IValidator;
	using Caliburn.Core.Behaviors;

    /// <summary>
    /// An implementation of <see cref="Core.Validation.IValidator"/> that uses FluentValidation.
    /// </summary>
    public class FluentValidationValidator : ValidatorFactoryBase, Core.Validation.IValidator
    {
        private readonly IServiceLocator _serviceLocator;

        /// <summary>
        /// Initializes a new instance of the <see cref="FluentValidationValidator"/> class.
        /// </summary>
        /// <param name="serviceLocator">The service locator.</param>
        public FluentValidationValidator(IServiceLocator serviceLocator)
        {
            _serviceLocator = serviceLocator;
        }

        /// <summary>
        /// Creates the instance.
        /// </summary>
        /// <param name="validatorType">Type of the validator.</param>
        /// <returns></returns>
        public override IValidator CreateInstance(Type validatorType)
        {
            return _serviceLocator.GetAllInstances(validatorType)
                .OfType<IValidator>()
                .FirstOrDefault();
        }

        /// <summary>
        /// Inidcates whether the specified property should be validated.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <returns>
        /// true if should be validated; otherwise false
        /// </returns>
        public bool ShouldValidate(PropertyInfo property)
        {
            var validator = GetValidator(property.DeclaringType);

            return validator != null && validator.CreateDescriptor().GetValidatorsForMember(property.Name).Any();
        }

        /// <summary>
        /// Validates the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>The validation errors.</returns>
        public IEnumerable<IError> Validate(object instance)
        {
            var validator = CreateValidatorForInstance(instance);
            if (validator == null)
                yield break;

            var result = validator.Validate(instance);

            foreach(var failure in result.Errors)
            {
                yield return new ErrorAdapter(instance, failure);
            }
        }

        /// <summary>
        /// Validates the specified property on the instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>The validation errors.</returns>
        public IEnumerable<IError> Validate(object instance, string propertyName)
        {
            var instanceValidator = CreateValidatorForInstance(instance);
            if(instanceValidator == null)
                yield break;

            var descriptor = instanceValidator.CreateDescriptor();
            var propertyValue = instance.GetType().GetProperty(propertyName).GetValue(instance, null);
            var validators = descriptor.GetValidatorsForMember(propertyName);
            var context = new PropertyValidatorContext(propertyName, instance, propertyValue, propertyName);

            foreach(var validator in validators)
            {
                var failures = validator.Validate(context);

                foreach(var failure in failures)
                {
                    yield return new ErrorAdapter(instance, failure);
                }
            }
        }

        /// <summary>
        /// Creates the validator for instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>The validator or null if none is found.</returns>
        protected IValidator CreateValidatorForInstance(object instance)
        {
            var proxy = instance as IProxy;
            return instance == null ? null : GetValidator(proxy == null ? instance.GetType() : proxy.OriginalType);
        }
    }
}
{'fraction_non_alphanumeric': 0.1047419317255039, 'fraction_numerical': 0.0002330187580100198, 'mean_word_length': 2.065714285714286, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3832090', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1979, 'n_words': 509}