csharp 验证至少有一个给定属性具有值(如果字符串也检查空字符串)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 验证至少有一个给定属性具有值(如果字符串也检查空字符串)相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace DataValidation
{
    /// <summary>
    /// Validates that at least one of the given properties has value (in case of strings also checks for empty strings)
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    
    public class OneOfIsRequired : ValidationAttribute
    {
        private readonly string[] _propertyNames;

        public OneOfIsRequired(params string[] propertyNames)
        {
            _propertyNames = propertyNames;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (_propertyNames.Length < 2)
            {
                return new ValidationResult($"At least two property names have to be specified in '{GetType().Name}' Validation Attribute for '{validationContext.ObjectType.Name}' class");
            }

            var conditionsMetCounter = 0;
            var propertyValues = new List<object>();
            foreach (var propertyName in _propertyNames)
            {
                var property = validationContext.ObjectType.GetProperty(propertyName);
                var propertyValue = property.GetValue(validationContext.ObjectInstance, null);
                if (propertyValue is string && !string.IsNullOrWhiteSpace((string) propertyValue))
                {
                    conditionsMetCounter++;
                }
                else if (propertyValue != null)
                {
                    conditionsMetCounter++;
                }
            }
            if (conditionsMetCounter > 0)
            {
                return ValidationResult.Success;
            }
            return new ValidationResult($"'{validationContext.ObjectType.Name} requires that one of '{string.Join("', '", _propertyNames)}' properties has value'");
        }
    }
}
using System.CodeDom;
using System.ComponentModel.DataAnnotations;
using System.Web.Http;
using DataValidation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests.Validation
{
    [TestClass]
    public class OneOfIsRequiredTests
    {
       private class TestClass
        {
            public string StringProperty1 { get; set; }
            public string StringProperty2 { get; set; }
            public string StringProperty3 { get; set; }

            public object ObjectProperty1 { get; set; }
            public object ObjectProperty2 { get; set; }
            public int IntProperty3 { get; set; }
        }

        
       
        [TestMethod]
        public void When_All_2_Properties_Have_Value_Should_Return_True()
        {
            //arrange
            var classInstance = new TestClass()
            {
                StringProperty1 = "abc",
                StringProperty2 = "xyz"
            };
            var sut = new OneOfIsRequired("Property1", "Property2");

            //act

            bool valid = sut.IsValid(classInstance);
            //assert

            Assert.IsTrue(valid);
        }

        [TestMethod]
        public void When_All_3_Properties_Have_Value_Should_Return_True()
        {
            //arrange
            var classInstance = new TestClass()
            {
                StringProperty1 = "abc",
                StringProperty2 = "xyz",
                ObjectProperty1 = new {Test=true,Text="Test Object"}
            };
            var sut = new OneOfIsRequired("StringProperty1", "StringProperty2","ObjectProperty1");

            //act
            bool valid = sut.IsValid(classInstance);
            //assert

            Assert.IsTrue(valid);
        }

        [TestMethod]
        public void When_One_3_Properties_Has_Value_Should_Return_True()
        {
                //arrange
                var classInstance = new TestClass()
                {
                    ObjectProperty1 = new { Test = true, Text = "Test Object" }
                };
                var sut = new OneOfIsRequired("StringProperty1", "StringProperty2", "ObjectProperty1");

                //act
                bool valid = sut.IsValid(classInstance);
                //assert

                Assert.IsTrue(valid);
            
        }

        [TestMethod]
        public void When_Properties_Are_Empty_or_whitespace_Strings_Should_Return_False()
        {
            //arrange
            var classInstance = new TestClass()
            {
                StringProperty1 = string.Empty,
                StringProperty2 = " ",
                ObjectProperty1 = new { Test = true, Text = "Test Object" },
                IntProperty3 = 5,

            };
            var sut = new OneOfIsRequired("StringProperty1", "StringProperty2");

            //act
            bool valid = sut.IsValid(classInstance);
            //assert

            Assert.IsFalse(valid);

        }

        [TestMethod]
        public void When_Property_Does_Not_Exist_Should_Return_False()
        {
            //arrange
            var classInstance = new TestClass()
            {
                StringProperty1 = string.Empty,
                StringProperty2 = " ",
                ObjectProperty1 = new { Test = true, Text = "Test Object" },
                IntProperty3 = 5,

            };
            var sut = new OneOfIsRequired("MadeUpProperty1", "StringProperty1");

            //act
            bool valid = sut.IsValid(classInstance);
            //assert

            Assert.IsFalse(valid);

        }

        [TestMethod]
        public void When_Properties_Are_Mixed_Type_Should_Return_True()
        {
            //arrange
            var classInstance = new TestClass()
            {
                ObjectProperty1 = new { Test = true, Text = "Test Object" }
            };
            var sut = new OneOfIsRequired("StringProperty1", "ObjectProperty1","IntProperty1");

            //act
            bool valid = sut.IsValid(classInstance);
            //assert

            Assert.IsTrue(valid);
        }
    }
}

以上是关于csharp 验证至少有一个给定属性具有值(如果字符串也检查空字符串)的主要内容,如果未能解决你的问题,请参考以下文章

如何在perl中验证具有给定文件扩展名的多个文件

流畅的断言。如何验证两个列表至少有一个相同的元素

请确保至少有一个领域可以验证这些令牌

在 JavaScript 中,如何确保数组至少有一个特定元素,而其他元素满足另一个条件?

LeetCode至 少有 1 位重复的数字

如何重新排序二进制矩阵,使列中至少有“1”值