LoginSignup
0
0

More than 1 year has passed since last update.

Firestore Security Ruleで、特定の値を特定の値に変更する更新のみを許可する

Posted at

RDMSだと、特定の更新のみを許可する場合が良くあります。たとえば、status = "done"にする場合だけ許可するようなパターン。これを、どう書くか、汎用的に関数化すると以下のようになります。

このテクニックを使えると、Cloud Functionsを使わなくて良いパターンがかなり増えるのではないかと思います。

    // 指定されたキー以外は変化していない
    function isNotChangedExcept(keys) {
      return request.resource.data.diff(resource.data).affectedKeys().hasOnly(keys) 
    }
  	// 更新後の値が、valueを含んでいる
    function hasFixedUpdate(value) {
      return request.resource.data.diff(value).unchangedKeys() == value.keys().toSet();
    }
  	// 更新時のupdate条件がvalueと一致する
    function isFixedUpdate(value) {
      return isNotChangedExcept(value.keys) && hasFixedUpdate(value);
    }
        // 使用例
    isFixedUpdate({"status": "done"}) || isFixedUpdate({"status": "cancelled"})
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0