📖 Scenario: You are building a simple Firebase Realtime Database to store user profiles. Each user has a unique ID, a name, and an age. You want to make sure that the data entered is correct and safe by using Firebase data validation rules.
🎯 Goal: Create Firebase Realtime Database validation rules that allow only user profiles with a name as a non-empty string and an age as a number between 1 and 120. Also, ensure that the user ID is a valid key.
📋 What You'll Learn
Create a users node in the database
Add a validation rule to check that name is a string and not empty
Add a validation rule to check that age is a number between 1 and 120
Ensure the user ID key is a valid Firebase key
💡 Why This Matters
🌍 Real World
Firebase data validation rules protect your database from bad or malicious data by enforcing correct data formats and values.
💼 Career
Understanding Firebase rules is essential for backend and full-stack developers working with Firebase to ensure data integrity and security.
Progress0 / 4 steps
1
Create the users node structure
Create a Firebase Realtime Database rules object with a users node that allows read and write access for now.
Firebase
Hint
Start by allowing all reads and writes on the users node.
2
Add validation for name field
Inside the users node, add a $userId wildcard and create a name child validation rule that checks if newData.isString() and newData.val().length > 0.
Firebase
Hint
Use $userId as a wildcard for user keys and validate the name field inside it.
3
Add validation for age field
Inside the $userId node, add an age child validation rule that checks if newData.isNumber() and newData.val() >= 1 and newData.val() <= 120.
Firebase
Hint
Validate that age is a number between 1 and 120 inclusive.
4
Validate the $userId key format
Add a validation rule on the $userId wildcard to ensure the key matches the regex ^[a-zA-Z0-9_-]+$ using newData.exists() and newData.key.matches().
Firebase
Hint
Use newData.key.matches() to check the user ID format.
Practice
(1/5)
1. What is the main purpose of Firebase data validation rules?
easy
A. To ensure data is correct and secure before saving
B. To speed up database queries
C. To create user interfaces automatically
D. To backup data to another server
Solution
Step 1: Understand what data validation means
Data validation checks if data meets certain conditions before saving.
Step 2: Identify the role of Firebase rules
Firebase rules check data type, format, and permissions to keep data safe and clean.
Final Answer:
To ensure data is correct and secure before saving -> Option A
Quick Check:
Data validation = correctness and security [OK]
Hint: Validation means checking data before saving [OK]
Common Mistakes:
Thinking rules speed up queries
Confusing validation with UI creation
Assuming rules backup data
2. Which of the following is the correct syntax to allow only string data for a field name in Firebase rules?
easy
A. "name": { ".validate": "newData.isString()" }
B. "name": { ".validate": "newData.isNumber()" }
C. "name": { ".validate": "newData.exists()" }
D. "name": { ".validate": "newData.val() > 0" }
Solution
Step 1: Identify the method to check string type
Firebase uses newData.isString() to check if data is a string.
Step 2: Match the syntax with the correct method
"name": { ".validate": "newData.isString()" } uses newData.isString(), which is correct for string validation.
Final Answer:
"name": { ".validate": "newData.isString()" } -> Option A
Quick Check:
Check string type = newData.isString() [OK]
Hint: Use newData.isString() to validate strings [OK]
But users with emails like 'user@exampleacom' are still allowed. What is the error?
medium
A. matches() does not work in Firebase rules
B. The regex pattern is incorrect; it should escape the dot
C. newData.isString() is missing
D. The rule should use newData.exists() instead
Solution
Step 1: Review the regex pattern used
The pattern '.*@example.com$' treats '.' as any character, not a literal dot.
Step 2: Correct the regex to escape the dot
It should be '.*@example\.com$' to match the literal '.com' ending.
Final Answer:
The regex pattern is incorrect; it should escape the dot -> Option B
Quick Check:
Escape dot in regex = \. [OK]
Hint: Escape dots in regex with double backslash \ [OK]
Common Mistakes:
Not escaping '.' in regex
Thinking matches() is unsupported
Confusing isString() with exists()
5. You want to write a Firebase validation rule for a posts node where each post must have a title (string, max 100 chars) and tags (array of strings, max 5 tags). Which rule correctly enforces this?