Validation Before Saving new documents by checking existing documents


#1

In TodoList couchbase Lite demo, If I want to restrict a user to create max 10 list and max 50 tasks inside list. How can I do on Server side? (NOT in client side)

Is there any way in sync function I can query existing documents?


#2

No, the Sync Function cannot query/reference other documents. For the type of restriction you describe, client-side validation would be the most common approach. If that’s not going to work for your scenario, you could consider modelling the lists (and the ‘list of lists’) as single documents, to allow enforcement within the scope of the doc.


#3

Thanks for replying. Question was related to generic problem of validating doc with external factors before saving but I will continue with same example.

so as I understand any validation that we need to do before saving the doc must be done by client side because on Sync Gateway you cant reject or accept the doc based on external logic. Please correct me if I am wrong.

On server side validation, which approach do you suggest we should take?

  1. Save the list with state == toBeCreated and use Change Worker pattern using some robot on server to validate the doc and change it to Created or Rejected. The problem I think with this is delay due to two write on database.
  2. Make an API on server side which each client should call to create the list, server API can create the list and then on Sync gateway reject all request of list creation that is not coming from server (requireUser as admin)

This way app will be protected from malicious client code.

Lastly one more question, As I understand Couchbase stores doc into memory first and then eventually on disk unless specified. _changes feed REST API on sync gateway provides feed after the doc is saved on disk or in memory doc as well?


#4

Hi Adam,
possible for you to answer above question?