private RegularExpressionRule MakeDigitsRule(DigitsAttribute att) { if (att == null) throw new ArgumentNullException("att"); string pattern; if (att.FractionalDigits < 1) pattern = string.Format(@"\d{{0,{0}}}", att.IntegerDigits); else pattern = string.Format(@"\d{{0,{0}}}(\.\d{{1,{1}}})?", att.IntegerDigits, att.FractionalDigits); return new RegularExpressionRule(pattern); }
public DigitsAttributeTests() { attribute = new DigitsAttribute(); }
private static string WriteExtenders(PropertyInfo p, out bool isValidatable) { isValidatable = false; StringBuilder sb = new StringBuilder(); sb.Append(".extend({ editState : false, disableValidation : false, empty : true })"); RequiredAttribute requiredAttribute = p.GetCustomAttribute <RequiredAttribute>(); if (requiredAttribute != null) { sb.Append(".extend({ required: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } })".FormatString(requiredAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } MinLengthAttribute minLengthAttribute = p.GetCustomAttribute <MinLengthAttribute>(); if (minLengthAttribute != null) { sb.Append(".extend({ minLength: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })".FormatString(minLengthAttribute.FormatErrorMessage(p.Name), minLengthAttribute.Length, p.Name)); isValidatable = true; } MaxLengthAttribute maxLengthAttribute = p.GetCustomAttribute <MaxLengthAttribute>(); if (maxLengthAttribute != null) { sb.Append(".extend({ maxLength: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })".FormatString(maxLengthAttribute.FormatErrorMessage(p.Name), maxLengthAttribute.Length, p.Name)); isValidatable = true; } RegularExpressionAttribute regularExpressionAttribute = p.GetCustomAttribute <RegularExpressionAttribute>(); if (regularExpressionAttribute != null) { sb.Append(".extend({ pattern: { message: \"{0}\", params: /{1}/, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })".FormatString(regularExpressionAttribute.FormatErrorMessage(p.Name), regularExpressionAttribute.Pattern, p.Name)); isValidatable = true; } UrlAttribute urlAttribute = p.GetCustomAttribute <UrlAttribute>(); if (urlAttribute != null) { sb.Append(".extend({ pattern: { message: \"{0}\", params: /{1}/, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })".FormatString(urlAttribute.FormatErrorMessage(p.Name), urlAttribute.Regex, p.Name)); isValidatable = true; } DateAttribute dateAttribute = p.GetCustomAttribute <DateAttribute>(); if (dateAttribute != null) { sb.Append(".extend({ date: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } })".FormatString(dateAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } NumericAttribute numericAttribute = p.GetCustomAttribute <NumericAttribute>(); if (numericAttribute != null) { sb.Append( ".extend({ number: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } }).extend({ numeric: {2} })".FormatString( numericAttribute.FormatErrorMessage(p.Name), p.Name, numericAttribute.Precision)); isValidatable = true; } EmailAttribute emailAttribute = p.GetCustomAttribute <EmailAttribute>(); if (emailAttribute != null) { sb.Append(".extend({ email: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } })".FormatString(emailAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } DigitsAttribute digitsAttribute = p.GetCustomAttribute <DigitsAttribute>(); if (digitsAttribute != null) { sb.Append(".extend({ digit: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } }).extend({ numeric: 0 })".FormatString(digitsAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } MinAttribute minAttribute = p.GetCustomAttribute <MinAttribute>(); if (minAttribute != null) { sb.Append(".extend({ min: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })".FormatString(minAttribute.FormatErrorMessage(p.Name), minAttribute.Min, p.Name)); isValidatable = true; } MaxAttribute maxAttribute = p.GetCustomAttribute <MaxAttribute>(); if (maxAttribute != null) { sb.Append(".extend({ max: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })".FormatString(maxAttribute.FormatErrorMessage(p.Name), maxAttribute.Max, p.Name)); isValidatable = true; } EqualToAttribute equalToAttribute = p.GetCustomAttribute <EqualToAttribute>(); if (equalToAttribute != null) { sb.Append(".extend({ equal: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })".FormatString(equalToAttribute.FormatErrorMessage(p.Name), equalToAttribute.OtherProperty, p.Name)); isValidatable = true; } CompareAttribute compareAttribute = p.GetCustomAttribute <CompareAttribute>(); if (compareAttribute != null) { sb.Append(".extend({ equal: { message: \"{0}\", params: \"{1}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })".FormatString(compareAttribute.FormatErrorMessage(p.Name), compareAttribute.OtherProperty, p.Name)); isValidatable = true; } FormatterAttribute formatterAttribute = p.GetCustomAttribute <FormatterAttribute>(); if (formatterAttribute != null) { IJsonSerializer serializer = Container.Get <IJsonSerializer>(); sb.Append(".formatted({0}, {1})".FormatString(formatterAttribute.Formatter, serializer.Serialize(formatterAttribute.Arguments))); } return(sb.ToString()); }
public void KnownRulesConvertAssing() { NhvMapping map = XmlMappingLoader.GetXmlMappingFor(typeof(WellKnownRules)); NhvmClass cm = map.@class[0]; XmlClassMapping rm = new XmlClassMapping(cm); MemberInfo mi; List <Attribute> attributes; mi = typeof(WellKnownRules).GetField("AP"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); Assert.AreEqual("A string value", ((ACustomAttribute)attributes[0]).Value1); Assert.AreEqual(123, ((ACustomAttribute)attributes[0]).Value2); Assert.AreEqual("custom message", ((ACustomAttribute)attributes[0]).Message); mi = typeof(WellKnownRules).GetField("StrProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); NotEmptyAttribute nea = FindAttribute <NotEmptyAttribute>(attributes); Assert.AreEqual("not-empty message", nea.Message); NotNullAttribute nna = FindAttribute <NotNullAttribute>(attributes); Assert.AreEqual("not-null message", nna.Message); NotNullNotEmptyAttribute nnea = FindAttribute <NotNullNotEmptyAttribute>(attributes); Assert.AreEqual("notnullnotempty message", nnea.Message); LengthAttribute la = FindAttribute <LengthAttribute>(attributes); Assert.AreEqual("length message", la.Message); Assert.AreEqual(1, la.Min); Assert.AreEqual(10, la.Max); PatternAttribute pa = FindAttribute <PatternAttribute>(attributes); Assert.AreEqual("pattern message", pa.Message); Assert.AreEqual("[0-9]+", pa.Regex); Assert.AreEqual(RegexOptions.Compiled, pa.Flags); EmailAttribute ea = FindAttribute <EmailAttribute>(attributes); Assert.AreEqual("email message", ea.Message); IPAddressAttribute ipa = FindAttribute <IPAddressAttribute>(attributes); Assert.AreEqual("ipAddress message", ipa.Message); EANAttribute enaa = FindAttribute <EANAttribute>(attributes); Assert.AreEqual("ean message", enaa.Message); CreditCardNumberAttribute ccna = FindAttribute <CreditCardNumberAttribute>(attributes); Assert.AreEqual("creditcardnumber message", ccna.Message); IBANAttribute iban = FindAttribute <IBANAttribute>(attributes); Assert.AreEqual("iban message", iban.Message); mi = typeof(WellKnownRules).GetField("DtProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); FutureAttribute fa = FindAttribute <FutureAttribute>(attributes); Assert.AreEqual("future message", fa.Message); PastAttribute psa = FindAttribute <PastAttribute>(attributes); Assert.AreEqual("past message", psa.Message); mi = typeof(WellKnownRules).GetField("DecProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); DigitsAttribute dga = FindAttribute <DigitsAttribute>(attributes); Assert.AreEqual("digits message", dga.Message); Assert.AreEqual(5, dga.IntegerDigits); Assert.AreEqual(2, dga.FractionalDigits); MinAttribute mina = FindAttribute <MinAttribute>(attributes); Assert.AreEqual("min message", mina.Message); Assert.AreEqual(100, mina.Value); MaxAttribute maxa = FindAttribute <MaxAttribute>(attributes); Assert.AreEqual("max message", maxa.Message); Assert.AreEqual(200, maxa.Value); DecimalMaxAttribute decimalmaxa = FindAttribute <DecimalMaxAttribute>(attributes); Assert.AreEqual("decimal max message", decimalmaxa.Message); Assert.AreEqual(200.1m, decimalmaxa.Value); DecimalMinAttribute decimalmina = FindAttribute <DecimalMinAttribute>(attributes); Assert.AreEqual("decimal min message", decimalmina.Message); Assert.AreEqual(99.9m, decimalmina.Value); mi = typeof(WellKnownRules).GetField("BProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); AssertTrueAttribute ata = FindAttribute <AssertTrueAttribute>(attributes); Assert.AreEqual("asserttrue message", ata.Message); AssertFalseAttribute afa = FindAttribute <AssertFalseAttribute>(attributes); Assert.AreEqual("assertfalse message", afa.Message); mi = typeof(WellKnownRules).GetField("ArrProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); SizeAttribute sa = FindAttribute <SizeAttribute>(attributes); Assert.AreEqual("size message", sa.Message); Assert.AreEqual(2, sa.Min); Assert.AreEqual(9, sa.Max); mi = typeof(WellKnownRules).GetField("Pattern"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); PatternAttribute spa = FindAttribute <PatternAttribute>(attributes); Assert.AreEqual("{validator.pattern}", spa.Message); Assert.AreEqual(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", spa.Regex); Assert.AreEqual(RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, spa.Flags); }