0
0
Expressframework~10 mins

Custom validation rules in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the express-validator check function.

Express
const { [1] } = require('express-validator');
Drag options to blanks, or click blank then click option'
Acheck
Bvalidate
Cverify
Dassert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'validate' or 'verify' instead of 'check'.
Forgetting to destructure the import.
2fill in blank
medium

Complete the code to create a custom validator that checks if a value is an even number.

Express
check('number').custom(value => value [1] 2 === 0);
Drag options to blanks, or click blank then click option'
A===
B%
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '===' instead of '%'.
Using '+' or '-' which do not check divisibility.
3fill in blank
hard

Fix the error in the custom validator to correctly reject odd numbers with an error message.

Express
check('number').custom(value => {
  if (value [1] 2 !== 0) {
    throw new Error('Number must be even');
  }
  return true;
});
Drag options to blanks, or click blank then click option'
A+
B===
C%
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '===' instead of '%'.
Using '+' or '-' which do not check divisibility.
4fill in blank
hard

Fill both blanks to create a custom validator that checks if a username starts with a capital letter and is at least 3 characters long.

Express
check('username').custom(value => {
  if (!(/^[2]/[1](value))) {
    throw new Error('Username must start with a capital letter');
  }
  if (value.length < 3) {
    throw new Error('Username must be at least 3 characters');
  }
  return true;
});
Drag options to blanks, or click blank then click option'
Amatch
Btest
CstartsWith
D[A-Z]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'match' which returns an array instead of boolean.
Using 'startsWith' which does not accept regex.
Using incorrect regex pattern.
5fill in blank
hard

Fill all three blanks to create a custom validator that checks if an email ends with '.com' and contains '@', and throws an error if not.

Express
check('email').custom(value => {
  if (!value[1]('@')) {
    throw new Error('Email must contain @');
  }
  if (!(/\.[3]$/[2](value))) {
    throw new Error('Email must end with .com');
  }
  return true;
});
Drag options to blanks, or click blank then click option'
Aincludes
Btest
Ccom
Dmatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'match' instead of 'test' for regex boolean check.
Using 'startsWith' instead of 'includes' for '@'.
Incorrect regex pattern for '.com'.