0
0
Firebasecloud~10 mins

Data validation rules in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Data validation rules
Client sends data write request
Firebase receives request
Check validation rules
Write data
Confirm success
When a client tries to write data, Firebase checks the validation rules. If data is valid, it writes; if not, it rejects.
Execution Sample
Firebase
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow write: if request.resource.data.age >= 18;
    }
  }
}
This rule allows writing user data only if the age is 18 or older.
Process Table
StepActionData SentValidation CheckResult
1Client sends write request{"age": 20}Check if age >= 18Valid - allow write
2Write data to database{"age": 20}N/AWrite successful
3Client sends write request{"age": 16}Check if age >= 18Invalid - reject write
4Reject write request{"age": 16}N/AWrite rejected with error
💡 Execution stops after write is allowed or rejected based on validation.
Status Tracker
VariableStartAfter Step 1After Step 3Final
request.resource.data.ageundefined2016N/A
validation resultundefinedtruefalseN/A
Key Moments - 2 Insights
Why does the write get rejected when age is 16?
Because the validation rule requires age to be 18 or older, as shown in execution_table step 3 where the check fails.
What happens if the data passes validation?
The data is written to the database successfully, as seen in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the validation result when age is 20?
ANo validation performed
BInvalid - reject write
CValid - allow write
DWrite rejected with error
💡 Hint
Check execution_table row 1 under 'Validation Check' and 'Result'
At which step does the write get rejected?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at execution_table rows 3 and 4 for rejection details
If the rule changed to age >= 21, what would happen at step 1 with age 20?
AWrite rejected
BNo change
CWrite allowed
DError in rule
💡 Hint
Compare the validation condition in execution_sample with the data in execution_table
Concept Snapshot
Firebase Data Validation Rules:
- Define rules to check data before writing
- Use conditions like 'age >= 18'
- If data passes, write allowed
- If data fails, write rejected
- Protects database from bad data
Full Transcript
When a client tries to write data to Firebase, the system checks the data against validation rules. For example, a rule might require the user's age to be 18 or older. If the data meets this rule, Firebase writes it to the database and confirms success. If not, Firebase rejects the write and sends an error. This process ensures only valid data is stored.