question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Be able to reference map keys in validation rules

See original GitHub issue

I’m having trouble with a DB structure that looks like this:

{
  "post-flag": {
    "-KvXU5eiR4oZM-vBihib": {
      "6t5KEJjAOSYbf8dleabDlTyxhux2": 1507542071604,
      "6t5KEJjAOSYbf8dleabDlTyxhux3": 1507542071605
    }
  }
}

I’d like to validate that the data is valid and that the keys follow a certain format.

I’ve tried several variations of the following, but none of them really work.

path /post-flag is Map<PushID, Map<UserID, UnixTimestamp>>;
path /post-flag/{post}/{user} is UnixTimestamp {
  write() {
    return isCurrentUser(user) && root.post[post] != null
  }
}

type PushID extends String {
  validate() { return this.test(/^[-_0-9a-zA-Z]{20}$/); }
}

type UserID extends String {
  validate() { return this.test(/^[0-9a-zA-Z]{27}[0-9]$/); }
}

type UnixTimestamp extends Number {
  validate() { return this >= 0; }
}

For the example above I get the following JSON:

{
  "rules": {
    "post-flag": {
      "$key1": {
        ".validate": "newData.hasChildren() && $key1.matches(/^[-_0-9a-zA-Z]{20}$/)",
        "$key2": {
          ".validate": "$key2.matches(/^[0-9a-zA-Z]{27}[0-9]$/) && newData.isNumber() && newData.val() >= 0"
        }
      },
      ".validate": "newData.hasChildren()",
      "$post": {
        "$user": {
          ".validate": "newData.isNumber() && newData.val() >= 0",
          ".write": "auth != null && auth.uid == $user && newData.parent().parent().parent().child('post').child($post).val() != null"
        }
      }
    }
  }
}

Which yields the following error in Firebase simulator:

Cannot have multiple default rules (‘$key1’ and ‘$post’).

I’m not sure if I’m writing my rules wrongly or if this is a limitation of Bolt.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
alixaxelcommented, Oct 25, 2017

I’ll have a go at it. 😄

0reactions
rockwotjcommented, Oct 24, 2017

Currently there is not, but feel free to open a PR 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation Rules - Salesforce Help
Validation rules verify that the data a user enters in a record meets the standards you specify before the user can save the...
Read more >
How to Use Validation Rules in Salesforce (+ Examples)
Validation Rules in Salesforce verify that the data entered by a user meets certain criteria before the user can save the record.
Read more >
Enable request validation in API Gateway - AWS Documentation
To enable basic validation, you specify validation rules in a request validator, add the validator to the API's map of request validators, and...
Read more >
GitHub - bufbuild/protoc-gen-validate
This plugin adds support to protoc-generated code to validate such constraints. ... map fields with message values, setting this rule to true disallows...
Read more >
Error Messages | Maps JavaScript API - Google Developers
The API key included in the script element that loads the API is not found. Please make sure you are using a correct...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found