Model validation for actions taken against the auditee

How can we validate data based on actions taken on a specific object? What more advanced alternatives are there for validating annotation data? Possibly connecting to Asp.net MVC and WebAPI so validation is still done automatically.

Example

Let's assume the user has joined a web application form.

public class User
{
    // required when providing user as input
    // not provided when creating new instance
    public int Id { get; set; }

    // required when user joins and of specific format AND IS UNIQUE based on data store users
    // optional when providing user as input
    public string Email { get; set; }

    ...
}

      

Perhaps object inheritance can help, but as far as I think about it inheritance would be just a hack. The base class would hardly have any properties, and we could get several extremely similar (properties) classes, but with different annotations in order to use the data annotations. And that's not good.

Desired implementation

I was thinking about validation based on actions being taken on a specific entity. This way we can define something like:

public class User
{
    [Required(Action = ValidationAction.Provide)] // or whatever action we'd define
    public int Id { get; set; }

    [Required(Action = ValidationAction.Create)]
    [IsUnique(Action = ValidationAction.Create)] // custom DataAnnotations validator
    [EmailAddress]
    public string Email { get; set; }

    ...
}

      

Asp.net MVC and WebAPI controller actions will require some kind of attribute to provide information about what is being done with specific objects, parameters

[HttpPost]
[ValidateForAction("user", ValidationAction.Create)]
[ValidateForAction("user.InvitedBy", ValidationAction.Provide)]
public ActionResult Join(User user)
{
    ...
}

      

or set uniformly for all parameters (and their entities in subtrees)

[HttpPost]
[ValidateForAction(ValidationAction.Create)]
public ActionResult Join(User user)
{
    ...
}

      

If ValidateForActionAttribute

not present in a controller action, validation should only check the validation action annotations (like EmailAddressAttribute

the ones above in my entity example).

A similar example would be a Stackoverflow script to add a response, where the response data will be validated using the create action and the associated question object (a property inside the response) will be validated for each action, because we basically just need it Id

.

Is there such a validation library? Has anyone done something like this?

How are you going to do this check?

+3


source to share


1 answer


It looks like it looks like a mandatory validator, where the validation depends on another property. However, model validation will not work here because the model is "assumed" to be independent of views or controllers.

Suppose you have a view model associated with individual actions on a controller, then the view model can use data annotations as appropriate for the view's requirements. See ASP.Net MVC and MVVM for more details on the MVVM pattern.



The last comment on the ID. Not sure if the Required attribute will work as the default for int is a valid value. Regular expression perhaps? ([1-9] | [0-9] {2,10})

public class RegistrationController

    [HttpPost]
    public ActionResult Provide(UserProvideViewModel user)
    {
         ...
    }

    [HttpPost]
    public ActionResult Join(UserJoinViewModel user)
    {
         ...
    }
}

[MetadataType(typeof(UserProvideViewModel_Validation))]
public partial class UserProvideViewModel : User
{
    // properties unique to the view model
}

public class UserProvideViewModel_Validation
{
    [RegularExpression(@"^([1-9]|\d{2,10})$")]
     public Id { get; set; }
}

[MetadataType(typeof(UserJoinViewModel_Validation))]
public partial class UserJoinViewModel : User
{
    // properties unique to the view model
}

public class UserJoinViewModel_Validation
{
     [Required]
     [EmailAddress]
     public Email { get; set; }
}

      

0


source







All Articles