Data validation rules in Firebase - Time & Space Complexity
When using Firebase data validation rules, it's important to know how the time to check data grows as more data is involved.
We want to understand how the validation process scales when many data items are checked.
Analyze the time complexity of validating multiple data entries with Firebase rules.
service cloud.firestore {
match /databases/{database}/documents {
match /items/{itemId} {
allow write: if request.resource.data.size() <= 100 &&
request.resource.data.keys().hasAll(['name', 'price']) &&
request.resource.data.price > 0;
}
}
}
This rule checks that each item written has a limited number of fields, required keys, and a positive price.
Look at what happens each time data is validated:
- Primary operation: Checking each field in the data object against the rules.
- How many times: Once per field in the data being written.
As the number of fields in the data grows, the validation checks grow too.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 field checks |
| 50 | About 50 field checks |
| 100 | About 100 field checks |
Pattern observation: The number of checks grows directly with the number of fields.
Time Complexity: O(n)
This means the time to validate grows in a straight line as the data size grows.
[X] Wrong: "Validation time stays the same no matter how much data is checked."
[OK] Correct: Each field must be checked, so more fields mean more work and longer validation time.
Understanding how validation scales helps you design efficient rules and shows you can think about system performance clearly.
"What if the validation rules included nested objects? How would that affect the time complexity?"
Practice
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 AQuick Check:
Data validation = correctness and security [OK]
- Thinking rules speed up queries
- Confusing validation with UI creation
- Assuming rules backup data
name in Firebase rules?Solution
Step 1: Identify the method to check string type
Firebase usesnewData.isString()to check if data is a string.Step 2: Match the syntax with the correct method
"name": { ".validate": "newData.isString()" } usesnewData.isString(), which is correct for string validation.Final Answer:
"name": { ".validate": "newData.isString()" } -> Option AQuick Check:
Check string type = newData.isString() [OK]
- Using isNumber() for strings
- Using exists() which only checks presence
- Using val() > 0 which is for numbers
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid",
".validate": "newData.hasChildren(['name', 'age']) && newData.child('age').isNumber() && newData.child('age').val() >= 18"
}
}
}
}What happens if a user tries to write data with
age set to 16?Solution
Step 1: Analyze the validation condition for age
The rule requiresageto be a number and at least 18.Step 2: Check the given age value
The user tries to writeage = 16, which is less than 18, so validation fails.Final Answer:
Write is denied because age is less than 18 -> Option CQuick Check:
Age must be ≥ 18 = deny if 16 [OK]
- Ignoring the minimum age check
- Assuming any number is allowed
- Confusing write and validate rules
{
".validate": "newData.isString() && newData.val().matches('.*@example.com$')"
}But users with emails like 'user@exampleacom' are still allowed. What is the error?
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 BQuick Check:
Escape dot in regex = \. [OK]
- Not escaping '.' in regex
- Thinking matches() is unsupported
- Confusing isString() with exists()
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?Solution
Step 1: Check the available Firebase validation functions
Firebase rules do not support JavaScript array methods likeevery()ortypeofinside rules.Step 2: Identify the valid checks
We can check iftagsis an array and its length, but cannot check each tag's type in rules.Final Answer:
"posts": { "$postid": { ".validate": "newData.hasChildren(['title', 'tags']) && newData.child('title').isString() && newData.child('title').val().length() <= 100 && newData.child('tags').numChildren() <= 5" } } -> Option DQuick Check:
Use only supported rule functions, no JS methods [OK]
- Using JavaScript methods inside rules
- Trying to check each array element's type
- Assuming isArray() checks element types
Ready to prove it?
Pass the test and this pattern moves unknown
