Last active
September 25, 2017 00:48
-
-
Save LSTANCZYK/554ae6d0e9e2cc91da03ffc1bc77e542 to your computer and use it in GitHub Desktop.
Data AnnotationAttirbute to validate that value is one one of given stings strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.ComponentModel.DataAnnotations; | |
| using System.Diagnostics.CodeAnalysis; | |
| using System.Linq; | |
| namespace .DataValidation | |
| { | |
| /// <inheritdoc /> | |
| /// <summary> | |
| /// Validates that string is one of the given ones: | |
| /// </summary> | |
| /// <example> | |
| /// <code> | |
| /// [OneOf("A","B","C")] | |
| /// [OneOf("A","B","C","Value2", CaseSensitive = false)] | |
| /// </code> | |
| /// </example> | |
| [SuppressMessage("ReSharper", "ConvertToAutoProperty")] | |
| [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | |
| public class OneOfAttribute : ValidationAttribute | |
| { | |
| private readonly string[] _stringValues; | |
| public OneOfAttribute(params string[] stringValues) | |
| { | |
| _stringValues = stringValues; | |
| } | |
| public bool CaseSensitive { get; set; } = true; | |
| protected override ValidationResult IsValid(object value, ValidationContext validationContext) | |
| { | |
| if (validationContext.ObjectType != typeof(string)) | |
| { | |
| return new ValidationResult($"'{validationContext.DisplayName}' is not of the string type"); | |
| } | |
| if (_stringValues.Length == 0) | |
| { | |
| return new ValidationResult($"{this.GetType().Name} Validdation attribute on property '{validationContext.DisplayName}' has no allowed values provided."); | |
| } | |
| string s = value.ToString(); | |
| StringComparison comparisionMethod = CaseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase; | |
| return new ValidationResult($"'{validationContext.DisplayName}' value should be one of the following: '{string.Join("', '", _stringValues)}'"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using DataValidation; | |
| using Microsoft.VisualStudio.TestTools.UnitTesting; | |
| namespace CHR.API.Provisioning.ATT.Web.Tests | |
| { | |
| [TestClass] | |
| public class OneOfValidationTests | |
| { | |
| [TestMethod] | |
| public void OneOfAttribute_Should_Validate_Test() | |
| { | |
| //Arrange | |
| var attributeValue = "test"; | |
| var validationAttribute = new OneOfAttribute("test", "test1", "test2"); | |
| //Act | |
| var isValid = validationAttribute.IsValid(attributeValue); | |
| //Assert | |
| Assert.IsTrue(isValid); | |
| } | |
| [TestMethod] | |
| public void OneOfAttribute_Should_Not_Validate_Test() | |
| { | |
| //Arrange | |
| var attributeValue = "TEST"; | |
| var validationAttribute = new OneOfAttribute("test", "test1", "test2"); | |
| //Act | |
| var isValid = validationAttribute.IsValid(attributeValue); | |
| //Assert | |
| Assert.IsFalse(isValid); | |
| } | |
| [TestMethod] | |
| public void OneOfAttribute_With_NonCaseSensitiveValue_Separator_Should_Validate_test() | |
| { | |
| //Arrange | |
| var attributeValue = "test"; | |
| var validationAttribute = new OneOfAttribute("TEST", "TESTING", "TESTED") {CaseSensitive = false}; | |
| //Act | |
| var isValid = validationAttribute.IsValid(attributeValue); | |
| //Assert | |
| Assert.IsTrue(isValid); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment