I have a legacy Java monolithic web application. My goal is to use React on the frontend, keep Java on the backend and add an API for the frontend to use.
My question is how can I write the data validation just once, using it on both the frontend for form validation and on the backend for data validation?
With the API written in java and the data validation written in javascript, is a workable solution to have an intermediate validation proxy API written in javascript+nodejs which then passes validated requests to the java API?
React(js validation) -> nodejs-API(js validation) -> Java-API(no data validation)
1 Answer 1
Two points:
- Validation in the UI is designed to illicit corrections from the user.
- Human readable explanations
- UI alert indicators
- dynamic checking
- Validation in the backend is designed to prevent nasty security issues.
- pass/fail
- fail fast
- single pass
While they both do "validate" the document:
- their intentions are quite different: online vs. offline, full vs. fail fast, verbose vs pass/fail.
- the resources available are different: Gigabytes vs. MegaBytes, Multiple threads vs. One thread
- and the sheering forces acting on the code is different: Browser Architectures vs. Server Architectures, the frameworks, and languages, etc...
Just to get this to work, as you've just pointed out, you would need to:
- reorganise your network architecture,
- adopt a new platform,
- ensure you validation library is poly-fillable for both browser and server usage.
That is a high price to pay for de-duplicating just 2000 lines of code.
-
Thanks. Good points. Duplicating the validation code might not be worthwhile once we begin using automated testing, where we currently have testers doing everything manually. I would think just about all our frontend validation logic would also need to be on the backend so we have data integrity. If something isn't allowed on the frontend then we shouldn't allow the backend API accept data which doesn't conform to the validation logic of the frontend.Dan– Dan2019年11月01日 12:38:17 +00:00Commented Nov 1, 2019 at 12:38
-
I'm thinking we'll still have an api gateway in front of the new API added to the legacy Java app so we can migrate functionality out of the legacy app and into services.Dan– Dan2019年11月01日 12:43:34 +00:00Commented Nov 1, 2019 at 12:43
-
Code duplication is always an evil and must be avoided by any cost.Andremoniy– Andremoniy2022年04月29日 06:46:55 +00:00Commented Apr 29, 2022 at 6:46
-
@Andremoniy Duplication isn't evil. There are many useful forms of it, such as when we instantiate an object, for the second time; when we write a test that describes our system, as after-all the system is code, and code is a description; and as above the same business requirement such as a field being an email address should be validated in UI, but also must be validated in the API possibly using different languages/ways of expressing validation failure. I do agree that so much duplication actually represents incidental complexity, and that should always be reduced as technically possible.Kain0_0– Kain0_02022年04月29日 11:55:26 +00:00Commented Apr 29, 2022 at 11:55
Explore related questions
See similar questions with these tags.
Java-API(no validation)
you are assuming there would be always "something" in front of the JAVA API doing validations. You are also assuming that whatever lays in front of Java API has no bugs. Too many assumptions. On the other hand, validations are part of the business. Why decouple them? Why make things so complicated just to reuse a bunch of "if null, if empty, ....". What's the goal? What's the problem to solve?that risk exists even with just the single Java API
yes, but with your design, you have 2 components to test and debug to find where the bug is at.