Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: How would you validate data on both back end and front end?
4 points by borlum on June 12, 2015 | hide | past | favorite | 5 comments
Dear HN,

in most projects you want to do validation of the submitted data both client- and server-side.

When using node/js the validation is simple, of course, because the validation rules are written once. But, more often than not I don't work in JS or use another server side language.

My Question: Do you know of any good way to validate objects/data structures in a language agnostic way?

I have considered something like JSON-schema or some sort of code generation.

Any ideas?




Client side validation is a user interface improvement when it provides convenience. It does not replace server side validation because server side validation ensures application integrity. Of course applications that run in the browser muddy the water a bit, but still whatever hits the server requires scrutiny.


Do basic validation client side - if you can, rely on html input types and html5 validation attributes. It should be enough to be helpful in most cases without being overly onerous to maintain or hard to match server-side exactly.

Put your full detailed validation rules server-side.


Can you give an example of what you mean by 'validate'? The word is highly overloaded and has different meaning depending on where the data is being used.


In some java web applications, I wrote form validation rules in javascript for client side validation, and reused them server side using java's scripting API.


By validate I mean, rules like:

- is "title" set, - when "age" is set "birthday" must not be set. - the cart can have a max of 10 items.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: