How to validate only a part of Model State in asp.net core 2 web api

社会主义新天地 提交于 2019-12-11 17:53:31

问题


We are using a Usermaster DTO in 2 different projects.

public class UserMaster : BaseProperties
{      

    [BsonElement]
    [BsonRequired]
    [Required]
    public string FirstName { get; set; }

    [BsonElement]
    [BsonRequired]
    [Required]
    public string LastName { get; set; }

    [BsonElement]
    [BsonRequired]
    [EmailAddress]
    [Required]
    public string EmailId { get; set; }

    [BsonElement]
    [BsonRequired]
    [DataType(DataType.Password)]
    [Required]
    public string Password { get; set; }

    [BsonIgnore]
    [DataType(DataType.Password)]
    [Required]
    public string NewPassword { get; set; }        
}

Now we are using this DTO in 2 different projects.
Project 1 is Admin in asp.net MVC 5.0, There we can use ModelState.IsValidField to validate only a part of this whole Model.
Project 2 is a Web Api build in asp.net core 2. There i could not find any solution which can only validate Email and Password for login purpose..

Basically i am facing issue in asp.net core 2 web api where i could not specify the exact data member that could be only validate. I have to pass anything on other [Required] fields to validate request. likewise ModelState.IsValidField

Any solutions??


回答1:


If you have two different validation requirements, then you should have two different view models/DTOs. The whole entire point of a view model/DTO is handle particular usage scenario. Here, you have two different sets of request data, so your issue is entirely down to trying to use the same class to satisfy both, when the two are not the same.

If you want to reduce code duplication, simply continue using inheritance:

public class UserLogin : BaseProperties
{
    [BsonElement]
    [BsonRequired]
    [EmailAddress]
    [Required]
    public string EmailId { get; set; }

    [BsonElement]
    [BsonRequired]
    [DataType(DataType.Password)]
    [Required]
    public string Password { get; set; }
}

public class UserMaster : UserLogin
{      

    [BsonElement]
    [BsonRequired]
    [Required]
    public string FirstName { get; set; }

    [BsonElement]
    [BsonRequired]
    [Required]
    public string LastName { get; set; }

    [BsonIgnore]
    [DataType(DataType.Password)]
    [Required]
    public string NewPassword { get; set; }        
}



回答2:


in your DTO implement IValidatableObject

public class UserMaster : BaseProperties, IValidatableObject
{      

    [BsonElement]
    [BsonRequired]
    [Required]
    public string FirstName { get; set; }

    [BsonElement]
    [BsonRequired]
    [Required]
    public string LastName { get; set; }

    [BsonElement]
    [BsonRequired]
    [EmailAddress]
    [Required]
    public string EmailId { get; set; }

    [BsonElement]
    [BsonRequired]
    [DataType(DataType.Password)]
    [Required]
    public string Password { get; set; }

    [BsonIgnore]
    [DataType(DataType.Password)]
    [Required]
    public string NewPassword { get; set; }      

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
    //you custom validation here...
    }  
}

So if you can detect the context it is being used in then you are on your way...



来源:https://stackoverflow.com/questions/52755073/how-to-validate-only-a-part-of-model-state-in-asp-net-core-2-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!