DataAnnotations中的必需属性似乎不起作用

问题描述 投票:2回答:2

我创建了一个DataAnnotationValidator。我目前正在尝试使用必填字段属性对其进行测试,并且当我的属性为null时,我无法使IsValid属性失败。当我创建具有超出指定范围的Range属性的数字时,它可以正常工作。

public class TestEntityWithDataAnnotations
{
    public Guid Id { get; set; }

    [Required(ErrorMessage = "Required")]
    public string Name { get; set; }
}

[TestFixture]
public class DataAnnotationValidatorTest
{
    [Test]
    public void Validate_ReturnsFailure_WhenPropertyValidationIsNotValid()
    {
        var validator = new DataAnnotationValidator();
        var invalidEntity = new TestEntityWithDataAnnotations
        {
            Id = Guid.NewGuid()
        };
        var validationResult = validator.Validate(invalidEntity);

        Assert.IsFalse(validationResult.IsValid);
    }
}

public class DataAnnotationValidator
{
    public ValidationResult Validate(object obj)
    {
        Type objType = obj.GetType();            
        var typeDescriptor = GetTypeDescriptor(obj, objType);
        var validationResult = new ValidationResult();

        var classValidationResult = CheckClassIsValid(obj, typeDescriptor);
        if (!classValidationResult.IsValid)
        {
            validationResult.AddErrors(classValidationResult.Errors);
        }
        foreach (PropertyDescriptor propertyDescriptor in typeDescriptor.GetProperties())
        {
            // Loop over all of the properties on our object that have Validation Attributes
            var propValidationResult = CheckPropertyIsValid(obj, propertyDescriptor);
            if(!propValidationResult.IsValid)
            {
                validationResult.AddErrors(propValidationResult.Errors);
            }
        }
        return validationResult;
    }

    /// <summary>
    /// Checks to see if there are any class level validation attributes and runs them
    /// </summary>
    /// <returns></returns>
    private static ValidationResult CheckClassIsValid(object obj, ICustomTypeDescriptor typeDescriptor)
    {
        var errors = typeDescriptor.GetAttributes().OfType<ValidationAttribute>()
            .Where(x => !x.IsValid(obj))
            .Select(x => new ValidationError(typeDescriptor.GetClassName(), x.ErrorMessage));
        return new ValidationResult(errors.ToList());
    }

    /// <summary>
    /// Checks to see if a property has any DataAnnotations that it has violated
    /// </summary>
    private static ValidationResult CheckPropertyIsValid(object obj, PropertyDescriptor propertyDescriptor)
    {
        var errors = propertyDescriptor.Attributes.OfType<ValidationAttribute>()
            .Where(x => !x.IsValid(obj))
            .Select(x => new ValidationError(propertyDescriptor.Name, x.ErrorMessage));
        return new ValidationResult(errors.ToList());
    }

    /// <summary>
    /// Gets the model's type descriptor. In order to support the buddy class metadata model
    /// for LINQ to SQL and Entity Framework, it uses
    /// <see cref="AssociatedMetadataTypeTypeDescriptionProvider"/>.
    /// </summary>
    /// <param name="obj">The model object</param>
    /// <param name="objType">The type of the model object</param>
    /// <returns>The model's type descriptor</returns>
    private static ICustomTypeDescriptor GetTypeDescriptor(object obj, Type objType)
    {
        var provider = new AssociatedMetadataTypeTypeDescriptionProvider(objType);
        return provider.GetTypeDescriptor(objType, obj);
    }
}
c# data-annotations
2个回答
2
投票

我有点愚蠢。我需要将属性的值传递给CheckPropertyIsValid内部的IsValid而不是整个对象。

private static ValidationResult CheckPropertyIsValid(object obj, PropertyDescriptor propertyDescriptor)
    {
        var errors = propertyDescriptor.Attributes.OfType<ValidationAttribute>()
            .Where(x => !x.IsValid(propertyDescriptor.GetValue(obj)))
            .Select(x => new ValidationError(propertyDescriptor.Name, x.ErrorMessage));
        return new ValidationResult(errors.ToList());
    }

0
投票

请参阅Micorosof Docs:https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

在ASP.NET Web API中,可以使用System.ComponentModel.DataAnnotations命名空间中的属性为模型上的属性设置验证规则。在ModelState上,您可能会发现验证错误。 .Net Core似乎可以自己处理这个问题。对我来说,这是在针对.net框架时发生的。

考虑以下模型:

using System.ComponentModel.DataAnnotations;

namespace MyApi.Models
{
    public class Product
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public decimal Price { get; set; }
        [Range(0, 999)]
        public double Weight { get; set; }
    }
}

using MyApi.Models;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MyApi.Controllers
{
    public class ProductsController : ApiController
    {
        public HttpResponseMessage Post(Product product)
        {
            **if (ModelState.IsValid)**
            {
                // Do something with the product (not shown).

                return new HttpResponseMessage(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.