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?
✗ Incorrect
The .custom() method allows you to define your own validation logic.
What should a custom validation function do if the input is invalid?
✗ Incorrect
Throwing an error or returning a rejected promise signals validation failure.
Why might you need a custom validation rule?
✗ Incorrect
Custom rules handle special cases that built-in validators can't.
Which package is commonly used with Express for validation including custom rules?
✗ Incorrect
express-validator provides easy validation including custom rules.
What happens if a custom validation function returns true?
✗ Incorrect
Returning true means the input passed the custom validation.
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.