Nik
Nik

Reputation: 709

How to validate location path in Firebase Rules?

I would like to validate $username length to be longer than 5 character and shorter than 24 character

{
    "rules": {
        "user": {
            "$username": {
                ".write": "auth != null",
                "uid": {},
                "created": {},
                "lastlogin": {}
            }
        }
    }
}

(if it's possible) How can i do that?

Upvotes: 1

Views: 472

Answers (2)

Stein Vanmassenhove
Stein Vanmassenhove

Reputation: 16

you can validate the $username with a regex

{
    "rules": {
        "user": {
            "$username": {
                ".write": "auth != null",
                ".validate": "$username.matches(/^.{6,23}$/)"
            }
        }
    }
}

Upvotes: 0

Nik
Nik

Reputation: 709

Yes it is possible:

{
    "rules": {
        "user": {
            "$username": {
                ".write": "auth != null",
                ".validate":"newData.hasChildren(['validate_username'])",
                "validate_username":{
                    ".validate":"
                        newData.val() == $username
                        && newData.val().length > 5
                        && newData.val().length < 24
                    "
                },
                "uid": {},
                "created": {},
                "lastlogin": {}
            }
        }
    }
}

Upvotes: 1

Related Questions