Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Honour Validation attributes #18

Closed
benfoster opened this issue Apr 11, 2016 · 9 comments
Closed

Honour Validation attributes #18

benfoster opened this issue Apr 11, 2016 · 9 comments
Milestone

Comments

@benfoster
Copy link

Currently only type checking is performed. It would be good if the object adaptor also honoured validation attributes on the object for example:

        [Range(18, 65)]
        public int Age { get; set; }

Currently I could set the age property to 10 using a patch operation.

@dougbu
Copy link
Member

dougbu commented Apr 12, 2016

Object validation of HTTP data using validation attributes is an MVC feature. What is your end-to-end scenario? Why have you chosen not to use MVC in this scenario?

@benfoster
Copy link
Author

We are using MVC. In the above scenario we'd have to manually invoke the validator on every patched object. You're currently doing type checking - it would be good if we had the ability to override this behaviour to also perform additional validation.

@Eilon Eilon added this to the Backlog milestone Apr 18, 2016
@Eilon
Copy link
Member

Eilon commented Apr 18, 2016

Seems reasonable to me, but this isn't planned at this time.

@aelghoneimy
Copy link

Maybe you could do a separate Package to augment JsonPatch for MVC purposes, like Microsoft.AspNetCore.JsonPatch.MVC, which will have Validation Attributes and Bind Attribute checking.

@arielcloud
Copy link

+1 for this feature

@arielcloud
Copy link

Also, It'll be great to honour validation rules of fluentValidation

@RehanSaeed
Copy link

RehanSaeed commented Feb 1, 2017

For those looking for a copy paste solution to this, you need to call TryValidateModel which adds any model errors to the ModelState for you and then you can check the model:

// Check that JSON patch document is valid.
if (!this.ModelState.IsValid)
{
    return this.BadRequest(this.ModelState);
}

patch.SafeApplyTo(model, this.ModelState);
this.TryValidateModel(model);
// Check that modified model is valid.
if (!this.ModelState.IsValid)
{
    return this.BadRequest(this.ModelState);
}

It's also worth noting that the JSON patch document could also be invalid, so you need to check the model state twice.

If you are using ASP.NET Core 1.0, then note that ApplyTo throws exceptions if the JSON patch document is invalid (More info and a workaround here). This was fixed in ASP.NET Core 1.1, where it adds model state errors instead.

@Eilon
Copy link
Member

Eilon commented Aug 28, 2017

Closing because there are no plans to implement this.

Using TryValidateModel is the best way to do this.

@Eilon Eilon closed this as completed Aug 28, 2017
@RehanSaeed
Copy link

@Eilon Is this because of the disconnect between ASP.NET Core and MVC? I've heard there are plans to remove that disconnect although that statement is vague, could this issue be reopened once that happens?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants