aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

Is it possible to provide an independent object validator for Model #8688

Closed xiaotupansy closed 5 years ago

xiaotupansy commented 5 years ago

Is this a Bug or Feature request?:

Feature request

What I want is an independent object validator for Model that can be used anywhere without binding to Controller.

Here's the sample usage: ObjectValidator<MyModel>.Validate(myModelEntity) returns true or false.

Like json schema. https://github.com/tdegrunt/jsonschema Sample json schema code for validation: var validate = require('jsonschema').validate; console.log(validate(4, {"type": "number"}));

And I hope that I can specify the properties that should be validated without validating the whole model. Here's the sample usage:

ObjectValidator<MyModel>.Validate(m=>m.Name,"111") returns true or false.
ObjectValidator<MyModel>.Validate("Name","111") returns true or false.
rynowak commented 5 years ago

We have no plans to provide something like this in the framework. MVC's validation features are deeply tied to details of how the framework understands model objects and how it tracks validation errors.