neode icon indicating copy to clipboard operation
neode copied to clipboard

Validation error for empty strings

Open zirkelc opened this issue 3 years ago • 3 comments

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!

zirkelc avatar Sep 09 '20 17:09 zirkelc

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

adam-cowley avatar Sep 10 '20 09:09 adam-cowley

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 avatar Sep 10 '20 10:09 zirkelc

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)

zirkelc avatar Sep 10 '20 11:09 zirkelc