Parsing Raw Payloads
Support plan
n/a
- is this issue currently blocking your project? (yes/no): yes
- is this issue affecting a production system? (yes/no): no
Context
- node version: 12.19.0
- module version: 12.0.1
- environment (e.g. node, browser, native): node
- used with (e.g. hapi application, another framework, standalone, ...): hapi application
- any other relevant information:
How can we help?
My team is working on a project where we need to use a raw payload, as such:
payload: {
parse: false,
},
However, since we also want to utilize schema validations, we're calling the .validate() method as such:
const validationSchema = Joi.object();
const result = validationSchema.validate(request.payload.toString());
My problem is that the .validate() method expects a JSON object. However, I'm unable to parse the full payload.
Does anyone know of a solution or have any ideas?
I believe that if you put
const result = validationSchema.validate(JSON.parse(request.payload.toString()));
What you were sending was JSON text vs a JavaScript Object.
I believe that if you put
const result = validationSchema.validate(JSON.parse(request.payload.toString()));What you were sending was JSON text vs a JavaScript Object.
Thanks, @devinstewart. that was my thought too, but JSON.parse(request.payload.toString()) throws an error: "SyntaxError: Unexpected token O in JSON at position 535"
So that tells me that the string being created by:
request.payload.toString()
Is not valid JSON. I would venture to guess that the Unexpected token O is the beginning of the word "Object".
Is there a chance you could provide a console.log of the string created? You can obfuscate the values, it's the keys I'm interested in.
Oh @devinstewart, . thank you for pointing out my stupid mistakes. Yes, I was trying to parse an [Object].
good grief! and Thank you!!!!