Validation Deep Dive — Field, field_validator & model_validator — Concept Check
Questions on Validation Deep Dive — Field, field_validator & model_validator. Field() handles declarative constraints (bounds, lengths, patterns); @field_validator runs custom logic per field; @model_validator checks rules across fields — all producing structured 422s.
Questions in this quiz
7 questions on Validation Deep Dive — Field, field_validator & model_validator — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses Field() to enforce a pattern on a pincode field. What happens if the input '012345' is provided?
- AValidation passes after normalizing to '12345'
- BValidation passes and stores '012345'
- CValidation fails due to incorrect length
- DValidation fails due to incorrect pattern
2.A model uses @field_validator to check phone numbers. What happens if '+91 98765 43210' is provided?
- AThe number is stored as '+919876543210'
- BThe number is stored as '9876543210'
- CValidation fails with a ValueError
- DThe number is stored without any changes
3.In a DriveWindow model, what happens if ends_on is before starts_on?
- AValidation passes but logs a warning
- BValidation fails only if online is False
- CValidation passes silently
- DValidation fails with a ValueError
4.A developer mistakenly believes @field_validator can make database calls. What is the correct approach?
- AUse service layer for DB calls
- BUse Field() with DB calls
- CUse @model_validator for DB calls
- DUse @field_validator with DB calls
5.A model uses @model_validator(mode='before'). What is its purpose?
- ATo reshape raw input before validation
- BTo enforce cross-field rules
- CTo validate fields after model construction
- DTo handle database interactions
6.A developer uses Field() for a cgpa field. What happens if 11 is provided?
- AValidation fails due to incorrect type
- BValidation passes and stores 11
- CValidation passes after normalizing to 10
- DValidation fails due to exceeding max value
7.A model uses @model_validator for cross-field validation. What happens if venue is None and online is False?
- AValidation passes with a warning
- BValidation fails only if ends_on < starts_on
- CValidation passes silently
- DValidation fails with a ValueError