public void PrincipalBalanceOutstandingAmountTest_SelfReportedLoanModel_check_for_regex() { SelfReportedLoanModel passingValueTwoDecimalPlaces = new SelfReportedLoanModel() { PrincipalBalanceOutstandingAmount = 100.01m }; SelfReportedLoanModel passingValueOneDecimalPlace = new SelfReportedLoanModel() { PrincipalBalanceOutstandingAmount = 100.1m }; SelfReportedLoanModel passingValueNoDecimalPlaces = new SelfReportedLoanModel() { PrincipalBalanceOutstandingAmount = 100m }; SelfReportedLoanModel failingValueImproperNumberOfDigits = new SelfReportedLoanModel() { PrincipalBalanceOutstandingAmount = 100.001m }; RegularExpressionAttribute regexCheck = new RegularExpressionAttribute(RegexStrings.CURRENCY); Assert.IsTrue(regexCheck.IsValid(passingValueTwoDecimalPlaces.PrincipalBalanceOutstandingAmount), "Assertion of positive case (2 decimal places) being true failed"); Assert.IsTrue(regexCheck.IsValid(passingValueNoDecimalPlaces.PrincipalBalanceOutstandingAmount), "Assertion of positive case (0 decimal places) being true failed"); Assert.IsTrue(regexCheck.IsValid(passingValueOneDecimalPlace.PrincipalBalanceOutstandingAmount), "Assertion of positive case (1 decimal place) being true failed"); Assert.IsFalse(regexCheck.IsValid(failingValueImproperNumberOfDigits.PrincipalBalanceOutstandingAmount), "Assertion of negative case (improper # of digits) case being false failed"); }
public void IsValid_NonStringValue() { RegularExpressionAttribute numericValueValidation = new RegularExpressionAttribute("1234"); Assert.IsTrue(numericValueValidation.IsValid(1234)); Assert.IsFalse(numericValueValidation.IsValid(20.10)); }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { try { Person contact = (Person)validationContext.ObjectInstance; if (contact.Category != Person.Supplier) { return(ValidationResult.Success); } RequiredAttribute required = new RequiredAttribute(); if (!required.IsValid(value)) { return(new ValidationResult($"Telephone is required for a {Person.Supplier}.")); } RegularExpressionAttribute regex = new RegularExpressionAttribute(Pattern); if (!regex.IsValid(value)) { return(new ValidationResult($"Telephone number should be at least 7 and no more than 12 digits.")); } } catch (Exception ex) { return(new ValidationResult("Somrthing wrong while validating Telephone Number!" + ex.Message)); } return(ValidationResult.Success); }
public string this[string columnName] { get { string error = String.Empty; switch (columnName) { case "Age": if ((Age < 0) || (Age > 100)) { error = "Возраст должен быть больше 0 и меньше 100"; } break; case "Name": if (!reg.IsValid(Name)) { error = "UUUUUUU"; } //Обработка ошибок для свойства Name break; case "Position": //Обработка ошибок для свойства Position break; } return(error); } }
public void EmptyGuidShouldFailRegex() { RegularExpressionAttribute attribute = new RegularExpressionAttribute(ExampleModel.EmptyGuidValidationRegex); Assert.False(attribute.IsValid(Guid.Empty)); }
public void IsCountryCodeValidTests() { var attribute = new RegularExpressionAttribute(Expressions.CountryCode); Assert.IsTrue(attribute.IsValid(null)); Assert.IsTrue(attribute.IsValid(string.Empty)); Assert.IsTrue(attribute.IsValid("AB")); Assert.IsTrue(attribute.IsValid("XYZ")); Assert.IsTrue(attribute.IsValid("123")); Assert.IsTrue(attribute.IsValid("789")); Assert.IsFalse(attribute.IsValid("A")); Assert.IsFalse(attribute.IsValid("WXYZ")); Assert.IsFalse(attribute.IsValid("12")); Assert.IsFalse(attribute.IsValid("6789")); }
public void Constructor_NullPattern() { var attr = new RegularExpressionAttribute(null); ExceptionHelper.ExpectException <InvalidOperationException>(delegate() { attr.IsValid(""); }, Resources.DataAnnotationsResources.RegularExpressionAttribute_Empty_Pattern); }
public void Constructor_InvalidPattern() { var attr = new RegularExpressionAttribute("("); // Cannot test the exception message, because on the non-developer runtime of Silverlight // the exception string is not available. ExceptionHelper.ExpectArgumentException(() => attr.IsValid(""), null); }
public override bool IsValid(object value) { if (value != null) { var regexValidator = new RegularExpressionAttribute(_pattern); if (!regexValidator.IsValid(value)) { return(false); } } return(true); }
public void LoanSelfReportedEntryIdTest_SelfReportedLoanModel_check_for_regex() { SelfReportedLoanModel passingValue = new SelfReportedLoanModel() { LoanSelfReportedEntryId = "B93F10EB-2BC0-47A2-B5C5-082A064E3099" }; SelfReportedLoanModel failingValueImproperNumberOfDigits = new SelfReportedLoanModel() { LoanSelfReportedEntryId = "B93F10EB-2BC0-47A2-B5C5-082A064E309" }; SelfReportedLoanModel failingValueImproperDigits = new SelfReportedLoanModel() { LoanSelfReportedEntryId = "!!3F10EB-2BC0-47A2-B5C5-082A064E309" }; RegularExpressionAttribute regexCheck = new RegularExpressionAttribute(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$"); Assert.IsTrue(regexCheck.IsValid(passingValue.LoanSelfReportedEntryId), "Assertion of positive case being true failed"); Assert.IsFalse(regexCheck.IsValid(failingValueImproperNumberOfDigits.LoanSelfReportedEntryId), "Assertion of negative case (improper # of digits) case being false failed"); Assert.IsFalse(regexCheck.IsValid(failingValueImproperDigits.LoanSelfReportedEntryId), "Assertion of negative case (improper characters) being false failed"); }
public void ReceivedYearTest_SelfReportedLoanModel_check_for_regex() { SelfReportedLoanModel passingValueLowerBound = new SelfReportedLoanModel() { ReceivedYear = 1900 }; SelfReportedLoanModel passingValueUpperBound = new SelfReportedLoanModel() { ReceivedYear = 2099 }; SelfReportedLoanModel failingValueYearTooLow = new SelfReportedLoanModel() { ReceivedYear = 1899 }; SelfReportedLoanModel failingValueYearTooHigh = new SelfReportedLoanModel() { ReceivedYear = 2100 }; SelfReportedLoanModel failingValueNull = new SelfReportedLoanModel() { }; SelfReportedLoanModel failingValueImproperNumberOfDigitsTooFew = new SelfReportedLoanModel() { ReceivedYear = 213 }; SelfReportedLoanModel failingValueImproperNumberOfDigitsTooMany = new SelfReportedLoanModel() { ReceivedYear = 20013 }; SelfReportedLoanModel failingValueNegative = new SelfReportedLoanModel() { ReceivedYear = -2013 }; RegularExpressionAttribute regexCheck = new RegularExpressionAttribute(@"^(19|20)\d{2}$"); Assert.IsTrue(regexCheck.IsValid(passingValueLowerBound.ReceivedYear), "Assertion of positive case lower bound being true failed"); Assert.IsTrue(regexCheck.IsValid(passingValueUpperBound.ReceivedYear), "Assertion of positive case upper bound being true failed"); Assert.IsFalse(regexCheck.IsValid(failingValueYearTooLow.ReceivedYear), "Assertion of negative case lower bound being true failed"); Assert.IsFalse(regexCheck.IsValid(failingValueYearTooHigh.ReceivedYear), "Assertion of negative case upper bound being true failed"); Assert.IsFalse(regexCheck.IsValid(failingValueNull.ReceivedYear), "Assertion of negative case null case being false failed"); Assert.IsFalse(regexCheck.IsValid(failingValueImproperNumberOfDigitsTooFew.ReceivedYear), "Assertion of negative case too few digits case being false failed"); Assert.IsFalse(regexCheck.IsValid(failingValueImproperNumberOfDigitsTooMany.ReceivedYear), "Assertion of negative case to many digits case being false failed"); Assert.IsFalse(regexCheck.IsValid(failingValueNegative.ReceivedYear), "Assertion of negative case (negative year) case being false failed"); }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var pageUrl = value as string; if (!string.IsNullOrWhiteSpace(pageUrl)) { var regExpAttribute = new RegularExpressionAttribute(PagesConstants.InternalUrlRegularExpression); if (!regExpAttribute.IsValid(pageUrl)) { return(new ValidationResult(errorMessage)); } } return(ValidationResult.Success); }
private void ValidateUsername(string username, string confirmUsername) { /* if (String.IsNullOrEmpty(username)) * { * this.AddValidationError("confirmUsername", LanguageResources.Authentication_NameNonMatching); * }*/ var changeUsername = new RegularExpressionAttribute(username); if (!changeUsername.IsValid(confirmUsername)) //(String.IsNullOrEmpty(username)) { changeUsername.FormatErrorMessage(LanguageResources.Authentication_NameNonMatching); // IsValid("usernam") //this.AddValidationError("username", LanguageResources.Authentication_NameRequired); } }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value == null) { return(ValidationResult.Success); } var list = value as IList; if (list == null) { throw new InvalidOperationException("The ListRegularExpressionAttribute is used on a type that doesn't implement IList."); } foreach (var item in list) { if (!regularExpressionAttribute.IsValid(item?.ToString())) { return(new ValidationResult(FormatErrorMessage("item"))); } } return(ValidationResult.Success); }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { try { Person contact = (Person)validationContext.ObjectInstance; if (contact.Category != Person.Customer || string.IsNullOrEmpty(value.ToString())) { return(ValidationResult.Success); } RegularExpressionAttribute regex = new RegularExpressionAttribute(Pattern); if (!regex.IsValid(value)) { return(new ValidationResult($"Birthday should be a valid date, format: yyyy-mm-dd")); } } catch (Exception ex) { return(new ValidationResult("Somrthing wrong while validating Birthday!" + ex.Message)); } return(ValidationResult.Success); }
public void IsCultureCodeValidTests() { var attribute = new RegularExpressionAttribute(Expressions.CultureCode); Assert.IsTrue(attribute.IsValid(null)); Assert.IsTrue(attribute.IsValid(string.Empty)); Assert.IsTrue(attribute.IsValid("ab")); Assert.IsTrue(attribute.IsValid("XYZ")); Assert.IsTrue(attribute.IsValid("ab-WxYz")); Assert.IsTrue(attribute.IsValid("abc-wXyZ")); Assert.IsTrue(attribute.IsValid("ab-cd")); Assert.IsTrue(attribute.IsValid("UVW-XYZ")); Assert.IsTrue(attribute.IsValid("ab-123")); Assert.IsTrue(attribute.IsValid("XYZ-789")); Assert.IsTrue(attribute.IsValid("ab-CD-Efgh")); Assert.IsTrue(attribute.IsValid("UVW-XYZ-abcd")); Assert.IsTrue(attribute.IsValid("ab-123-cdef")); Assert.IsTrue(attribute.IsValid("XYZ-789-abcd")); Assert.IsFalse(attribute.IsValid("12")); Assert.IsFalse(attribute.IsValid("789")); Assert.IsFalse(attribute.IsValid("6789")); Assert.IsFalse(attribute.IsValid("A")); Assert.IsFalse(attribute.IsValid("AB-")); Assert.IsFalse(attribute.IsValid("WXYZ")); Assert.IsFalse(attribute.IsValid("AB-789-XYZ")); Assert.IsFalse(attribute.IsValid("AB-789-VWXYZ")); Assert.IsFalse(attribute.IsValid("UVW-XYZ-0000")); }
/// <inheritdoc /> public override bool IsValid(object value) { return(_regularExpressionAttribute.IsValid(value)); }
public void IsValid_NullValue() { Assert.IsTrue(attribute.IsValid(null)); }
public override bool IsValid(object value) { RegularExpressionAttribute rule = new RegularExpressionAttribute(@"(?=.*[#\[\]\@!$%^&()<>\?\*\\/\|\.\-\\_]){2,100}.+$"); return(!rule.IsValid(Convert.ToString(value).Trim())); }
public override bool IsValid(object value) { return(_regexAttribute.IsValid(value)); }
public void IsCuitValidTests() { var attribute = new RegularExpressionAttribute(Expressions.Cuit); Assert.IsTrue(attribute.IsValid(null)); Assert.IsTrue(attribute.IsValid("20245597151")); Assert.IsTrue(attribute.IsValid("20-24559715-1")); Assert.IsTrue(attribute.IsValid("27-23840320-6")); Assert.IsTrue(attribute.IsValid("27238403206")); Assert.IsTrue(attribute.IsValid(27238403206)); Assert.IsFalse(attribute.IsValid("20 24559715 1")); Assert.IsFalse(attribute.IsValid("123456789")); Assert.IsFalse(attribute.IsValid("aa-aaaaaaaa-a")); Assert.IsFalse(attribute.IsValid("4408 0412 3456 7890")); Assert.IsFalse(attribute.IsValid(0)); Assert.IsFalse(attribute.IsValid(123)); }
/// <summary> /// Tries to convert the given value to the given format /// </summary> /// <param name="dataType">The data type of the given value</param> /// <param name="value">The value to type check</param> /// <returns>True of successful, otherwise throws exception</returns> private bool TryDataConversion(DataDescriptor descriptor, object data) { // Gaurd against null reference if (data == null) { throw new NullReferenceException(); } var value = data.ToString(); switch (descriptor.DataType) { case "bool": Convert <bool>(value); break; case "date-time": Convert <DateTimeOffset>(value); break; case "double": Convert <double>(value); break; case "email": Convert <string>(value); var emailValidator = new EmailAddressAttribute(); return(emailValidator.IsValid(value)); case "int32": Convert <int>(value); break; case "int64": Convert <long>(value); break; case "guid": Convert <Guid>(value); break; case "phone": Convert <string>(value); var phoneValidator = new PhoneAttribute(); return(phoneValidator.IsValid(value)); case "string": Convert <string>(value); if (!string.IsNullOrEmpty(descriptor.Pattern)) { var regexValidator = new RegularExpressionAttribute(descriptor.Pattern); return(regexValidator.IsValid(value)); } break; case "time-span": Convert <TimeSpan>(value); break; default: // Log error throw new NotImplementedException(string.Format("The format: {0} with value: {1} has no corresponding cast", descriptor.DataType, value)); } return(true); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var regex = new RegularExpressionAttribute(@"^(.*?(\.(png|jpg|gif|jpeg)\b)[^$]*)$"); if (Used > InStock) { yield return(new ValidationResult( "Components used cannot be higher then Components owned", new[] { "InStock", "Used" })); } if (DatasheetFile != null && DataSheet != DatasheetFile.FileName) { yield return(new ValidationResult( "Text input is not the same as the file upload.</br>Did you wanted to change it to something else?", new[] { "DataSheet" })); } if (ComponentImageFile != null && ComponentImage != ComponentImageFile.FileName) { yield return(new ValidationResult( "Text input is not the same as the file upload.</br>Did you wanted to change it to something else?", new[] { "ComponentImage" })); } if (ComponentPinoutImageFile != null && ComponentPinoutImage != ComponentPinoutImageFile.FileName) { yield return(new ValidationResult( "Text input is not the same as the file upload.</br>Did you wanted to change it to something else?", new[] { "ComponentPinoutImage" })); } if (DataSheet != null) { if (DataSheet.Contains("www.google.")) { var datasheetLink = DataSheet.GetQueryParam("url"); if (datasheetLink.Contains(".pdf")) { DataSheet = datasheetLink; } else { yield return(new ValidationResult( "The Google link that was parsed does not contain a valid pdf file", new[] { "DataSheet" })); } } else if (!DataSheet.EndsWith(".pdf")) { yield return(new ValidationResult("Please upload a PDF file.", new[] { "DataSheet" })); } } if (ComponentPinoutImage != null) { if (ComponentPinoutImage.Contains("www.google.")) { var imagelink = ComponentPinoutImage.GetQueryParam("url"); if (regex.IsValid(imagelink)) { ComponentPinoutImage = imagelink; } else { yield return(new ValidationResult( "The Google link that was parsed does not contain a valid image link", new[] { "ComponentPinoutImage" })); } } else if (!regex.IsValid(ComponentPinoutImage)) { yield return(new ValidationResult("The file is not a valid image file", new [] { "ComponentPinoutImage" })); } } if (ComponentImage != null) { if (ComponentImage.Contains("www.google.")) { var imagelink = ComponentImage.GetQueryParam("url"); if (regex.IsValid(imagelink)) { ComponentImage = imagelink; } else { yield return(new ValidationResult( "The Google link that was parsed does not contain a valid image link", new[] { "ComponentImage" })); } } else if (!regex.IsValid(ComponentImage)) { yield return(new ValidationResult("The file is not a valid image file", new[] { "ComponentImage" })); } } }
static Rule() { //extra add("creditCard", new RuleVali(new DataTypeAttribute(DataType.CreditCard))); add("html", new RuleVali(new DataTypeAttribute(DataType.Html))); add("image", new RuleVali(new DataTypeAttribute(DataType.ImageUrl))); //base add("ip", new RuleVali(new RegularExpressionAttribute("^((([0-9])|(1[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-9])|([1-9][0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))$"))); add("url", new RuleVali(new DataTypeAttribute(DataType.Url))); add("email", new RuleVali(new DataTypeAttribute(DataType.EmailAddress))); add("korean", new RuleVali(new RegularExpressionAttribute("^[ㄱ-힣]+$"))); add("japanese", new RuleVali(new RegularExpressionAttribute("^[ぁ-んァ-ヶー一-龠!-゚・~「」“”‘’{}〜−]+$"))); add("alpha", new RuleVali(new RegularExpressionAttribute("^[a-z]+$"))); add("ALPHA", new RuleVali(new RegularExpressionAttribute("^[A-Z]+$"))); add("num", new RuleVali(F)); add("alphanum", new RuleVali(new RegularExpressionAttribute("^[a-z0-9]+$"))); add("1alpha", new RuleVali(new RegularExpressionAttribute("^[a-z]"))); add("1ALPHA", new RuleVali(new RegularExpressionAttribute("^[A-Z]"))); //type add("int", new Rule((value, arg, safe) => { if (value is string) { if (!I.IsValid(value)) { return(FAIL); } } return(to <int>(value)); })); add("float", new Rule((value, arg, safe) => { if (value is string) { if (!F.IsValid(value)) { return(FAIL); } } return(to <float>(value)); })); add("equalto", new Rule((value, arg, safe) => { var s = safe[arg[0]]; if (value is string) { return((string)value == to <string>(s) ? value : FAIL); } if (value is int) { return((int)value == to <int>(s) ? value : FAIL); } if (value is float) { return((float)value == to <float>(s) ? value : FAIL); } if (value is double) { return((double)value == to <double>(s) ? value : FAIL); } if (value is bool) { return((bool)value == to <bool>(s) ? value : FAIL); } return(FAIL); })); add("max", new Rule((value, arg, safe) => { var l = to <float>(arg[0]); if (value is string) { return(((string)value).Length < l ? value : FAIL); } else { return(to <float>(value) < l ? value : FAIL); } })); add("min", new Rule((value, arg, safe) => { var l = to <float>(arg[0]); if (value is string) { return(((string)value).Length > l ? value : FAIL); } else { return(to <float>(value) > l ? value : FAIL); } })); add("length", new Rule((value, arg, safe) => { var l = to <float>(arg[0]); if (value is string) { return(((string)value).Length == l ? value : FAIL); } else { return(to <float>(value) == l ? value : FAIL); } })); add("range", new Rule((value, arg, safe) => { float l = to <float>(arg[0]), m = to <float>(arg[1]); var v = value is string?((string)value).Length: to <float>(value); return(l <= v && v <= m ? value : FAIL); })); add("in", new Rule((value, arg, safe) => { return(Array.IndexOf(arg, value) > -1 ? value : FAIL); })); add("notin", new Rule((value, arg, safe) => { return(Array.IndexOf(arg, value) == -1 ? value : FAIL); })); add("string", new Rule((value, arg, safe) => { if (value is string) { return(value); } return(FAIL); })); }
public void IsPhoneNumberValidTests() { var attribute = new RegularExpressionAttribute(Expressions.PhoneNumber); Assert.IsTrue(attribute.IsValid(null)); Assert.IsTrue(attribute.IsValid(string.Empty)); Assert.IsTrue(attribute.IsValid("1234567")); Assert.IsTrue(attribute.IsValid("20 24559715 1")); Assert.IsTrue(attribute.IsValid("123456789")); Assert.IsTrue(attribute.IsValid("+1(234)4567789")); Assert.IsTrue(attribute.IsValid("+235 7789")); Assert.IsTrue(attribute.IsValid("00 27 238.40320")); Assert.IsTrue(attribute.IsValid("0011 87 9854 23")); Assert.IsTrue(attribute.IsValid("011 777899 854-2356")); Assert.IsTrue(attribute.IsValid("0011 (87) 9854 23")); Assert.IsTrue(attribute.IsValid("011 (777899) 854-2356")); Assert.IsTrue(attribute.IsValid("07778542356")); Assert.IsTrue(attribute.IsValid("1.234.567-0000")); Assert.IsTrue(attribute.IsValid("1/23/56/000")); Assert.IsTrue(attribute.IsValid("1/2/5/0")); Assert.IsTrue(attribute.IsValid("4408 0412 3456 7890")); Assert.IsTrue(attribute.IsValid("00")); Assert.IsTrue(attribute.IsValid(12)); Assert.IsTrue(attribute.IsValid(123)); Assert.IsTrue(attribute.IsValid(1234567890)); Assert.IsFalse(attribute.IsValid("1/2/")); Assert.IsFalse(attribute.IsValid("1/23/56/000-")); Assert.IsFalse(attribute.IsValid("-1/23/56/000")); Assert.IsFalse(attribute.IsValid("a")); Assert.IsFalse(attribute.IsValid("0")); Assert.IsFalse(attribute.IsValid("+0")); Assert.IsFalse(attribute.IsValid("+00 27 238.40320")); Assert.IsFalse(attribute.IsValid("+0011 (87) 9854 23")); Assert.IsFalse(attribute.IsValid("+011 (777899) 854-2356")); Assert.IsFalse(attribute.IsValid("+07778542356")); Assert.IsFalse(attribute.IsValid(0)); }