Firestore规则限制字符串的字符

时间:2018-03-10 20:57:26

标签: firebase google-cloud-firestore

如何在Firestore规则中限制用户名的字符?我尝试逐个验证每个字符,但它不起作用

     function validateChar1(sername) {

     return sername[0] == "a" || sername[0] == "b" || sername[0] == "c" || sername[0] == "d" || sername[0] == "e" || sername[0] == "f" || sername[0] == "g" || sername[0] == "h"
        || sername[0] == "i" || sername[0] == "j" || sername[0] == "k" || sername[0] == "l" || sername[0] == "m" || sername[0] == "n" || sername[0] == "o" || sername[0] == "p"
  || sername[0] == "q" || sername[0] == "r" || sername[0] == "s" || sername[0] == "t" || sername[0] == "u" || sername[0] == "v" || sername[0] == "w" || sername[0] == "x"
  || sername[0] == "y" || sername[0] == "z" || sername[0] == "0" || sername[0] == "1" || sername[0] == "2" || sername[0] == "3" || sername[0] == "4" || sername[0] == "5" 
  || sername[0] == "6" || sername[0] == "7" || sername[0] == "8" || sername[0] == "9" || sername[0] == "." || sername[0] == "_"
    }

    function validateChar3(sername) {

    return sername[2] == "a" || sername[2] == "b" || sername[2] == "c" || sername[2] == "d" || sername[2] == "e" || sername[2] == "f" || sername[2] == "g" || sername[2] == "h"
        || sername[2] == "i" || sername[2] == "j" || sername[2] == "k" || sername[2] == "l" || sername[2] == "m" || sername[2] == "n" || sername[2] == "o" || sername[2] == "p"
  || sername[2] == "q" || sername[2] == "r" || sername[2] == "s" || sername[2] == "t" || sername[2] == "u" || sername[2] == "v" || sername[2] == "w" || sername[2] == "x"
  || sername[2] == "y" || sername[2] == "z" || sername[2] == "0" || sername[2] == "1" || sername[2] == "2" || sername[2] == "3" || sername[2] == "4" || sername[2] == "5" 
  || sername[2] == "6" || sername[2] == "7" || sername[2] == "8" || sername[2] == "9" || sername[2] == "." || sername[2] == "_" || sername.size() < 3;
    }



    function validateUsername(username) {

    return validateChar1(username) && validateChar2(username) && validateChar3(username) && validateChar4(username) && validateChar5(username) &&
        validateChar6(username) && validateChar7(username) && validateChar8(username) && validateChar9(username) && validateChar10(username) &&
  validateChar11(username) && validateChar12(username) && validateChar13(username) && validateChar14(username) && validateChar15(username) 
    }

1 个答案:

答案 0 :(得分:2)

看看Cloud Firestore Rules Reference。在string部分,您会找到matches。这允许您根据正则表达式检查值。您可以创建正则表达式,仅验证用户名允许的字符。