Serverless Event object failed validation at createError - node.js

I am working on a serverless/nodejs12 project. The project deploys fine, but when I try to invoke the endpoint with postman I get the following error. I have browsed through many postings of similar errors but still failed to understand what's going on. Will appreciate any pointers.
Thank you
at createError (/var/task/src/handlers/webpack:/home/serverless-workspace/notification/node_modules/#middy/util/index.js:259:1)
at validatorMiddlewareBefore (/var/task/src/handlers/webpack:/home/serverless-workspace/notification/node_modules/#middy/validator/index.js:55:1)
at runMiddlewares (/var/task/src/handlers/webpack:/home/serverless-workspace/notification/node_modules/#middy/core/index.js:120:1)
at runRequest (/var/task/src/handlers/webpack:/home/serverless-workspace/notification/node_modules/#middy/core/index.js:80:1) {
details: [
{
instancePath: '/body',
schemaPath: '#/properties/body/type',
keyword: 'type',
params: [Object],
message: 'must be object'
}
]
createNotification.js
--------------------------
async function createNotification(event, context) {
const { title } = event.body;
const { destination } = event.body;
const { destinationType } = event.body
const notification = {
id: uuid(),
title,
destination,
destinationType,
status: 'OPEN',
createdAt: new Date().toISOString(),
}
try {
await dynamodb.put({
TableName: process.env.NOTIFY_TABLE_NAME,
Item: notification
}).promise();
} catch (error) {
console.error(error);
throw new createError.InternalServerError(error);
}
return {
statusCode: 200,
body: JSON.stringify(notification),
};
}
export const handler = commonMiddleware(createNotification)
.use(validator({ inputSchema: createNotificationSchema }));
and the schema
----------------------
const schema = {
type: 'object',
properties: {
body: {
type: 'object',
required: ['status'],
default: { status: 'OPEN' },
properties: {
status: {
default: 'OPEN',
enum: ['OPEN', 'CLOSED'],
},
},
},
},
required: ['body'],
};
export default schema;

You're getting the error must be object. I'm guessing your input looks like { event: { body: 'JSON string' } }. You'll need to use another middy middleware to parse the body prior to validating the input. Which middleware will depend on what AWS event it's expecting. Middy >3.0.0 supports all AWS events.

Related

AJV validation doesn't return multiple errors when different values are missing in the fastify request body

I have a fastify server with a post endpoint. It accepts a JSON request body with a validation. Server code below:
const fastify = require("fastify");
const server = fastify({
ajv: {
customOptions: {
allErrors: true,
},
},
logger: true,
});
const schema = {
schema: {
body: {
type: "object",
properties: {
data: {
type: "array",
items: {
type: "object",
properties: {
foo: {
type: "string",
},
bar: {
type: "string",
},
},
required: ["foo", "bar"],
},
},
},
required: ["data"],
},
},
};
server.post("/", schema, function (request, reply) {
console.log({
request: {
body: JSON.stringify(request.body),
},
});
reply.send({ message: "hello" });
});
server.listen(3000, function (err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
console.log(`server listening on ${address}`);
});
Below is a valid request body.
{ "data":[{ "bar": "bar exists", "foo": "foo exists" }]}
When I try to access the same server with multiple values in input missing i.e.,
{ "data":[{ "bar": "bar exists, foo missing" }, {}] }
I am getting the below response.
{
"statusCode": 400,
"error": "Bad Request",
"message": "body.data[0] should have required property 'foo', body.data[1] should have required property 'foo', body.data[1] should have required property 'bar'"
}
I want to get each error separately, instead of getting a single large error message as this request can go very large. I have tried a bit of trial around the ajv options but couldn't find anything.
Any help is appreciated. Cheers :)
You need to have a custom parser after the error is caught.
In order to achieve this approach, there is a method called setErrorHandler.
According to Fastify documentation:
Set a function that will be called whenever an error happens.
This is a simple parser, but you may need to change it to your taste:
server.setErrorHandler(function (error, request, reply) {
if (error.validation) {
reply.status(400).send({
statusCode: error.statusCode,
error: 'Bad Request',
message: error.validation.map(err => `${err.instancePath} ${err.message}`)
});
}
})
// rest of code

Node.Js Elasticsearch Responding Blank _source

I have created one sample index using elasticsearch and node.js with below code setup.
const { Client } = require('#elastic/elasticsearch');
const { ELASTIC_SEARCH } = require('../config');
// Elastic Search Cloud Client Setup
const elasticClient = new Client({
cloud: { id: ELASTIC_SEARCH.CLOUDID },
auth: {
apiKey: ELASTIC_SEARCH.API_KEY
}
});
async function prepareIndex() {
const merchantIndexExists = await elasticClient.indices.exists({ index: 'index2' });
if (merchantIndexExists) return;
await elasticClient.indices.create({
index: 'index2',
body: {
mappings: {
dynamic: 'strict',
properties: {
company_name: { type: 'text' },
company_email: { type: 'keyword' },
name: { type: 'text' },
price: { type: 'scaled_float', scaling_factor: 10 },
created_date: { type: 'date' },
is_delete: { type: 'boolean', doc_values: false },
merchant: { type: 'keyword', index: 'true' }
}
}
}
});
}
After index creation i have added document with below code:
const { company_name, company_email, price } = req.body;
const response = await elasticClient.index({
index: 'index2',
document: {
company_email,
company_name,
price
}
});
Now when I'm calling search API from my kibana cloud console it's returning the exact search results with all the fileds. like
But when I'm hitting same search query via code in postman it's returning blank _source. Here is the search query with postman response
const response = await elasticClient.search({
index: 'index2',
query: {
match_all: {}
}
});
Can anyone please help me out of this?
When using the Node.js ElasticSearch client, you have to wrap the query into a body property and pass it to the search.
const response = await elasticClient.search({
index: 'index2',
body: {
query: {
match_all: {}
}
}
});

Recaptcha v3 Invalid Input Response

I'm having a problem when trying to apply recaptcha into my web app.
Basically, it's returning the error message: "invalid-input-response"
What could I be doing wrong?
Stack:
#nuxtjs/recaptcha 1.1.1
express-recaptcha 5.1.0
nuxt 2.15.8
node 16.15.9
Here is my configuration on front, I don't have certain about the recaptcha mode, if I should use base or enterprise.
nuxt.config.js
modules: [
'#nuxtjs/recaptcha',
],
recaptcha: {
hideBadge: true,
mode: 'base',
siteKey: 'MY_SITE_KEY',
version: 3,
size: 'normal',
},
On index I don't know if has any problem here
Based on docs, it's only do that
On my action I'm sending the token alongside my data on that format
{
token: 'asdadsadadasdas',
review: {...}
}
index.vue
async mounted() {
try {
await this.$recaptcha.init()
} catch (e) {
console.log(e);
}
},
methods: {
...mapActions({
getProduct: "getProduct",
postReview: "postReview",
}),
async submit() {
const postReviewToken = await this.$recaptcha.execute('postReview')
try {
await this.postReview({
token: postReviewToken,
productId: this.$route.params.productId,
review: {
title: this.review.title,
content: this.review.content,
},
});
this.getProduct({ productId: this.$route.params.productId });
this.review = {
title: "",
content: "",
};
} catch (error) {}
},
},
Node
And on my api I just added the middleware as docs require
import { RecaptchaV3 } from 'express-recaptcha/dist'
const recaptcha = new RecaptchaV3(
'MY_SITE_KEY',
'MY_SECRET_KEY'
)
routes.post('/product/:id/review', recaptcha.middleware.verify, async (request: Request, response: Response) => {
if (request.recaptcha?.error) {
return response.status(400).json({ message: request.recaptcha.error })
}
const review = await prisma.review.create({
data: {
productId: request.params.id,
title: request.body.review.title,
content: request.body.review.content,
},
select: {
id: true,
title: true,
content: true,
}
})
response.json({review});
});

Serverless can't fetch all records Event object failed validation?

I am trying to fetch all records using query and JSON schema but I am keep getting Event object failed validation unless I pass a query it didn't give me any result.
I am trying to fetch all the records that have status=OPEN
I set the default value of status=OPEN but it looks like default value is working.
Unless I pass the status=OPEN as a query
Please help me!!!
And used #middy/validator for this case anyone it's been 2 days I still can't figured out the problem
JSON Schema file
const getAuctionsSchema = {
type: 'object',
required: ['queryStringParameters'],
properties: {
queryStringParameters: {
type: 'object',
required: ['status'],
properties: {
status: {
default: 'OPEN',
enum: ['OPEN', 'CLOSED'],
},
},
},
},
};
module.exports = getAuctionsSchema;
Here all records fetch file
const AWS = require('aws-sdk');
const createError = require('http-errors');
const validator = require('#middy/validator');
const commonMiddleware = require('../lib/commonMiddleware');
const getAuctionsSchema = require('../lib/schemas/getAuctionsSchema');
const dynamoDB = new AWS.DynamoDB.DocumentClient();
const get_auctions = async (event) => {
const { status } = event.queryStringParameters;
let auctions;
const params = {
TableName: process.env.AUCTIONS_TABLE_NAME,
IndexName: 'statusAndEndDate',
KeyConditionExpression: '#status = :status',
ExpressionAttributeValues: {
':status': status,
},
ExpressionAttributeNames: {
'#status': 'status',
},
};
try {
const result = await dynamoDB.query(params).promise();
auctions = result.Items;
} catch (err) {
console.log(err);
throw new createError.InternalServerError(err);
}
return {
statusCode: 200,
body: JSON.stringify(auctions),
};
};
module.exports = {
handler: commonMiddleware(get_auctions).use(
validator({
inputSchema: getAuctionsSchema,
ajvOptions: {
useDefaults: true,
strict: false,
},
})
),
};
Here is the error I can see in Cloud Watch
ERROR BadRequestError: Event object failed validation
at createError (/var/task/node_modules/#middy/util/index.js:259:10)
at validatorMiddlewareBefore (/var/task/node_modules/#middy/validator/index.js:55:21)
at runMiddlewares (/var/task/node_modules/#middy/core/index.js:120:88)
at async runRequest (/var/task/node_modules/#middy/core/index.js:80:5) {
details: [
{
instancePath: '',
schemaPath: '#/required',
keyword: 'required',
params: [Object],
message: 'must have required property queryStringParameters'
}
]
}
The validator is expecting a queryStringParameters property of type object. According to the JSON Schema Specification for Objects, if a property is declared as having a certain type, that property fails validation if it is has a different type.
If you don't pass any query parameters to Api Gateway (in a Lambda Proxy integration), queryStringParameters will be null, but you have specified that it must be an object and null is not an object.
It is possible to specify several allowed types in the Schema: type: ['object', 'null']. You can read more about using several types here.
EDIT:
To be able to set status to 'OPEN' even when queryStringParameters is null in the query, you can give queryStringParameters a default value (an object), with status set to 'OPEN'):
const getAuctionsSchema = {
type: 'object',
required: ['queryStringParameters'],
properties: {
queryStringParameters: {
type: 'object',
required: ['status'],
default: {status: 'OPEN'},
properties: {
status: {
default: 'OPEN',
enum: ['OPEN', 'CLOSED'],
},
},
},
},
};

Parse xml request body using loopback4

I'm looking for an extension to existing body parsers.. I have a requirement to parse xml data using loopback4 node.. I have tried setting content-type to application/xml but it is giving me an unsupported content type error. xml is not supported. I did not find any examples in loopback4 documentation.
I have tried below code:
#post('/dashboard/parseXmlRequest/', {
responses: {
'200': {
description: 'parses the xml and sends the response',
content: {
'application/xml': {
schema: {
type: 'any'
}
}
},
},
},
})
async parseXmlRequest(
#requestBody({
content: {
'application/xml': {
schema: {type: 'object'},
},
},
})
data: object) {
try {
console.log("request : ", data);
return await this.myService.deviceResponse(body);
}
catch (err) {
console.log(err);
return err;
}
}
Can you please help.. thank you.
have you made any progress?
As workaround, I'm using text/plain instead xml;
#post('/data/import', {
responses: {
'200': {
description: 'Import XML',
content: {'application/json': {type: 'any' }},
},
},
})
async import(
#requestBody({
content: {
'text/plain': {
},
},
})
xml : string,
): Promise<string> {
return this.dataRepository.import(xml);
}
async import( xml: string) : Promise<string> {
try {
console.log("XML =>",xml);
const result = await xml2js.parseStringPromise(xml, {mergeAttrs: true});
const json = JSON.stringify(result, null, 4);
return json;
} catch (err) {
console.log(err);
return Promise.resolve(err);
}
}

Resources