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

Validation error for empty strings #140

Open
zirkelc opened this issue Sep 9, 2020 · 3 comments
Open

Validation error for empty strings #140

zirkelc opened this issue Sep 9, 2020 · 3 comments

Comments

@zirkelc
Copy link

zirkelc commented Sep 9, 2020

Hi,

the validator throws an error if a string property is empty, e.g. phone = ''. I defined the property phone on the model as follows:

phone: {
  type: 'string',
  required: false,
}

I get the following error stack trace:

{ Error: ERROR_VALIDATION
    at .../node_modules/neode/build/Services/Validator.js:297:23
    at internals.Object._validateWithOptions (.../node_modules/@hapi/joi/lib/types/any/index.js:766:20)
    at module.exports.internals.Any.root.validate (.../node_modules/@hapi/joi/lib/index.js:145:23)
    at .../node_modules/neode/build/Services/Validator.js:295:21
    at new Promise (<anonymous>)
    at Validator (.../node_modules/neode/build/Services/Validator.js:294:10)
    at .../node_modules/neode/build/Services/MergeOn.js:38:38
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
  details:
   [ { message: '"phone" is not allowed to be empty',
       path: [Array],
       type: 'any.empty',
       context: [Object] } ],
  input:
   { id: '3564889440353',
     phone: '' },
  _joiError:
   { ValidationError: child "phone" fails because ["phone" is not allowed to be empty]
       at Object.exports.process (.../node_modules/@hapi/joi/lib/errors.js:202:19)
       at internals.Object._validateWithOptions (.../node_modules/@hapi/joi/lib/types/any/index.js:763:31)
       at module.exports.internals.Any.root.validate (.../node_modules/@hapi/joi/lib/index.js:145:23)
       at .../node_modules/neode/build/Services/Validator.js:295:21
       at new Promise (<anonymous>)
       at Validator (.../node_modules/neode/build/Services/Validator.js:294:10)
       at .../node_modules/neode/build/Services/MergeOn.js:38:38
       at processTicksAndRejections (internal/process/next_tick.js:81:5)
     isJoi: true,
     name: 'ValidationError',
     details: [ [Object] ],
     _object:
      { id: '3564889440353',
        phone: ''},
     annotate: [Function] } }

I understand that this validation error comes from another library, however, I was wondering if there's a parameter which can be passed to enable empty strings. Otherwise, what is the desired outcome for required: false?

Thank you for sour support!

@adam-cowley
Copy link
Owner

Have you tried using the empty rule?

phone: {
  type: 'string',
  required: false,
  validation: { empty: true },
}

Personally I would clean the object so you're not storing empty strings

@zirkelc
Copy link
Author

zirkelc commented Sep 10, 2020

I don't see the validation property in VSCode intellisense, but could be due to the reason I'm using TypeScript. Thats what I see:

image

With cleaning the object you mean remove the property from the object or set it to undefined?

@zirkelc
Copy link
Author

zirkelc commented Sep 10, 2020

I was able to suppress the TypeScript error message for validation with a //@ts-ignore comment
However, now I get the following error:

Error: Not sure how to validate validation on phone
    at .../node_modules/neode/build/Services/Validator.js:273:15
    at Array.forEach (<anonymous>)
    at .../node_modules/neode/build/Services/Validator.js:247:25
    at Array.forEach (<anonymous>)
    at BuildValidationSchema (.../node_modules/neode/build/Services/Validator.js:165:23)
    at Validator (.../node_modules/neode/build/Services/Validator.js:293:16)
    at .../node_modules/neode/build/Services/MergeOn.js:38:38
    at processTicksAndRejections (internal/process/next_tick.js:81:5)

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