Cloud Firestore规则中的字符串函数

时间:2019-02-25 19:27:52

标签: firebase-realtime-database google-cloud-firestore firebase-security firebase-security-rules

我需要删除request.auth.phone_number返回的字符串处的加号信号。为此,我尝试使用替换功能,但是收到以下错误:“找不到功能错误:名称:[replace]。”;

match /test/{id} {
  allow read, update, delete, create: if request.auth != null && (resource.data.items[request.auth.token.phone_number.replace('+', '')] == true || resource == null);
}

当我在实时数据库上运行时,这很好。例如:

"tests": {
  "$uid": {
    ".write": "auth.uid.replace('+', '') === '5521999991234'"
  }
}   

在Cloud Firestore数据库中,有没有办法使用字符串函数,例如“ contains(),replace(),toLowerCase()”等?

谢谢

3 个答案:

答案 0 :(得分:1)

Firestore安全规则使用的语言与实时数据库完全不同。

您可以在API documentation中看到字符串对象上所有可用方法的列表。您要求做的唯一实际可用的是lowermatches

答案 1 :(得分:1)

您可以编写这样的函数:

function replace(string, replace, by) {
  return string.split(replace).join(by)
}

并使用它。.

replace('+3312312345', '+', '')

在您的示例中:

match /test/{id} {
  allow read, write: if request.auth != null && 
  (resource.data.items[replace(request.auth.token.phone_number, '+', '')] 
  == true || resource == null);
}

// below your rules
function replace(string, replace, by) {
  return string.split(replace).join(by)
}

答案 2 :(得分:0)

更新:几个月前添加了.flowconfig方法。您现在可以做类似的事情

[ignore]
<PROJECT_ROOT>/node_modules/.*config-chain/test/broken.json
<PROJECT_ROOT>/node_modules/@mapbox/jsonlint-lines-primitives/test/fails/*
<PROJECT_ROOT>/node_modules/react-universal-component/*
<PROJECT_ROOT>*/buildClient/*
<PROJECT_ROOT>/node_modules/mapbox-gl/src/style-spec/node_modules/jsonlint-lines-primitives/test/fails/*
<PROJECT_ROOT>/node_modules/mapbox-gl/src/style-spec/node_modules/@mapbox/jsonlint-lines-primitives/test/fails/*
<PROJECT_ROOT>/node_modules/redux-persist/lib/persistReducer.js.flow

[include]

[libs]

[lints]
# This must be turned off until a fix for https://github.com/flow-typed/flow-typed/issues/3008 is released
deprecated-utility=off

[options]
emoji=true
esproposal.optional_chaining=enable
module.name_mapper.extension='graphql' -> '<PROJECT_ROOT>/packages/marketing-site/internals/flow/graphqlMock'
server.max_workers=1

[strict]

请注意,第一个参数是格式为字符串的正则表达式。

文档:https://firebase.google.com/docs/reference/rules/rules.String.html#replace