<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;

namespace Umbraco.Web
{
    internal static class ModelStateExtensions
    {

        /// <summary>
        /// Merges ModelState that has names matching the prefix
        /// </summary>
        /// <param name="state"></param>
        /// <param name="dictionary"></param>
        /// <param name="prefix"></param>
        public static void Merge(this ModelStateDictionary state, ModelStateDictionary dictionary, string prefix)
        {
            if (dictionary == null)
                return;
            foreach (var keyValuePair in dictionary
                //It can either equal the prefix exactly (model level errors) or start with the prefix. (property level errors)
                .Where(keyValuePair => keyValuePair.Key == prefix || keyValuePair.Key.StartsWith(prefix + ".")))
            {
                state[keyValuePair.Key] = keyValuePair.Value;
            }
        }

        /// <summary>
        /// Checks if there are any model errors on any fields containing the prefix
        /// </summary>
        /// <param name="state"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static bool IsValid(this ModelStateDictionary state, string prefix)
        {
            return state.Where(v => v.Key.StartsWith(prefix + ".")).All(v => !v.Value.Errors.Any());
        }
        
        /// <summary>
        /// Adds the error to model state correctly for a property so we can use it on the client side.
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="result"></param>
        /// <param name="propertyAlias"></param>
        /// <param name="culture">The culture for the property, if the property is invariant than this is empty</param>
        internal static void AddPropertyError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
            ValidationResult result, string propertyAlias, string culture = "")
        {
            if (culture == null)
                culture = "";
            modelState.AddValidationError(result, "_Properties", propertyAlias,
                //if the culture is null, we'll add the term 'invariant' as part of the key
                culture.IsNullOrWhiteSpace() ? "invariant" : culture);
        }

        /// <summary>
        /// Adds a generic culture error for use in displaying the culture validation error in the save/publish/etc... dialogs
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="culture"></param>
        /// <param name="errMsg"></param>
        internal static void AddCultureValidationError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
            string culture, string errMsg)
        {
            var key = "_content_variant_" + culture + "_";
            if (modelState.ContainsKey(key)) return;
            modelState.AddModelError(key, errMsg);
        }

        /// <summary>
        /// Returns a list of cultures that have property validation errors errors
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="localizationService"></param>
        /// <param name="cultureForInvariantErrors">The culture to affiliate invariant errors with</param>
        /// <returns>
        /// A list of cultures that have property validation errors. The default culture will be returned for any invariant property errors.
        /// </returns>
        internal static IReadOnlyList<string> GetCulturesWithPropertyErrors(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
            ILocalizationService localizationService, string cultureForInvariantErrors)
        {
            //Add any culture specific errors here
            var cultureErrors = modelState.Keys<commit_msg>Update ModelStateExtensions.cs<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;

namespace Umbraco.Web
{
    internal static class ModelStateExtensions
    {

        /// <summary>
        /// Merges ModelState that has names matching the prefix
        /// </summary>
        /// <param name="state"></param>
        /// <param name="dictionary"></param>
        /// <param name="prefix"></param>
        public static void Merge(this ModelStateDictionary state, ModelStateDictionary dictionary, string prefix)
        {
            if (dictionary == null)
                return;
            foreach (var keyValuePair in dictionary
                //It can either equal the prefix exactly (model level errors) or start with the prefix. (property level errors)
                .Where(keyValuePair => keyValuePair.Key == prefix || keyValuePair.Key.StartsWith(prefix + ".")))
            {
                state[keyValuePair.Key] = keyValuePair.Value;
            }
        }

        /// <summary>
        /// Checks if there are any model errors on any fields containing the prefix
        /// </summary>
        /// <param name="state"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static bool IsValid(this ModelStateDictionary state, string prefix)
        {
            return state.Where(v => v.Key.StartsWith(prefix + ".")).All(v => !v.Value.Errors.Any());
        }
        
        /// <summary>
        /// Adds the error to model state correctly for a property so we can use it on the client side.
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="result"></param>
        /// <param name="propertyAlias"></param>
        /// <param name="culture">The culture for the property, if the property is invariant than this is empty</param>
        internal static void AddPropertyError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
            ValidationResult result, string propertyAlias, string culture = "")
        {
            if (culture == null)
                culture = "";
            modelState.AddValidationError(result, "_Properties", propertyAlias,
                //if the culture is null, we'll add the term 'invariant' as part of the key
                culture.IsNullOrWhiteSpace() ? "invariant" : culture);
        }

        /// <summary>
        /// Adds a generic culture error for use in displaying the culture validation error in the save/publish/etc... dialogs
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="culture"></param>
        /// <param name="errMsg"></param>
        internal static void AddCultureValidationError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
            string culture, string errMsg)
        {
            var key = "_content_variant_" + culture + "_";
            if (modelState.ContainsKey(key)) return;
            modelState.AddModelError(key, errMsg);
        }

        /// <summary>
        /// Returns a list of cultures that have property validation errors
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="localizationService"></param>
        /// <param name="cultureForInvariantErrors">The culture to affiliate invariant errors with</param>
        /// <returns>
        /// A list of cultures that have property validation errors. The default culture will be returned for any invariant property errors.
        /// </returns>
        internal static IReadOnlyList<string> GetCulturesWithPropertyErrors(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
            ILocalizationService localizationService, string cultureForInvariantErrors)
        {
            //Add any culture specific errors here
            var cultureErrors = modelState.Keys
{'fraction_non_alphanumeric': 0.11335480673934589, 'fraction_numerical': 0.0, 'mean_word_length': 2.2188995215311005, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 99, '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': '1675441', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1918, 'n_words': 605}