Reputation: 3076
How can I prevent writing to the firebase database if the value/string is too long ?
Ex: if I have this dataset
and I don't want to add this product to the database if the 'name' is longer than 10 characters. How can I do that ?
Like this:?
exports.preventNameTooLong = functions.database.ref('/userProducts/{userID}/')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
const product = event.data.val();
if(product.name.length > 10){
return;
}else{
return event.data.ref;
}
});
Or is this code completely wrong ?
Upvotes: 0
Views: 282
Reputation: 13063
Use a realtime database rule for quicker validation: https://firebase.google.com/docs/database/security/securing-data
In your case, add this rule:
".validate": "newData.isString() && newData.val().length < 10"
This should go in your realtime database rules, so not in your function. This way the user can not even write to that path you inserted the rule in.
Upvotes: 3