<commit_before>﻿using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stylet.Samples.ModelValidation.Pages
{
    public class UserViewModel : Screen
    {
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string PasswordConfirmation { get; set; }
        public Stringable<int> Age { get; set; }

        public bool AutoValidate
        {
            get { return this.autoValidate; }
            set { this.autoValidate = value; this.NotifyOfPropertyChange(); }
        }

        public UserViewModel(IModelValidator<UserViewModel> validator) : base(validator)
        {
        }

        protected override void OnValidationStateChanged(IEnumerable<string> changedProperties)
        {
            base.OnValidationStateChanged(changedProperties);
            // Fody can't weave other assemblies, so we have to manually raise this
            this.NotifyOfPropertyChange(() => this.CanSubmit);
        }

        public bool CanSubmit
        {
            get { return !this.AutoValidate || !this.HasErrors; }
        }
        public void Submit()
        {
            if (this.Validate())
                System.Windows.MessageBox.Show("Successfully submitted");
        }
    }

    public class UserViewModelValidator : AbstractValidator<UserViewModel>
    {
        public UserViewModelValidator()
        {
            RuleFor(x => x.UserName).NotEmpty().Length(1, 20);
            RuleFor(x => x.Email).NotEmpty().EmailAddress();
            RuleFor(x => x.Password).NotEmpty().Matches("[0-9]").WithMessage("{PropertyName} must contain a number");
            RuleFor(x => x.PasswordConfirmation).Equal(s => s.Password).WithMessage("{PropertyName} should match Password");
            RuleFor(x => x.Age).Must(x => x.IsValid).WithMessage("{PropertyName} must be a valid number");
            When(x => x.Age.IsValid, () =>
            {
                RuleFor(x => x.Age.Value).GreaterThan(0).WithName("Age");
            });
        }
    }
}<commit_msg>Update validation rules in Stylet.Samples.ModelValidation<commit_after>﻿using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stylet.Samples.ModelValidation.Pages
{
    public class UserViewModel : Screen
    {
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string PasswordConfirmation { get; set; }
        public Stringable<int> Age { get; set; }

        public bool AutoValidate
        {
            get { return this.autoValidate; }
            set { this.autoValidate = value; this.NotifyOfPropertyChange(); }
        }

        public UserViewModel(IModelValidator<UserViewModel> validator) : base(validator)
        {
        }

        protected override void OnValidationStateChanged(IEnumerable<string> changedProperties)
        {
            base.OnValidationStateChanged(changedProperties);
            // Fody can't weave other assemblies, so we have to manually raise this
            this.NotifyOfPropertyChange(() => this.CanSubmit);
        }

        public bool CanSubmit
        {
            get { return !this.AutoValidate || !this.HasErrors; }
        }
        public void Submit()
        {
            if (this.Validate())
                System.Windows.MessageBox.Show("Successfully submitted");
        }
    }

    public class UserViewModelValidator : AbstractValidator<UserViewModel>
    {
        public UserViewModelValidator()
        {
            RuleFor(x => x.UserName).NotEmpty().Length(1, 20);
            RuleFor(x => x.Email).NotEmpty().EmailAddress();
            RuleFor(x => x.Password).NotEmpty().Matches("[0-9]").WithMessage("{PropertyName} must contain a number");
            RuleFor(x => x.PasswordConfirmation).Equal(s => s.Password).WithMessage("{PropertyName} should match Password");
            RuleFor(x => x.Age).Must(x => x != null && x.IsValid).WithMessage("{PropertyName} must be a valid number");
            When(x => x.Age != null && x.Age.IsValid, () =>
            {
                RuleFor(x => x.Age.Value).GreaterThan(0).WithName("Age");
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.11421435059037238, 'fraction_numerical': 0.0027247956403269754, 'mean_word_length': 2.332072617246596, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, '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': '6959280', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1153, 'n_words': 260}