Search code examples
google-cloud-firestorefirebase-security

In firestore rules, how to convert int value to string with left padding zeros?


In firebase rules, I am converting a month to 2 char string to access a document.

For eg, when converting let var = string(3) I want to get "03". Instead I get 3.

I can't locate anything for zero padding in https://github.com/google/cel-spec/blob/master/doc/langdef.md

How to convert int value to string with left padding zeros?


Solution

  • I can't find a simple way for zero padding. For now, I added a check on string length and concatenating a "0".

    let d = string(3);
    let dd = (d.size()==2 ? d : "0"+d);
    

    Here d.size() is not 2, so dd will be assigned "03"