Avoid duplicate validation code on Javascript interface and Java backend?

We have a standard web application, and the front-end uses Angular, the back-end is written in Java 7. I noticed that we get a lot of duplicate code. That is, the check is performed on the client side of Angular, but I can not completely trust it, since the user can turn off the check on the client side and just send an email request to the server.

So I have to repeat the same server side validation logic in Java. This is pretty tiring, but I don't know how to avoid it. Perhaps I can write some parts of the back-end with a javascript framework (suppose it is Node.js) that can use the same validation library as the front-end and make Java back-end interact with Node.js? What is the best approach to solve this problem?

+4
source share
3 answers

Java can run JavaScript code and other dynamic scripting languages. In Java 7, JavaScript ScriptEngine is called Rhino, and in Java 8 it is called Nashorn.

Java Java Java 7: https://docs.oracle.com/javase/7/docs/technotes/guides/scripting/programmer_guide/

, JavaScript js , BackEnd .

+3

AngularBeans framework BeanValidation java angular, . .

0

Define validation rules as data in JSON format, for example, and output them through the endpoint in the same API that your data provides. Now the validation rules are publicly available and can be used by any other service to validate the data using some library that can analyze the rules and accept / reject the submitted data (i.e. from the form).

0
source

Source: https://habr.com/ru/post/1615386/


All Articles