Why validation rules behave differently (only) with onDisconnect update?

I have made some tests with firebase and saw difference in rules validation between onDisconnect().update() and simple update() using newData.parent() in rules.

When I using update() it works. But with onDisonnect().update() I have PERMISSION_DENIED!

Database rules:

{
    "rules": {
        "$id": {
            ".read": true,
            ".write": true,
            "a": { ".validate": true },
            "b": { ".validate": "newData.parent().child('a').val() == 'a'" },
        }
    }
}

Javascript:

const valid = {
  a: 'a',
  b: 'b'
};

function update(obj) {
  console.log('update', obj);
  return ref.update(obj);
}

function onDisconnectUpdate(obj) {
  console.log('onDisconnectUpdate', obj);
  return ref.onDisconnect().update(obj);
}

update(valid);
onDisconnectUpdate(valid);

Full example code

See a screenshot here

I hope you can help me.

Answers:

Answer

firebaser here

During an onDisconnect() operation, each property is treated as a separate write operation while we're validating the rules. That means that in your case the operation fails. While this is an unfortunate design, it is unlikely to change in the near future. For this reason I recommend using simpler write operations in onDisconnect() handlers.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.