7

Is there an approach that preserves the single-definition of validation, allows immediate client-side validation, and still provides the strength of server-side validation?

I'm familiar with the following two approaches:

The first renders ng-validation syntax in Razor output, based on rules stated in the model. That seems like it couples me too tightly to building Razor views, when Razor views often won't intuitively pair with a well-organized Angular app. I could use this approach, but it seems it's more a result of someone wanting to replicate "Unobtrusive jQuery/MVC Validation", than building something well-suited to Angular.

The second approach just returns server-side validation back to Angular to render. It also doesn't do a thorough job of it. If needed I could run without client-side validation, since a single-page app still won't get screen flashes... but it's not ideal.

For example, maybe there is a toolset to more directly reflect the validation rules directly on WebAPIs and consume them in an Angular app. Or another approach that I haven't found?

At https://www.youtube.com/watch?v=lHbWRFpbma4#t=1336 , the presenter seems to imply this problem is already well-solved for Angular, and refers to specification (DDD Using Specification pattern for Validation). If you are aware of any tools that make this applicable to my problem, I'd love to hear it.

p.s. It seems like this is almost certainly an often-asked question. I'm sorry I was unable to find an answer here before posting

p.p.s. I'm currently planning to use Entity Framework, but I'd switch to address this. Heck, I'd consider switching to a whole different platform for this, my first Angular-focused project.

6
  • Have you considered using Node.js for the server side, which would allow you to create "classes" in JS for your models that could have validation embedded, and which can be used on both the client and the server? I have no experience with this personally, but I understand that it is theoretically possible. Commented Sep 30, 2014 at 0:29
  • 1
    One more note: I found this series of posts on good JS patterns illuminating, you may too. Because they are just vanilla JS, they may lend themselves well to being used on both the client and server. Commented Sep 30, 2014 at 0:32
  • Thank you Greg. On your first comment, I'm imagining a monstrosity of automapping models into my node.js classes, then back out into my data models, which then obviously still must pass into the data services. Instinctively, I'm also a little shy of using javascript anything as a security layer at my server. I'm not saying it can't work, my brain is just screaming "be very afraid". Commented Sep 30, 2014 at 6:30
  • 1
    MajoB: I'm not sure if it was clear, but I was looking for a way to only define the validation rules in a single place, because defining two separately increases the likelihood of implementation errors. e.g. differing min/max string lengths. Commented Sep 30, 2014 at 14:55
  • 1
    One options is to just output the metadata for the model in a JSON format. Then you will just need to map the particular validation attributes to an equivalent client-side rule. Commented Oct 2, 2014 at 15:59

1 Answer 1

2

The approach I recommend is based on @Esteban Felix commented and I used similar strategy in Java/Bean Validation and JSON schema generator.

Annotate your Domain model using Validate Model Data Using DataAnnotations

Generate schema using JSON.net other useful links

Create a AutoValidate directive that goes on and decorate fields with AngularJS build in directives for form validation e.g. ngPattern and simple things like min/max. In my case I created a mapping from Java world to AngularJS directives and wherever I needed we created custom directives.

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

2 Comments

I've accepted this answer for now, as an indication that such a framework does not already exist in a complete form. If you've visited here and found that it does exists, please add an answer!
@shannon Not aware of any such framework. You may find some useful JS code in ~/Scripts/MicrosoftMvcValidation.debug.js mentioned in the DataAnnotate link. I haven't looked at it or know where is the source is but that Javascript might be useful in rolling your own.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.