Nick
Nick

Reputation: 3090

How to validate a key in Firebase Rules?

In a Realtime Firebase database, this is my data structure:

{'comments': {
  ${project_uuid}: {
     ${thread_uuid}: {
        'author': 12,
        'data': 'who lot of text'
     }
  }
}}

I want to validate that inside comments there are only uuid Objects. I use bolt to generate the Firebase Rules:

path /comments {
   read() { auth != null }
   write() { auth != null }

   /{project_uuid} is UuidOnly {
       /{thread_uuid} is Thread;
   }      
}

type Thread {
   author: Number,
   data: String,
}

type UuidOnly {
  validate() {
    // The next line is the problem
    this.parent().key().test(/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/)
  }
}

In that last line, key() doesn't seem to be available as function. How can I do this validation to ensure that inside comments there are only uuid Objects?

Upvotes: 0

Views: 222

Answers (1)

Rainy sidewalks
Rainy sidewalks

Reputation: 1160

greeting nick , try the following

type UuidOnly {
  validate() {
    data.test(/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/)
  }
}

try the above and let me know any

EDIT (possible fault in pattern match ):

type UuidOnly {
  validate() {
    this.parent().key().test(/^[0-9A-Fa-f]{8}\-[0-9A-Fa-f]{4}\-[0-9A-Fa-f]{4}\-[0-9A-Fa-f]{4}\-[0-9A-Fa-f]{12}$/)
  }
}

i am not a pro bolt user but i think here we need to add the escape character before each "-" character in the regular expression used to validate the UUID format. try it.

Upvotes: 0

Related Questions