0
0
Firebasecloud~20 mins

Data validation rules in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firebase Rules Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
What happens when a Firebase Realtime Database rule denies a write?

You have a Firebase Realtime Database rule that denies write access to a path. What is the result when a client tries to write data to that path?

Firebase
rules_version = '2';
service firebase.database {
  match /{database}/ {
    match /users/{userId} {
      allow write: if false;
    }
  }
}
AThe write is accepted and data is saved regardless of the rule.
BThe write is accepted but data is not saved.
CThe write is queued and retried later automatically.
DThe write is rejected and the client receives a permission denied error.
Attempts:
2 left
💡 Hint

Think about what happens when rules explicitly deny access.

🧠 Conceptual
intermediate
1:30remaining
Which Firebase Realtime Database rule condition checks if a user is authenticated?

In Firebase Realtime Database rules, which condition correctly checks if the user is signed in?

Aauth != null
Buser != null
Crequest.auth == true
Dauth.isSignedIn == true
Attempts:
2 left
💡 Hint

Firebase uses a special variable to represent authentication info.

Configuration
advanced
2:30remaining
Which rule correctly validates that a 'score' field is an integer between 0 and 100?

You want to write a Firebase Realtime Database rule that allows writes only if the 'score' field is an integer from 0 to 100 inclusive. Which rule condition achieves this?

AnewData.exists() && newData.score >= 0 && newData.score <= 100
BnewData.hasChild('score') && newData.child('score').isNumber() && newData.child('score').val() >= 0 && newData.child('score').val() <= 100
CnewData.hasChild('score') && typeof newData.score == 'number' && newData.score >= 0 && newData.score <= 100
DnewData.child('score').isInteger() && newData.child('score').val() > 0 && newData.child('score').val() < 100
Attempts:
2 left
💡 Hint

Use Firebase's newData.child('field').isNumber() and val() methods.

security
advanced
2:00remaining
What is the risk of using 'allow read, write: if true;' in Firebase rules?

What security risk arises if you set Firebase Realtime Database rules to allow read, write: if true;?

Firebase
rules_version = '2';
service firebase.database {
  match /{database}/{document=**} {
    allow read, write: if true;
  }
}
AOnly authenticated users can read and write data.
BNo one can read or write data, causing app failures.
CAnyone can read and write all data without restriction, risking data leaks and unauthorized changes.
DData is encrypted automatically, so no risk exists.
Attempts:
2 left
💡 Hint

Consider what 'if true' means for access control.

Architecture
expert
3:00remaining
How to design Firebase rules to allow users to edit only their own profile data?

You have a Firebase Realtime Database /users where each child key is a user's UID. Which rule correctly allows users to read and write only their own data?

Firebase
rules_version = '2';
service firebase.database {
  match /{database}/ {
    match /users/{userId} {
      allow read, write: if auth != null && auth.uid == userId;
    }
  }
}
Aallow read, write: if auth != null && auth.uid == userId;
Ballow read, write: if auth != null;
Callow read, write: if userId == 'admin';
Dallow read, write: if true;
Attempts:
2 left
💡 Hint

Match the authenticated user ID with the document ID.