0

It is easy to add validation e.g. RequiredAttribute as attributes dev time:

[Required, StringLength(100)]
public string Title {get; set;}

But what if my model is an entity (part of EF Core model, described with fluent EF Core API) and I want to extract validation information from EF Core model at run-time (requried, string length, custom annotations)? How to add the validation rules for Title field (and do not decorate entity with attributes , that means to avoid validation information duplication)?

I could try to add attributes using reflection to each instance and this could work, but I just search for the alternative validation API. Is it really absent?

I of course can through away the standard validation, create my own validation functions and load errors to ModelState but then I loose the jquery-validation-unobtrusive client side validation configuration.

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
  • You want to use your EF models as MVC models? – DavidG Mar 06 '19 at 22:44
  • I see those data attributes are good for view models. For persistence entities, I often configure them using fluent API with different rules. With that being said, I usually have 2 different models. – David Liang Mar 06 '19 at 22:45
  • If you need it for all requests on your Api, you could use an ActionFilter. Can you add a bit, how your data and your metadata looks like? – Nikolaus Mar 06 '19 at 22:45
  • @DavidG yes I use them in this case, but would like to avoid the discussion of this decision. This question is about how to configure validation run time extracting validation information from ef core model. – Roman Pokrovskij Mar 06 '19 at 22:53
  • @Nikolaus I'm not in need of alternative or "correct" decisions. I just search for possibility to configure asp core validation run time (opposite to dev time attributes). – Roman Pokrovskij Mar 06 '19 at 22:56
  • 1
    But all this discussion goes away if you don't do that. I know you want to avoid this, but you should never use EF models as MVC models. Good luck finding a solution for this, but I'm afraid I won't be helping. – DavidG Mar 06 '19 at 22:57
  • @DavidG "never"? MS samples and code generators do this. Why do we should be surprised that you can't help? I'm more surprised that I can't find the run time api to configure validation... – Roman Pokrovskij Mar 06 '19 at 23:14
  • @RomanPokrovskij: If MS samples do this, why do you still need to ask question like this? Just follow the MS sample codes there. And, I hope you've never followed MS samples to build any enterprise application do you? They putting the code there doesn't mean it's the right way to do it, at least in enterprise level software. – David Liang Mar 06 '19 at 23:34
  • @DavidLiang That has it specific name "tunnel vision". You have never tried to create e.g. universal CRUD controller for the "EF entity" so please move your talks of what is "enterpise level" application to context of other questions may be related to "enterpise level" how you imagine it. – Roman Pokrovskij Mar 06 '19 at 23:49
  • @RomanPokrovskij For my reply: This is Stack Overflow. You ask for help and at least most of the people who answer want to help you. But SO-Users prefer examples to work with. Me for example, I‘m very lazy in writing. My programming teacher always said, if you are a good programmer, you‘re lazy and think before coding, to write as few code as possible. ;-) – Nikolaus Mar 07 '19 at 06:19

1 Answers1

1

How to add the validation rules for Title field (and do not decorate entity with attributes , that means to avoid validation information duplication)?

Implement the IClientModelValidator interface. Then use reflection against whatever model you want to build out the jQuery validation.

There isn't any automatic, enterprise, n-tier framework that will do what you want out of the box.

I just expect that there should be alternative validation configuration API.

I believe what both an use is the Validator Class. By default it can validate against an IValidatableObject interface (non-attribute based validation).

Stack Overflow - How do I use the IValidatableObect.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • But as I understand IClientModelValidator does not load errors to the server side ModelState - it is an API only to inject some code to html serialisation (used to create html that supports the client side validation). Am I wrong in that? BTW I'm not in search for fremework, I just expect that there should be alternative validation configuration API. It should be there. Attributes are interpretated by some kind of code. Does this code has a public api - this is a question. – Roman Pokrovskij Mar 07 '19 at 00:09
  • And third, there is one framework: https://fluentvalidation.net/aspnet . But I guess it ads attributes to every instance using reflection. What I would like to avoid. I hope there should be some native non-attribute way. – Roman Pokrovskij Mar 07 '19 at 00:20
  • (Updated Per your comments). – Erik Philips Mar 07 '19 at 04:51