André
André

Reputation: 783

Firestore Rules - Validate if data exists in other collection

I'm currently working on my firestore rules and I need to validate the incoming data. Besides what I already have, I also need to validate if the incoming origin and tag fields exist in the collection origins and tags. I've found how to do so when using references but I'm using embedded data so I'm unsure how to exactly do it.

function incomingData() {
  return request.resource.data
}
function validTicket() {
    return incomingData().email is string &&
    incomingData().description is string &&
    incomingData().address is string &&
    incomingData().location is string &&
    incomingData().postCode.matches('^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]') &&
    incomingData().size() == 5 &&
    incomingData().hasAll(['email','description', 'address', 'location', 'postCode']) &&
    isSecretary()
}

In the tags collection, every document has a single value with the tag name. The same applies to the origins.

enter image description here

Upvotes: 3

Views: 2133

Answers (1)

OmryRo
OmryRo

Reputation: 86

I'm sorry my answer will be partial, i need you to post your current firestore rules, and the name of the ticket collection...

anyway, for tags, you won't be able to search them for their value, and nor inside the rules, so you should save them as keys. that mean, that the key for the document of sports, should be sports, and not 8VCCvq7qnvjyT98r95pu.

next, you will have to use the function exists, in the follow way:

function isTagExists(tag) {
    return exists(/databases/$(database)/documents/tags/$(tag));
}

let me know if you updated the question or you need more help with my solution.

also you can read more at: https://firebase.google.com/docs/firestore/security/rules-conditions

Upvotes: 3

Related Questions