Search code examples
regexfirebasefirebase-realtime-databasebolt

How to validate regex using the bolt compiler


I am trying to validate a field that will receive a URL. I need to validate it using an appropriate regex, but what I got from the Firebase documentation itself is not working. The compiler bolt neither compiles, causing the error:

bolt:37:3: Invalid property or method: 'validate() { this.test((/^(ht|f)tp(s?):\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*((0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\'.
bolt:37:3: Invalid property or method: '\\'\\/\\\\+&=%\\$#_]*)?$/) '.
bolt: Fatal errors: 2
My code:
type Category {
  categoryName: String,
  isAvailable: Boolean,
  createdAt: Number,
  photoUrl: LinkURL,
  subcategories: Object | Null
}

type LinkURL extends String {
  validate() { this.test((/^(ht|f)tp(s?):\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*((0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\'\\/\\\\+&=%\\$#_]*)?$/) }
}

Solution

  • I believe you have an extra "(" at the start of the regex

    try this

    type LinkURL extends String {
      validate() { this.test(/^(ht|f)tp(s?):\/\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*((0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&=%\$#_]*)?$/) }
    }