vogels icon indicating copy to clipboard operation
vogels copied to clipboard

Need to validate data when doing Table.update

Open csdev opened this issue 10 years ago • 9 comments

The Table.create method uses joi to check that all of the supplied data is valid before attempting to serialize it and write it to the database (table.js line 116). However, Table.update does not do any data validation. This can result in invalid data being saved to the db. It can also cause a crash since some of the serializer code assumes that it is working with valid data. (For example, if the user performs an update with an invalid Date, a later call to Date.toISOString will raise an uncaught error.)

csdev avatar Jun 17 '14 21:06 csdev

I agree that we should validate this data. The reason data is currently only validated during create is because during update you can pass different pieces of data which are completely valid during update, but would not be valid when creating an initial record. Such as partial updates, removing params from dynamodb, and adding items to Sets.

I'm sure there is a good solution for this, but I want to be careful that we don't over complicate things and potentially make it difficult to fully utilize the feature DynamoDB offers

ryanfitz avatar Dec 22 '14 00:12 ryanfitz

@ryanfitz I've got on similar issue this week and some coworkers have faced it too in the past. I've two different suggestions that could be implemented (I could open the PR if you prefer and don't have time to it). First one would be add an option like 'updateValidation' (false as default) and it would be able validation before update. The problem that I see on this feature is that it is a little bit intrusive. The second one would be able to return the object schema and them it would be able to validate it before performing an update whenever the developer feels it is necessary.

Would like to know your opinion. I'm adapting (not modifying the lib) the second solution over here as we've faced some problems...

viniciuskneves avatar Aug 27 '15 14:08 viniciuskneves

@viniciuskneves Yes, this issue need to be handled, an record update is important operation as much as create, so we must validate data before update.

We should have separate mechanism to validate data on update eg. updateValidations() or validateOnUpdate(true) during schema.

avtaniket avatar Oct 19 '15 06:10 avtaniket

validation mechanism in vogels is incomplete without this

+1 for this feature

Alex0007 avatar Jan 18 '16 22:01 Alex0007

+1 for feature

speedster-kiev avatar Feb 25 '16 13:02 speedster-kiev

+1 for feature

andyday avatar Mar 15 '16 23:03 andyday

+1 I would also like to do on demand validation, before making the create request, which currently seems impossible due to table/schema being private.

sjungwirth avatar Aug 12 '16 00:08 sjungwirth

@sjungwirth I finally found you can access the table/schema by calling model.scan(), which returns an object with the table, which contains the schema object, which you can call validate. It's hacky but it seems the easiest way to access the object.

AaronHarris avatar Aug 15 '16 19:08 AaronHarris

@AaronHarris this is really helpful, thank you. While looking into what you said it looks like there is a reference to table on model instances as well. So it looks like either of these will work:

myModelInstance.table.schema.validate(myModelInstance) or MyModel.scan().table.schema.validate(myDataOrModelInstanceToValidate)

sjungwirth avatar Aug 15 '16 21:08 sjungwirth