Joi validation string().trim() not working
Asked Answered
C

2

6

I am using @hapi/joi for express validation and sanitation. When validating, certain validators are not working. In this one, not only does trim() not validate for white space at the beginning and end of the input string but it also does not trim it as it is supposed to given that convert is set to true by default. However, the check for valid email and required both work and throw their respective errors. I also tried lowercase() and that did not validate or convert it to lowercase.

const Joi = require("@hapi/joi");

const string = Joi.string();

const localRegistrationSchema = Joi.object().keys({
  email: string
    .email()
    .trim()
    .required()
    .messages({
      "string.email": "Email must be a valid email address",
      "string.trim": "Email may not contain any spaces at the beginning or end",
      "string.empty": "Email is required"
    })
});
Couperin answered 4/4, 2020 at 1:10 Comment(0)
C
8

With version >= 17 of Joi you could write the schema as followed:

const localRegistrationSchema = Joi.object({ // changes here,
  email: Joi.string() // here
    .email()
    .trim()
    .lowercase() // and here
    .required()
    .messages({
      'string.email': 'Email must be a valid email address',
      'string.trim': 'Email may not contain any spaces at the beginning or end', // seems to be unnecessary
      'string.empty': 'Email is required'
    })
});

console.log(localRegistrationSchema.validate({ email: '' }));
// error: [Error [ValidationError]: Email is required]

console.log(localRegistrationSchema.validate({ email: '  [email protected]' }));
// value: { email: '[email protected]' }

console.log(localRegistrationSchema.validate({ email: '[email protected]  ' }));
// value: { email: '[email protected]' }

console.log(localRegistrationSchema.validate({ email: '[email protected]' }));
// value: { email: '[email protected]' }
Constellate answered 4/4, 2020 at 1:36 Comment(5)
Yeah, both this code and my original code work with console logs. I am using postman to test. Could it be how postman sends the form data? I am using x-www-form-urlencoded to send the form data to be validated.Couperin
Can you add an accordant curl command? Maybe I can test it then.Constellate
curl --location --request POST 'localhost:5000/api/auth/register/local' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'email= [email protected] ' \ --data-urlencode 'password=password' \ --data-urlencode 'confirmPassword=password' \ --data-urlencode 'username= foobar 'Couperin
Never mind. I was passing the original body to my database rather than the validated and sanitized value. And I wasn't getting the errors that I was expecting because it was sanitizing them and therefore the sanitized values had no errors.Couperin
Alright, sounds reasonable. And what works is usually fine :)Constellate
S
0

Try using trim() with strict()

const Joi = require("@hapi/joi");

const string = Joi.string();
const localRegistrationSchema = Joi.object().keys({
email: string
    .email()
    .trim()
    .strict()
    .required()
});
Schock answered 16/2, 2023 at 4:10 Comment(1)
strict disable the convert flag and trim needs it to work properlyRubinstein

© 2022 - 2024 — McMap. All rights reserved.