0

Suppose you have a web application with AngularJS front-end and .NET Web Services back-end. You need to apply a set of normalization rules for form values. For example, where saving an item Name you'd like to ensure that it contains only single spaces, that "Parkway" inside address is replaced with "Pkwy" and "Drive" is replaced with "Dr", that -90 > Latitude <= 90, etc. Most of these rules are easy to set in terms of regular expressions.

  1. You would like the system to be easily upgradable to a future version, which is going to read those rules from a database. (For now, you would probably specify rules in server-side C# code with an ability to pass them to a client code.)
  2. You would like to apply some of normalization rules on a client side - in AngularJS code.
  3. Ideally, you don't want to modify [much] of your AngularJS code when rules are updated on a server (or in a database).
  4. You would possibly like to double-check that values are normalized in your C# code before passing form values to a database.

What approach would you use to add such a flexible system of normalization rules to your application?

vkelman
  • 1,501
  • 1
  • 15
  • 25

2 Answers2

0

On the client-side you mostly would do it using directives that implement custom validations / filters upon the FormController. You can see a little bit more about this here:

How to add custom validation to an AngularJS form?

Do you have to have .NET as the back-end? Because if you really want to share this code between client and server, the best option would be to go with Node.js on the back-end.

If you have to go with .NET and can stand a performance hit, you could set up a Node process that runs the validations on your data using the same code as the front-end, doing some out-of-process communication.

Otherwise, you will have duplicated code on the front- and back-end.

Community
  • 1
  • 1
Dema
  • 6,867
  • 11
  • 40
  • 48
  • Right, we're already using directives with some custom validations / filters. I'm going to improve upon those directives by adding normalization, like I described above - probably using $parsers.unshift() and/or some actions performed when a "Save" button is clicked. Application is .NET based. I just want to here for other people experience of passing those normalization rules from a server side's .Net web services to AngularJS. – vkelman Dec 17 '13 at 16:29
  • It's OK if some of the code will run on both client and server, but I have to avoid _hard-coding_ of rules on a client. They must be passed from a server, later on they'd be stored in a database. – vkelman Dec 17 '13 at 16:40
0

I agree with Dema regarding doing all validations and normalizations on server side, however I think it would make more sense to use .NET code for validation rather than building the whole Node.js back end just to be able to run javascript on the server.

starnovsky
  • 76
  • 2
  • Yep, my comment on that was more directed if they had a choice of going with Node.js on the backend instead of .NET. – Dema Dec 17 '13 at 14:36