<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Assisticant.Validation
{
    public class ValidationRules : IDisposable
    {
        public delegate void ErrorsChangedDelegate(string propertyName);

        public event ErrorsChangedDelegate ErrorsChanged;

        private Dictionary<string, PropertyValidator> _validatorByPropertyName =
            new Dictionary<string, PropertyValidator>();

        public PropertyValidator AddPropertyValidator<T>(Expression<Func<T>> property)
        {
            var body = (MemberExpression)property.Body;
            var propertyName = body.Member.Name;
            var function = property.Compile();
            var propertyValidator = new PropertyValidator(propertyName, () => function(), Notify);
            _validatorByPropertyName.Add(propertyName, propertyValidator);
            return propertyValidator;
        }

        private void Notify(string propertyName)
        {
            ErrorsChanged?.Invoke(propertyName);
        }

        public void Dispose()
        {
            foreach (var propertyValidator in _validatorByPropertyName.Values)
                propertyValidator.Dispose();
        }

        public bool HasErrors => _validatorByPropertyName.Values
            .Any(p => p.ValidationErrors.Any());

        public IEnumerable<string> GetErrors(string propertyName)<commit_msg>Signature for generic validator.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Assisticant.Validation
{
    public class ValidationRules : IDisposable
    {
        public delegate void ErrorsChangedDelegate(string propertyName);

        public event ErrorsChangedDelegate ErrorsChanged;

        private Dictionary<string, PropertyValidator> _validatorByPropertyName =
            new Dictionary<string, PropertyValidator>();

        public PropertyValidator AddPropertyValidator<T>(Expression<Func<T>> property)
        {
            var body = (MemberExpression)property.Body;
            var propertyName = body.Member.Name;
            var function = property.Compile();
            var propertyValidator = new PropertyValidator(propertyName, () => function(), Notify);
            _validatorByPropertyName.Add(propertyName, propertyValidator);
            return propertyValidator;
        }

        public ValidationRules For<T>(Expression<Func<T>> property, Func<bool> predicate, Func<string> message)
        {
            var validator = AddPropertyValidator(property);
            validator.AddRule()
            return this;
        }

        private void Notify(string propertyName)
        {
            ErrorsChanged?.Invoke(propertyName);
        }

        public void Dispose()
        {
            foreach (var propertyValidator in _validatorByPropertyName.Values)
                propertyValidator.Dispose();
        }

        public bool HasErrors => _validatorByPropertyName.Values
            .Any(p => p.ValidationErrors.Any());

        public IEnumerable<string> GetErrors(string propertyName)
{'fraction_non_alphanumeric': 0.07814992025518341, 'fraction_numerical': 0.0, 'mean_word_length': 2.751196172248804, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 26, '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': '4613785', 'n_tokens_mistral': 730, 'n_tokens_neox': 713, 'n_words': 170}