public void IsValidTests() {
            // Arrange
            var attribute = new CreditCardAttribute();

            // Act & Assert
            Assert.IsTrue(attribute.IsValid(null));                  // Optional values are always valid
            Assert.IsTrue(attribute.IsValid("0000000000000000"));    // Simplest valid value
            Assert.IsTrue(attribute.IsValid("1234567890123452"));    // Good checksum
            Assert.IsTrue(attribute.IsValid("1234-5678-9012-3452")); // Good checksum, with dashes
            Assert.IsFalse(attribute.IsValid("0000000000000001"));   // Bad checksum
            Assert.IsFalse(attribute.IsValid(0));                    // Non-string
        }
Example #2
0
 private static void ApplyCreditCardAttribute(OpenApiSchema schema, CreditCardAttribute creditCardAttribute)
 {
     schema.Format = "credit-card";
 }
 public static void DataType_CustomDataType()
 {
     var attribute = new CreditCardAttribute();
     Assert.Equal(DataType.CreditCard, attribute.DataType);
     Assert.Null(attribute.CustomDataType);
 }
        private void LoadAttributes(ModelMetadata metadata)
        {
            //TO-DO: Refazer os métodos para tornar-los mais dinâmicos...

            if (metadata != null)
            {
                MetadataAttribute commonAttribute = new MetadataAttribute()
                {
                    AttributeName = "Common" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "DisplayName", metadata.DisplayName },
                        { "ShortDisplayName", metadata.ShortDisplayName },
                        { "IsRequired", metadata.IsRequired },
                        { "IsReadOnly", metadata.IsReadOnly },
                        { "IsNullableValueType", metadata.IsNullableValueType },
                        { "Description", metadata.Description },
                        { "Watermark", metadata.Watermark },
                        { "ShowForDisplay", metadata.ShowForDisplay },
                        { "ShowForEdit", metadata.ShowForEdit },

                        { "DataTypeName", metadata.DataTypeName },
                        { "IsComplexType", metadata.IsComplexType },
                        { "EditFormatString", metadata.EditFormatString },
                        { "HideSurroundingHtml", metadata.HideSurroundingHtml },
                        { "HtmlEncode", metadata.HtmlEncode },
                        { "ConvertEmptyStringToNull", metadata.ConvertEmptyStringToNull },
                        { "NullDisplayText", metadata.NullDisplayText },
                        { "SimpleDisplayText", metadata.SimpleDisplayText },
                        { "TemplateHint", metadata.TemplateHint },
                        { "DisplayFormatString", metadata.DisplayFormatString },
                    }
                };
                metadataAttributes.Add(commonAttribute);
            }

            HtmlAttributesAttribute htmlAttributesAttribute = GetModelMetadataAttributes(metadata).OfType <HtmlAttributesAttribute>().FirstOrDefault();

            if (htmlAttributesAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "HtmlAttributes" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "ID", htmlAttributesAttribute.ID },
                        { "Name", htmlAttributesAttribute.Name },
                        { "Class", htmlAttributesAttribute.Class },
                        { "Style", htmlAttributesAttribute.Style },
                        { "Width", htmlAttributesAttribute.Width },
                        { "Height", htmlAttributesAttribute.Height },
                        { "Placeholder", htmlAttributesAttribute.Placeholder },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            DataTypeAttribute dataTypeAttribute = GetModelMetadataAttributes(metadata).OfType <DataTypeAttribute>().FirstOrDefault();

            if (dataTypeAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "DataType" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "DataType", dataTypeAttribute.DataType },
                        { "ErrorMessage", dataTypeAttribute.ErrorMessage },
                        { "ErrorMessageResourceName", dataTypeAttribute.ErrorMessageResourceName },
                        { "RequiresValidationContext", dataTypeAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            DataTypeFieldAttribute dataTypeFieldAttribute = GetModelMetadataAttributes(metadata).OfType <DataTypeFieldAttribute>().FirstOrDefault();

            if (dataTypeFieldAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "DataTypeField" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "DataType", dataTypeFieldAttribute.DataType },
                        { "ErrorMessage", dataTypeFieldAttribute.ErrorMessage },
                        { "ErrorMessageResourceName", dataTypeFieldAttribute.ErrorMessageResourceName },
                        { "RequiresValidationContext", dataTypeFieldAttribute.RequiresValidationContext },
                        { "Cols", dataTypeFieldAttribute.Cols },
                        { "Rows", dataTypeFieldAttribute.Rows },
                        { "Wrap", (dataTypeFieldAttribute.HardWrap ? "hard" : null) },
                        { "MinLength", dataTypeFieldAttribute.MinLength },
                        { "MaxLength", dataTypeFieldAttribute.MaxLength },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            RegularExpressionAttribute regularExpressionAttribute = GetModelMetadataAttributes(metadata).OfType <RegularExpressionAttribute>().FirstOrDefault();

            if (regularExpressionAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "RegularExpression" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "Pattern", regularExpressionAttribute.Pattern },
                        { "ErrorMessage", regularExpressionAttribute.ErrorMessage },
                        { "ErrorMessageResourceName", regularExpressionAttribute.ErrorMessageResourceName },
                        { "RequiresValidationContext", regularExpressionAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            StringLengthAttribute stringLengthAttribute = GetModelMetadataAttributes(metadata).OfType <StringLengthAttribute>().FirstOrDefault();

            if (stringLengthAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "StringLength" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "MinimumLength", stringLengthAttribute.MinimumLength },
                        { "MaximumLength", stringLengthAttribute.MaximumLength },
                        { "ErrorMessage", stringLengthAttribute.ErrorMessage },
                        { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) },
                        { "ErrorMessageResourceName", stringLengthAttribute.ErrorMessageResourceName },
                        { "RequiresValidationContext", stringLengthAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            MinLengthAttribute minLengthAttribute = GetModelMetadataAttributes(metadata).OfType <MinLengthAttribute>().FirstOrDefault();

            if (minLengthAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "MinLength" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "Length", minLengthAttribute.Length },
                        { "TypeId", minLengthAttribute.TypeId },
                        { "ErrorMessage", minLengthAttribute.ErrorMessage },
                        { "ErrorMessageResourceName", minLengthAttribute.ErrorMessageResourceName },
                        { "RequiresValidationContext", minLengthAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            MaxLengthAttribute maxLengthAttribute = GetModelMetadataAttributes(metadata).OfType <MaxLengthAttribute>().FirstOrDefault();

            if (maxLengthAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "MaxLength" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "Length", maxLengthAttribute.Length },
                        { "TypeId", maxLengthAttribute.TypeId },
                        { "ErrorMessage", maxLengthAttribute.ErrorMessage },
                        { "ErrorMessageResourceName", maxLengthAttribute.ErrorMessageResourceName },
                        { "RequiresValidationContext", maxLengthAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            DisplayAttribute displayAttribute = GetModelMetadataAttributes(metadata).OfType <DisplayAttribute>().FirstOrDefault();

            if (displayAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "Display" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "ShortName", displayAttribute.ShortName },
                        { "Name", displayAttribute.Name },
                        { "Prompt", displayAttribute.Prompt },
                        { "GroupName", displayAttribute.GroupName },
                        { "Description", displayAttribute.Description },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            RequiredAttribute requiredAttribute = GetModelMetadataAttributes(metadata).OfType <RequiredAttribute>().FirstOrDefault();

            if (requiredAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "Required" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "IsRequired", true },
                        { "AllowEmptyStrings", requiredAttribute.AllowEmptyStrings },
                        { "ErrorMessage", requiredAttribute.ErrorMessage },
                        { "ErrorMessageResourceName", requiredAttribute.ErrorMessageResourceName },
                        { "RequiresValidationContext", requiredAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            RangeAttribute rangeAttribute = GetModelMetadataAttributes(metadata).OfType <RangeAttribute>().FirstOrDefault();

            if (rangeAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "Range" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "OperandType", rangeAttribute.OperandType },
                        { "AllowEmptyStrings", rangeAttribute.Minimum },
                        { "Maximum", rangeAttribute.Maximum },
                        { "ErrorMessage", rangeAttribute.ErrorMessage },
                        { "ErrorMessageResourceName", rangeAttribute.ErrorMessageResourceName },
                        { "RequiresValidationContext", rangeAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            DisplayFormatAttribute displayFormatAttribute = GetModelMetadataAttributes(metadata).OfType <DisplayFormatAttribute>().FirstOrDefault();

            if (displayFormatAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "DisplayFormat" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "DataFormatString", displayFormatAttribute.DataFormatString },
                        { "ApplyFormatInEditMode", displayFormatAttribute.ApplyFormatInEditMode },
                        { "ConvertEmptyStringToNull", displayFormatAttribute.ConvertEmptyStringToNull },
                        { "HtmlEncode", displayFormatAttribute.HtmlEncode },
                        { "NullDisplayText", displayFormatAttribute.NullDisplayText },
                        { "IsDefault" + "Attribute", displayFormatAttribute.IsDefaultAttribute() },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            CreditCardAttribute creditCardAttribute = GetModelMetadataAttributes(metadata).OfType <CreditCardAttribute>().FirstOrDefault();

            if (creditCardAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "CreditCard" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "DataType", creditCardAttribute.DataType },
                        { "CustomDataType", creditCardAttribute.CustomDataType },
                        { "DisplayFormat", creditCardAttribute.DisplayFormat },
                        { "ErrorMessage", creditCardAttribute.ErrorMessage },
                        { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) },
                        { "ErrorMessageResourceName", creditCardAttribute.ErrorMessageResourceName },
                        { "ErrorMessageResourceType", creditCardAttribute.ErrorMessageResourceType },
                        { "RequiresValidationContext", creditCardAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            CustomValidationAttribute customValidationAttribute = GetModelMetadataAttributes(metadata).OfType <CustomValidationAttribute>().FirstOrDefault();

            if (customValidationAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "CustomValidation" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "ValidatorType", customValidationAttribute.ValidatorType },
                        { "Method", customValidationAttribute.Method },
                        { "ErrorMessage", creditCardAttribute.ErrorMessage },
                        { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) },
                        { "ErrorMessageResourceName", creditCardAttribute.ErrorMessageResourceName },
                        { "ErrorMessageResourceType", creditCardAttribute.ErrorMessageResourceType },
                        { "RequiresValidationContext", creditCardAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            EmailAddressAttribute emailAddressAttribute = GetModelMetadataAttributes(metadata).OfType <EmailAddressAttribute>().FirstOrDefault();

            if (emailAddressAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "EmailAddress" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "DataType", emailAddressAttribute.DataType },
                        { "CustomDataType", emailAddressAttribute.CustomDataType },
                        { "DisplayFormat", emailAddressAttribute.DisplayFormat },
                        { "ErrorMessage", creditCardAttribute.ErrorMessage },
                        { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) },
                        { "ErrorMessageResourceName", creditCardAttribute.ErrorMessageResourceName },
                        { "ErrorMessageResourceType", creditCardAttribute.ErrorMessageResourceType },
                        { "RequiresValidationContext", creditCardAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            FileExtensionsAttribute fileExtensionsAttribute = GetModelMetadataAttributes(metadata).OfType <FileExtensionsAttribute>().FirstOrDefault();

            if (fileExtensionsAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "FileExtensions" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "DataType", emailAddressAttribute.DataType },
                        { "CustomDataType", emailAddressAttribute.CustomDataType },
                        { "DisplayFormat", emailAddressAttribute.DisplayFormat },
                        { "ErrorMessage", creditCardAttribute.ErrorMessage },
                        { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) },
                        { "ErrorMessageResourceName", creditCardAttribute.ErrorMessageResourceName },
                        { "ErrorMessageResourceType", creditCardAttribute.ErrorMessageResourceType },
                        { "RequiresValidationContext", creditCardAttribute.RequiresValidationContext },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            TimestampAttribute timestampAttribute = GetModelMetadataAttributes(metadata).OfType <TimestampAttribute>().FirstOrDefault();

            if (timestampAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "FileExtensions" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "TypeId", timestampAttribute.TypeId },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            ViewDisabledAttribute viewDisabledAttribute = GetModelMetadataAttributes(metadata).OfType <ViewDisabledAttribute>().FirstOrDefault();

            if (viewDisabledAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "ViewDisabled" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "Declared", true },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            TextAreaAttribute textAreaAttribute = GetModelMetadataAttributes(metadata).OfType <TextAreaAttribute>().FirstOrDefault();

            if (textAreaAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "TextArea" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "Cols", textAreaAttribute.Cols },
                        { "Rows", textAreaAttribute.Rows },
                        { "Wrap", (textAreaAttribute.HardWrap ? "hard" : null) },
                        { "MinLength", textAreaAttribute.MinLength },
                        { "MaxLength", textAreaAttribute.MaxLength },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            OnlyNumberAttribute onlyNumberAttribute = GetModelMetadataAttributes(metadata).OfType <OnlyNumberAttribute>().FirstOrDefault();

            if (onlyNumberAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "OnlyNumber" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "Declared", true },
                        { "ClassDecorator", "onlyNumber" },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            CurrencyAttribute currencyAttribute = GetModelMetadataAttributes(metadata).OfType <CurrencyAttribute>().FirstOrDefault();

            if (currencyAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "Currency" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "Declared", true },
                        { "ClassDecorator", "onlyNumber" },
                        { "Pattern", "currency" },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            NoEspecialCharsAttribute noEspecialCharsAttribute = GetModelMetadataAttributes(metadata).OfType <NoEspecialCharsAttribute>().FirstOrDefault();

            if (noEspecialCharsAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "NoEspecialChars" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "Declared", true },
                        { "ClassDecorator", "noCaracEsp" },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            ProgressAttribute progressAttribute = GetModelMetadataAttributes(metadata).OfType <ProgressAttribute>().FirstOrDefault();

            if (progressAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "Progress" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "MinValue", progressAttribute.MinValue },
                        { "MaxValue", progressAttribute.MaxValue },
                        { "Step", progressAttribute.Step },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }

            PlaceHolderAttribute placeHolderAttribute = GetModelMetadataAttributes(metadata).OfType <PlaceHolderAttribute>().FirstOrDefault();

            if (placeHolderAttribute != null)
            {
                MetadataAttribute metaAttribute = new MetadataAttribute()
                {
                    AttributeName = "PlaceHolder" + "Attribute",
                    Property      = new Dictionary <string, object>()
                    {
                        { "Declared", true },
                        { "Text", placeHolderAttribute.Text },
                    }
                };
                metadataAttributes.Add(metaAttribute);
            }
        }
        public void ClientRule() {
            // Arrange
            var attribute = new CreditCardAttribute();
            var provider = new Mock<ModelMetadataProvider>();
            var metadata = new ModelMetadata(provider.Object, null, null, typeof(string), "PropertyName");

            // Act
            ModelClientValidationRule clientRule = attribute.GetClientValidationRules(metadata, null).Single();

            // Assert
            Assert.AreEqual("creditcard", clientRule.ValidationType);
            Assert.AreEqual("The PropertyName field is not a valid credit card number.", clientRule.ErrorMessage);
            Assert.AreEqual(0, clientRule.ValidationParameters.Count);
        }