Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase安全规则确保子项有条件_Firebase_Firebase Security_Firebase Realtime Database - Fatal编程技术网

Firebase安全规则确保子项有条件

Firebase安全规则确保子项有条件,firebase,firebase-security,firebase-realtime-database,Firebase,Firebase Security,Firebase Realtime Database,我正在使用Firebase构建我的安全规则,我遇到了以下情况: 我有一些嵌套数据,希望以以下形式发布到Firebase: data: { a: //Number, must always be present b: //Number, conditionally present (condition B) c: //Number, conditionally present (condition C) } 条件B和C基于一段数据,其他地方是Firebase,如果满足,我希望包含

我正在使用Firebase构建我的安全规则,我遇到了以下情况:

  • 我有一些嵌套数据,希望以以下形式发布到Firebase:

    data: {
      a: //Number, must always be present
      b: //Number, conditionally present (condition B)
      c: //Number, conditionally present (condition C)
    }
    
  • 条件B和C基于一段数据,其他地方是Firebase,如果满足,我希望包含值,但如果不满足,我希望值为
    null

    root.condition >= 5  //condition B
    root.condition >= 10 //condition C
    
以下是我尝试使用Bolt来构建这些规则:

type ConditionB extends Number {
  validate() = root.condition >= 5 ? this != null : this == null;
} 

type ConditionC extends Number {
  validate() = root.condition >= 10 ? this != null : this == null;
}

type Data {
  a: Number,
  b: ConditionB,
  c: ContitionC
}

path /data is Data {
  read()  = true,
  write() = true
}
以下是rules.json:

"rules": {
  "data": {
    ".validate": "newData.hasChildren(['a', 'b', 'c']),
    ...
  }
}

可以看出,
.validate
规则强制所有子项出现在
数据中。那么,根据我的条件,我如何确保
数据及其每个子项的
.validate
规则都是正确的?

既然您的类型可以为Null,您必须像这样扩展Number | Null:

type ConditionB extends Number | Null {
  validate() = root.condition >= 5 ? this != null : this == null;
}

type ConditionC extends Number | Null {
  validate() = root.condition >= 10 ? this != null : this == null;
}

type Data {
  a: Number,
  b: ConditionB,
  c: ConditionC
}

path /data is Data {
  read() = true;
  write() = true;
}
这将生成以下JSON规则文件:

{
  "rules": {
    "data": {
      ".validate": "newData.hasChildren(['a'])",
      "a": {
        ".validate": "newData.isNumber()"
      },
      "b": {
        ".validate": "(newData.isNumber() || newData.val() == null) && (newData.parent().parent().child('condition').val() >= 5 ? newData.val() != null : newData.val() == null)"
      },
      "c": {
        ".validate": "(newData.isNumber() || newData.val() == null) && (newData.parent().parent().child('condition').val() >= 10 ? newData.val() != null : newData.val() == null)"
      },
      "$other": {
        ".validate": "false"
      },
      ".read": "true",
      ".write": "true"
    }
  }
}

提示:如果您不喜欢看到newData.parent()…,请在验证表达式中使用previor(root)。谢谢您的回答和额外提示!我没有意识到您可以或键入
扩展
,如果看到添加到文档中会很好(或者可能是这样,我跳过了:)