Arjun Arora
Arjun Arora

Reputation: 996

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

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??

Upvotes: 0

Views: 1650

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239200

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; }        
}

Upvotes: 1

jazb
jazb

Reputation: 5791

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...

Upvotes: 0

Related Questions