Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not allowing empty string #41

Open
Gilbert1391 opened this issue Jan 5, 2019 · 4 comments
Open

Not allowing empty string #41

Gilbert1391 opened this issue Jan 5, 2019 · 4 comments

Comments

@Gilbert1391
Copy link

Gilbert1391 commented Jan 5, 2019

It seems like joi browser does not allow to have empty string. This is my schema:

schema = {
    _id: Joi.string(),
    heading: Joi.string()
      .min(5)
      .max(50)
      .label("Heading")
      .required(),
    subHeading: Joi.string().max(50)
  };

If I leave the subHeading field empty (because it is not a required field) I get an error: ""subHeading" is not allowed to be empty"

Is there anyways to allow empty string in the validation?

@Marsup
Copy link

Marsup commented Jan 6, 2019

You should probably look at joi docs for joi usage, not here. The answer is Joi.string().allow('').

@Gilbert1391
Copy link
Author

You should probably look at joi docs for joi usage, not here. The answer is Joi.string().allow('').

Yeah I tried that and for some reason I'm getting a 400 bad request. I am using Node.js for the back-end and I made sure that this field is not required in my modal, something I can confirm using Postman. This must be a joi related issue (or something else is wrong with my code). When catch the error.response and print it on the console I get the following object:

{data: ""subHeading" is not allowed to be empty", status: 400, statusText: "Bad Request", headers: {…}, config: {…}, …}

The message is the same I was getting in the errors object before using .allow('')

@Gilbert1391
Copy link
Author

Ok I solved the issue. In my back-end joi validation schema I had to add allow("") and then I also had to add the same piece of code in my front-end joi-browser validation schema.

It is weird though... How come using Postman I was not getting any errors when passing an empty string, the joi validation should've thrown an error because I didn't specify allow('')

@Marsup
Copy link

Marsup commented Jan 6, 2019

Hard to tell without code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants