title: Firebase realtime database validation username and email
body: <p>Recently found out the database rules can be set to validate whether the database already have the same data but currently it just accepted same data to insert but how to validate and prevent same input of username and email?</p>
  
  <pre><code>{
  "rules": {
          ".read": true,
      ".write": true,
          "username":{
         ".validate": "!root.child('username').child(newData.val()).exists()"
          },
          "email":{
          ".validate": "!root.child('email').child(newData.val()).exists()"
        }
  
          }
    }
  </code></pre>
  
  <p>The root child is created by email authentication uid and the rest will be under the same nodes.</p>
  
  <p><a href="https://i.stack.imgur.com/AZNyE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AZNyE.jpg" alt="enter image description here"></a></p>
  
  <p>How to prevent user enter same username and email?</p>