Esempio n. 1
0
        public static ValidatableField <F> Between <F>(this ValidatableField <F> field, F min, F max) where F : IComparable
        {
            if (field is null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (!field.Continue)
            {
                return(field);
            }

            if (field.Field.CompareTo(min) < 0)
            {
                field.IsValid = false;
                field.AddError(new Error(Error.Types.Warning, nameof(Between), $"Field '{field.FieldName}' must be more than {min}"));
            }

            if (field.Field.CompareTo(max) > 0)
            {
                field.IsValid = false;
                field.AddError(new Error(Error.Types.Warning, nameof(Between), $"Field '{field.FieldName}' must be less than {max}"));
            }

            return(field);
        }
Esempio n. 2
0
        public static ValidatableField <string> Match(this ValidatableField <string> field, string expression)
        {
            if (field is null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (!field.Continue)
            {
                return(field);
            }

            if (field.Field is null)
            {
                throw new ArgumentNullException(nameof(field));
            }
            if (expression is null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (!Regex.IsMatch(field.Field, expression))
            {
                field.IsValid = false;
                field.AddError(new Error(Error.Types.Warning, nameof(Match), $"Field '{field.FieldName}' doesn't match to '{expression}'"));
            }

            return(field);
        }
Esempio n. 3
0
        public static ValidatableField <F> Required <F>(this ValidatableField <F> field) where F : class
        {
            if (field is null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (!field.Continue)
            {
                return(field);
            }

            if (field.Field is null)
            {
                field.IsValid = false;
                field.AddError(new Error(Error.Types.Warning, nameof(Required), $"Field '{field.FieldName}' is required"));
            }

            return(field);
        }