K
K
kazhuravlev2014-06-16 15:19:14
JavaScript
kazhuravlev, 2014-06-16 15:19:14

How to synchronize JS framework models with server models?

I’ll make a reservation right away - the question is general and it doesn’t matter which framework is used (neither on the server nor on the client).
The problem is point by point:
- There are some data models (tables, schemas, structures) on the server.
- The server provides the client with some API for accessing data
- Each endpoint in the server API has its own set of fields, its own restrictions on reading, writing, changing data
- The client must adapt to the server data models each time the models or data schema change on the server
Question:
How do you ensure that client models (with field descriptions, validation, etc.) are identical to server endpoints?
Example:
There is some web application.
The application can:
- display a list of users
- create new users
A user has a set of attributes, including a phone number and email.
When filling out the user creation form, the client part of the application must check the phone number and email for validity. After sending valid data to the server to create a new user, the server may respond positively or return an error like "Not unique email" or "Not unique phone number". These are normal responses from the server and should be handled appropriately by the client.
The server must validate the data. the client is ideal too. It turns out that it is necessary to describe the validation twice for each field (on the server and on the client)? Doesn't this bother anyone?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vitaliycto, 2014-06-16
@vitaliycto

I store the validation rules in the model and, when displaying, I send these rules to the client in json or in the data attributes of the field (depending on the template engine)
well, it’s not difficult to write a function that will process these rules)
if you need to check the fields for uniqueness, then do the field validation via server

K
Konstantin Kitmanov, 2014-06-16
@k12th

It turns out that it is necessary to describe the validation twice for each field (on the server and on the client)? Doesn't this bother anyone?

Yes, it bothers a lot of people. Therefore, node.js and full-stack frameworks appeared .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question