adam-cowley / neode

Neo4j OGM for Node.js
MIT License
394 stars 72 forks source link

Validation error for empty strings #140

Open zirkelc opened 4 years ago

zirkelc commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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)