0
0
Expressframework~5 mins

Custom validation rules in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom validation rule in Express?
A custom validation rule is a user-defined function that checks if input data meets specific conditions beyond built-in validations.
Click to reveal answer
beginner
How do you add a custom validation rule using express-validator?
Use the .custom() method on a validation chain and provide a function that returns true or throws an error if validation fails.
Click to reveal answer
intermediate
What should a custom validation function return or throw?
It should return true if validation passes or throw an error (or return a rejected promise) if validation fails.
Click to reveal answer
intermediate
Why use custom validation rules instead of built-in ones?
Custom rules let you check complex or unique conditions specific to your app that built-in validators can't handle.
Click to reveal answer
beginner
Give an example of a simple custom validation rule in Express.
Example: .custom(value => { if (!value.startsWith('A')) throw new Error('Must start with A'); return true; }) checks if input starts with 'A'.
Click to reveal answer
Which method is used to create a custom validation rule in express-validator?
A.validate()
B.custom()
C.checkCustom()
D.rule()
What should a custom validation function do if the input is invalid?
AIgnore the input
BReturn true
CReturn false silently
DThrow an error or return a rejected promise
Why might you need a custom validation rule?
ATo handle unique or complex input checks not covered by built-in validators
BTo speed up server response
CTo check simple required fields
DTo replace all built-in validators
Which package is commonly used with Express for validation including custom rules?
Aexpress-validator
Bbody-parser
Ccors
Dmongoose
What happens if a custom validation function returns true?
AValidation is skipped
BValidation fails
CValidation passes
DServer crashes
Explain how to create and use a custom validation rule in Express with express-validator.
Think about how you tell express-validator to run your own check.
You got /4 concepts.
    Why are custom validation rules important in web applications using Express?
    Consider what built-in validators might miss.
    You got /4 concepts.