2

Here we go again with topic what was asked many times all over internet, but I not found any good enough answer:

What are best practices in making client and server side validations synced?

I was not doing it in web apps I was making (I just had duplicated validations), but now when I started with React Native and REST API as backend I asked myself if I have nerves to do every validation twice and edit it twice if something change.

Options what are getting into my mind:

  1. Not share validation at all (Have two validation rules, two validator functions, two regexes set...)
  2. Have just server side validation (so much 2000's am I right?)
  3. Having just client side validation (I AM JOKING!)
  4. Let client app download at start some rules/sets from server and use them

I was really thinking about the 4th option, but maybe its too complicated (regexes, functions, etc. cannot be moved easily trough internet - JSON for example), but it is also good for on-air updates etc.

I really dont know.

Any ideas, tips, best practices?

0

Yes!!! This could be a bit annoying. I too find no prefect solution.

Now that your have listed all possible options, let me give you my opinion on them

Option 3: "Having just client side validation (I AM JOKING!)" Glad you were only joking :)

Option 4: "Let client app download at start some rules/sets from server and use them" As you rightly said, this will push the complexity. We could have a different kinds of validations such as "lessThan", "lessThanOrEqualTo", "greaterThan", "inBetween", "regex" and so on... So if the client has to download all this validation rules from server and validate, we need to have a schema for the validation rules. This sounds too complex to me. I wouldn't want to do this as we are not building a validation framework. I am okay with a bit of duplicate code in favor of reduced complexity.

Option 2: "Have just server side validation" I think this would be a nice idea, as there is no duplicate code (validation not in two places and two languages). The cost of this is few extra Http calls. I think this would be okay in most business scenarios. If the user experience is bad in this option, then move towards option 1

Option 1: "Not share validation at all" This is also okay. However over time when the validations change, a good effort is needed to keep the back end and front end validations in sync. This would increase the probability of bugs.

I would prefer to start with option 2, move towards options 1 if necessary on case by case basis.

For the specific case of Node js back end, you could share the validation logic between the front end and back end.

As WebAssembly is getting popular, you could try to use WebAssembly for validation. This way it is possible to execute Java or C# or few other language code in the browser. (I haven't explored this option much)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.