vogels
vogels copied to clipboard
Need to validate data when doing Table.update
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.)
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 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 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.
validation mechanism in vogels is incomplete without this
+1 for this feature
+1 for feature
+1 for feature
+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 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 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)