0

This is my controller.I Need to validate the input parameters before the action method gets executed.The action method accepts the Json object but inside the method we will convert it into model object.Is it possible to validate the input as Model object in my custom action filter?ValidateParamFilterAttribute is my custom filter class and it should accept the action methods as a parameter .It should be generic so I don't need to repeat it for every action methods in my app.

public class InputValidationController : ApiController
    {

        [HttpPost][ValidateParamFilter(typeof(Users))]     
        public string SaveData([FromBody]JObject testdata )
        {

        }
    }

My Custom Action Filter:

public class ValidateParamFilterAttribute: Attribute , IActionFilter
    {
     public ValidateParamFilterAttribute(Type type) {
            //not able to proceed here 
            // the type parameter will take my modal classes.in this case it should be Users
        }

        public override void OnActionExecuting(HttpActionContext actionContext)
        {

        }
    }

Model class:

public class Users
    {
        [Required(ErrorMessage ="Id field is mandatory")]
        public int id { get; set; }
        [Required][StringLength(10,ErrorMessage ="Name length can't be more than 10")]
        public string Name { get; set; }
        [Required][Phone][MaxLength(10)]
        public string PhoneNumber { get; set; }
        [Required][EmailAddress]
        public string Email { get; set; }
    }
1
  • Any updates about this case? Have you achieved the requirement? Commented Jun 18, 2020 at 8:10

1 Answer 1

3

Is it possible to validate the input as Model object in my custom action filter?

If you'd like to generate a instance of your model class Users based on the received value of action argument testdata, and manually do model validation in action filter, you can refer to the following code snippet.

OnActionExecuting method of action filter

public void OnActionExecuting(ActionExecutingContext context)
{
    var jobj = context.ActionArguments["testdata"] as JObject;

    var users = new Users
    {
        id = (int)jobj["id"],
        Name = (string)jobj["name"],
        PhoneNumber = (string)jobj["phoneNumber"],
        Email = (string)jobj["email"]
    };

    var isvalid = (context.Controller as UsersController).TryValidateModel(users, nameof(Users));

    if (!isvalid)
    {
        //...
        //code logic here
        //...

UsersController and SaveData action

[ApiController]
public class UsersController : ControllerBase
{
    [HttpPost("SaveData")]
    [ValidateParamFilter(typeof(Users))]
    public IActionResult SaveData([FromBody]JObject testdata)
    {
        //...

Test Result

1) Not Valid

enter image description here

2) Valid

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Rather reading each property and creating the object. you can use JObject.ToObject<T>();
I have upvoted this answer because it got me to find out TryValidateModel() which was the thing I was looking for to apply model validation on deserialized objects from JObject, Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.