Exemplo n.º 1
0
        /// <summary>
        /// Method to create a deep or shallow copy of this object
        /// </summary>
        public EntryValidation Clone(bool deep)
        {
            // All value types can be simply copied
            var copy = new EntryValidation
            {
                Minimum    = Minimum,
                Maximum    = Maximum,
                Regex      = Regex,
                IsRequired = IsRequired
            };

            return(copy);
        }
Exemplo n.º 2
0
        /// <see cref="ICustomSerialization"/>
        public virtual EntryValidation CreateValidation(Type memberType, ICustomAttributeProvider attributeProvider)
        {
            var validation = new EntryValidation();

            var validationAttributes = attributeProvider.GetCustomAttributes <ValidationAttribute>();

            if (validationAttributes.Length == 0)
            {
                return(validation);
            }

            // Iterate over attributes reading all validation rules
            foreach (var attribute in validationAttributes)
            {
                if (attribute is MinLengthAttribute minAttribute)
                {
                    validation.Minimum = minAttribute.Length;
                }
                else if (attribute is MaxLengthAttribute maxAttribute)
                {
                    validation.Maximum = maxAttribute.Length;
                }
                else if (attribute is RangeAttribute rangeAttribute)
                {
                    validation.Minimum = Convert.ToDouble(rangeAttribute.Minimum);
                    validation.Maximum = Convert.ToDouble(rangeAttribute.Maximum);
                }
                else if (attribute is RegularExpressionAttribute regexAttribute)
                {
                    validation.Regex = regexAttribute.Pattern;
                }
                else if (attribute is StringLengthAttribute strLength)
                {
                    validation.Minimum = strLength.MinimumLength;
                    validation.Maximum = strLength.MaximumLength;
                }
                else if (attribute is RequiredAttribute)
                {
                    validation.IsRequired = true;
                }
            }

            return(validation);
        }