<commit_before>﻿using Serenity.Data;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Serenity.Services
{
    public static class DataValidation
    {
        public static void AutoTrim(Row row, StringField stringField)
        {
            if ((stringField.Flags & FieldFlags.Trim) == FieldFlags.Trim)
            {
                string value = stringField[row];

                if ((stringField.Flags & FieldFlags.TrimToEmpty) == FieldFlags.TrimToEmpty)
                    value = value.TrimToEmpty();
                else // TrimToNull
                    value = value.TrimToNull();

                stringField[row] = value;
            }
        }

        public static void ValidateRequired(this Row row, Field field)
        {
            var str = field as StringField;
            if (!ReferenceEquals(null, str) &&
                str[row].IsTrimmedEmpty())
            { 
                throw RequiredError(row, field);
            }
        }

        public static void ValidateRequired(this Row row, IEnumerable<Field> fields)
        {
            foreach (var field in fields)
                ValidateRequired(row, field);
        }

        public static void ValidateRequiredIfModified(this Row row, IEnumerable<Field> fields)
        {
            foreach (var field in fields)
                if (row.IsAssigned(field))
                    ValidateRequired(row, field);
        }

        public static void EnsureUniversalTime(this Row row, DateTimeField field)
        {
            if (!field.IsNull(row))
                field[row] = field[row].Value.ToUniversalTime();
        }

        public static void ValidateEnum(this Row row, Field field, Type enumType)
        {
            if (!Enum.IsDefined(enumType, field.AsObject(row)))<commit_msg>make sure required field validation server side also runs for non-string fields<commit_after>﻿using Serenity.Data;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Serenity.Services
{
    public static class DataValidation
    {
        public static void AutoTrim(Row row, StringField stringField)
        {
            if ((stringField.Flags & FieldFlags.Trim) == FieldFlags.Trim)
            {
                string value = stringField[row];

                if ((stringField.Flags & FieldFlags.TrimToEmpty) == FieldFlags.TrimToEmpty)
                    value = value.TrimToEmpty();
                else // TrimToNull
                    value = value.TrimToNull();

                stringField[row] = value;
            }
        }

        public static void ValidateRequired(this Row row, Field field)
        {
            var str = field as StringField;
            if ((!ReferenceEquals(null, str) && str[row].IsTrimmedEmpty()) ||
                (ReferenceEquals(null, str) && field.AsObject(row) == null))
            { 
                throw RequiredError(row, field);
            }
        }

        public static void ValidateRequired(this Row row, IEnumerable<Field> fields)
        {
            foreach (var field in fields)
                ValidateRequired(row, field);
        }

        public static void ValidateRequiredIfModified(this Row row, IEnumerable<Field> fields)
        {
            foreach (var field in fields)
                if (row.IsAssigned(field))
                    ValidateRequired(row, field);
        }

        public static void EnsureUniversalTime(this Row row, DateTimeField field)
        {
            if (!field.IsNull(row))
                field[row] = field[row].Value.ToUniversalTime();
        }

        public static void ValidateEnum(this Row row, Field field, Type enumType)
        {
            if (!Enum.IsDefined(enumType, field.AsObject(row)))
{'fraction_non_alphanumeric': 0.08676041390289202, 'fraction_numerical': 0.0, 'mean_word_length': 1.8282070517629407, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4673396', 'n_tokens_mistral': 969, 'n_tokens_neox': 924, 'n_words': 248}