I want to share input validation because:
What is the best way to share input validation in a full javascript web app (frontend: react, backend: nodejs)?
The solution I am thinking of is creating a validator module with all my javascript validators at the root of my codebase:
const validator = process.env.ENV === 'local' ? require('../../validator') : require('/validator')Is there any other widely accepted method to do this (I guess it is a very common problem but I didn't find similar issues)? If not, is my method correct?
Takeaways from answers and implementation:
export keyword in my validator but use module.exports = {}, because ES6 export won't work in nodejs)validator module and test in both environments (I added a script that runs before deploying and check for diff between the 2 modules)It is a good practice to use validations as a module but you should consider what would happen if for some reason you will have/want to change the back-end tech to something that is not Javascript like node.
In case you are going to implement it - you will have a dependency of the client and server on this module and you'll have to maintain different versions of this module or deploy client code and server code any time you have a validation change - which leads to some complexity you should take into your consideration as well .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With