简体   繁体   English

firebase 安全规则:可以是 null 或字符串的可选字段?

[英]firebase security rules: optional field that could be a null or a string?

How to make firebase accept a key that could be a null or a string?如何使 firebase 接受可以是 null 或字符串的密钥? and yet the field is optional, since in security rules you canot ceck if (is) is null?但是该字段是可选的,因为在安全规则中您无法检查(是否)是 null?

function dataCheckCreate(requestData) {
  return (
    // requestData.count required 
    requestData.count is number &&
    // requestData.src required 
    requestData.src is string &&
    // requestData.date optional !!
    // if available it could be a null or a string 
    (requestData.date == null || requestData.date is string)
  );
}

The last rule will be true when date is either equal to null or is a string.date等于null或者是一个字符串时,最后一条规则为true If you want that field to be optional then try:如果您希望该字段是可选的,请尝试:

function dataCheckCreate(requestData) {
  return (
    requestData.count is number &&
    requestData.src is string &&

    // [date not present in data keys]       [date is string]
    (!('date' in requestData.keys()) || requestData.date is string)
  );
}

You cant store null in a string field on firebase. The equivalent is to not store it at all.你不能把null存储在firebase上的一个字符串字段中。相当于根本不存储它。

Javascript will set the key date to undefined/null if it doesn't exist. Javascript 将关键日期设置为 undefined/null 如果它不存在。 Since it's either null or string and optional, do you really need to check it?因为它要么是 null 要么是字符串和可选的,你真的需要检查它吗? This would be a suitable check:这将是一个合适的支票:

function dataCheckCreate(requestData) {
   return requestData.count && requestData.src

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM