Firebase rules with number of childs varying










2














I'm building an angular firebase web app based on a chat. Simplified example of my structure:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...


I'm trying to write the rules ".write" for writing a new message (without cloud functions) that must have an author, a message and a number of users' id varying (here user1, user2, user3) which are here under /users (= a list of users varying). No problem with the author and the message but how can I do the part varying ? Is it even possible without cloud functions ?



This is what this part of the rules look now:



"$other": 
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"



(if you have followed me so you should understand $other represents user1, user2 and user3 in my example).



EDIT:



Based on the example above, let's imagine a user user4 joined the room 1:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...


And now a user write a message:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ msg2
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ user4: "user4"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...


The message must follow list of users.










share|improve this question























  • Did you try the rule you gave? Because that's basically how a $ variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under a users subnode though, just to make things more explicit.
    – Frank van Puffelen
    Nov 12 '18 at 19:32










  • Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
    – Curse
    Nov 12 '18 at 20:03










  • Ah, that means that you still want to add a newData.hasChildren(...) to the parent node's .validate rule. I'll write up an answer.
    – Frank van Puffelen
    Nov 12 '18 at 20:16















2














I'm building an angular firebase web app based on a chat. Simplified example of my structure:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...


I'm trying to write the rules ".write" for writing a new message (without cloud functions) that must have an author, a message and a number of users' id varying (here user1, user2, user3) which are here under /users (= a list of users varying). No problem with the author and the message but how can I do the part varying ? Is it even possible without cloud functions ?



This is what this part of the rules look now:



"$other": 
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"



(if you have followed me so you should understand $other represents user1, user2 and user3 in my example).



EDIT:



Based on the example above, let's imagine a user user4 joined the room 1:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...


And now a user write a message:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ msg2
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ user4: "user4"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...


The message must follow list of users.










share|improve this question























  • Did you try the rule you gave? Because that's basically how a $ variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under a users subnode though, just to make things more explicit.
    – Frank van Puffelen
    Nov 12 '18 at 19:32










  • Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
    – Curse
    Nov 12 '18 at 20:03










  • Ah, that means that you still want to add a newData.hasChildren(...) to the parent node's .validate rule. I'll write up an answer.
    – Frank van Puffelen
    Nov 12 '18 at 20:16













2












2








2


2





I'm building an angular firebase web app based on a chat. Simplified example of my structure:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...


I'm trying to write the rules ".write" for writing a new message (without cloud functions) that must have an author, a message and a number of users' id varying (here user1, user2, user3) which are here under /users (= a list of users varying). No problem with the author and the message but how can I do the part varying ? Is it even possible without cloud functions ?



This is what this part of the rules look now:



"$other": 
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"



(if you have followed me so you should understand $other represents user1, user2 and user3 in my example).



EDIT:



Based on the example above, let's imagine a user user4 joined the room 1:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...


And now a user write a message:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ msg2
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ user4: "user4"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...


The message must follow list of users.










share|improve this question















I'm building an angular firebase web app based on a chat. Simplified example of my structure:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...


I'm trying to write the rules ".write" for writing a new message (without cloud functions) that must have an author, a message and a number of users' id varying (here user1, user2, user3) which are here under /users (= a list of users varying). No problem with the author and the message but how can I do the part varying ? Is it even possible without cloud functions ?



This is what this part of the rules look now:



"$other": 
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"



(if you have followed me so you should understand $other represents user1, user2 and user3 in my example).



EDIT:



Based on the example above, let's imagine a user user4 joined the room 1:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...


And now a user write a message:



|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ msg2
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ user4: "user4"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...


The message must follow list of users.







firebase firebase-realtime-database firebase-security-rules






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:01

























asked Nov 12 '18 at 18:10









Curse

1531111




1531111











  • Did you try the rule you gave? Because that's basically how a $ variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under a users subnode though, just to make things more explicit.
    – Frank van Puffelen
    Nov 12 '18 at 19:32










  • Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
    – Curse
    Nov 12 '18 at 20:03










  • Ah, that means that you still want to add a newData.hasChildren(...) to the parent node's .validate rule. I'll write up an answer.
    – Frank van Puffelen
    Nov 12 '18 at 20:16
















  • Did you try the rule you gave? Because that's basically how a $ variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under a users subnode though, just to make things more explicit.
    – Frank van Puffelen
    Nov 12 '18 at 19:32










  • Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
    – Curse
    Nov 12 '18 at 20:03










  • Ah, that means that you still want to add a newData.hasChildren(...) to the parent node's .validate rule. I'll write up an answer.
    – Frank van Puffelen
    Nov 12 '18 at 20:16















Did you try the rule you gave? Because that's basically how a $ variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under a users subnode though, just to make things more explicit.
– Frank van Puffelen
Nov 12 '18 at 19:32




Did you try the rule you gave? Because that's basically how a $ variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under a users subnode though, just to make things more explicit.
– Frank van Puffelen
Nov 12 '18 at 19:32












Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
– Curse
Nov 12 '18 at 20:03




Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
– Curse
Nov 12 '18 at 20:03












Ah, that means that you still want to add a newData.hasChildren(...) to the parent node's .validate rule. I'll write up an answer.
– Frank van Puffelen
Nov 12 '18 at 20:16




Ah, that means that you still want to add a newData.hasChildren(...) to the parent node's .validate rule. I'll write up an answer.
– Frank van Puffelen
Nov 12 '18 at 20:16












1 Answer
1






active

oldest

votes


















-1














You want a few things here:



  1. The node must have user1, user2 and user3 properties.

  2. The value of those properties must exist as a key under /users

The relevant snippet of your rules:



"messages": 
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",

"message":
".validate": "newData.isString()",

"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"









share|improve this answer




















  • Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
    – Curse
    Nov 12 '18 at 20:40










  • Yeah, that I'd only see possible if you put all of them in a $msgid/users node under each message, because then each child would have to exist under /users too.
    – Frank van Puffelen
    Nov 12 '18 at 22:55










  • I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
    – Curse
    Nov 13 '18 at 7:28










  • Frank any idea ?
    – Curse
    Nov 19 '18 at 11:07











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267807%2ffirebase-rules-with-number-of-childs-varying%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














You want a few things here:



  1. The node must have user1, user2 and user3 properties.

  2. The value of those properties must exist as a key under /users

The relevant snippet of your rules:



"messages": 
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",

"message":
".validate": "newData.isString()",

"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"









share|improve this answer




















  • Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
    – Curse
    Nov 12 '18 at 20:40










  • Yeah, that I'd only see possible if you put all of them in a $msgid/users node under each message, because then each child would have to exist under /users too.
    – Frank van Puffelen
    Nov 12 '18 at 22:55










  • I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
    – Curse
    Nov 13 '18 at 7:28










  • Frank any idea ?
    – Curse
    Nov 19 '18 at 11:07
















-1














You want a few things here:



  1. The node must have user1, user2 and user3 properties.

  2. The value of those properties must exist as a key under /users

The relevant snippet of your rules:



"messages": 
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",

"message":
".validate": "newData.isString()",

"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"









share|improve this answer




















  • Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
    – Curse
    Nov 12 '18 at 20:40










  • Yeah, that I'd only see possible if you put all of them in a $msgid/users node under each message, because then each child would have to exist under /users too.
    – Frank van Puffelen
    Nov 12 '18 at 22:55










  • I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
    – Curse
    Nov 13 '18 at 7:28










  • Frank any idea ?
    – Curse
    Nov 19 '18 at 11:07














-1












-1








-1






You want a few things here:



  1. The node must have user1, user2 and user3 properties.

  2. The value of those properties must exist as a key under /users

The relevant snippet of your rules:



"messages": 
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",

"message":
".validate": "newData.isString()",

"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"









share|improve this answer












You want a few things here:



  1. The node must have user1, user2 and user3 properties.

  2. The value of those properties must exist as a key under /users

The relevant snippet of your rules:



"messages": 
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",

"message":
".validate": "newData.isString()",

"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"










share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 20:19









Frank van Puffelen

227k28372397




227k28372397











  • Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
    – Curse
    Nov 12 '18 at 20:40










  • Yeah, that I'd only see possible if you put all of them in a $msgid/users node under each message, because then each child would have to exist under /users too.
    – Frank van Puffelen
    Nov 12 '18 at 22:55










  • I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
    – Curse
    Nov 13 '18 at 7:28










  • Frank any idea ?
    – Curse
    Nov 19 '18 at 11:07

















  • Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
    – Curse
    Nov 12 '18 at 20:40










  • Yeah, that I'd only see possible if you put all of them in a $msgid/users node under each message, because then each child would have to exist under /users too.
    – Frank van Puffelen
    Nov 12 '18 at 22:55










  • I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
    – Curse
    Nov 13 '18 at 7:28










  • Frank any idea ?
    – Curse
    Nov 19 '18 at 11:07
















Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
– Curse
Nov 12 '18 at 20:40




Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
– Curse
Nov 12 '18 at 20:40












Yeah, that I'd only see possible if you put all of them in a $msgid/users node under each message, because then each child would have to exist under /users too.
– Frank van Puffelen
Nov 12 '18 at 22:55




Yeah, that I'd only see possible if you put all of them in a $msgid/users node under each message, because then each child would have to exist under /users too.
– Frank van Puffelen
Nov 12 '18 at 22:55












I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
– Curse
Nov 13 '18 at 7:28




I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
– Curse
Nov 13 '18 at 7:28












Frank any idea ?
– Curse
Nov 19 '18 at 11:07





Frank any idea ?
– Curse
Nov 19 '18 at 11:07


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267807%2ffirebase-rules-with-number-of-childs-varying%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto