Skip to content

Bug: parser built-in schema accept empty Records array #3475

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

Closed
Jimmy89 opened this issue Jan 15, 2025 · 10 comments · Fixed by #3521 or #3524
Closed

Bug: parser built-in schema accept empty Records array #3475

Jimmy89 opened this issue Jan 15, 2025 · 10 comments · Fixed by #3521 or #3524
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped parser This item relates to the Parser Utility

Comments

@Jimmy89
Copy link

Jimmy89 commented Jan 15, 2025

Expected Behavior

Trying out the S3Schema parser with an empty records array passes validation. This should not occur.

Current Behavior

Output of success is true, while false was expected.

Code snippet

import { S3Schema } from '@aws-lambda-powertools/parser/schemas';
const event = { Records: [] };
const parsedEvent = await S3Schema.safeParseAsync(event);
console.log(parsedEvent.success); // <-- Output: true, expected: false  

Steps to Reproduce

Run code snippet above

Possible Solution

No response

Powertools for AWS Lambda (TypeScript) version

"@aws-lambda-powertools/parser": "2.13.0"

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

Zod version: 3.24.1
@Jimmy89 Jimmy89 added bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels Jan 15, 2025
@Jimmy89 Jimmy89 changed the title Bug: S3Schema of parser accepts invalid input Bug: S3Schema of parser accepts invalid input as success Jan 15, 2025
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation parser This item relates to the Parser Utility and removed triage This item has not been triaged by a maintainer, please wait labels Jan 15, 2025
@dreamorosi dreamorosi moved this from Triage to Backlog in Powertools for AWS Lambda (TypeScript) Jan 15, 2025
@dreamorosi
Copy link
Contributor

Hi @Jimmy89, thank you for opening this issue.

I can confirm that if Records is an empty array validation passes. While in practice this can never happen in Lambda, since no record means no event, and thus no invocation, I think we should fix the schema and set a minimum of one item.

@Jimmy89
Copy link
Author

Jimmy89 commented Jan 16, 2025

Thank you @dreamorosi for putting it in the current iteration.
I'm wondering (not tested) if other schema parsers might have the same issue. I agree that in practice this should not occur that fast. However, in our case, we invoke the lambda with multiple different input types. I found this issue by doing a simple test, checking if our if statements would detect it as non-s3 event.

@dreamorosi
Copy link
Contributor

Yes, I haven't checked all of them, but from spot checking a couple of them, all the built-in schemas we have that use z.array(something) would pass if the array is empty - which is a problem.

If you agree I'd like to modify the title of the issue to reflect this, so we can get them all fixed.

@Jimmy89
Copy link
Author

Jimmy89 commented Jan 16, 2025

@dreamorosi go ahead and adjust the title/content

@leandrodamascena
Copy link
Contributor

Just my 2 cents here... I agree that events that any service sends to Lambda have at least one record. Some of them (S3, Kinesis, SQS, SNS and others) are a group of records - a list - and TS Parser should not accept an empty list.

In theory, empty events will never happen in the real world (Services -> Lambda), but based on what you're trying to achieve, @Jimmy89 , and to improve the developer experience/feedback loop, yes, we need to validate this in our models.

@dreamorosi dreamorosi changed the title Bug: S3Schema of parser accepts invalid input as success Bug: parser built-in schema accept empty Records array Jan 16, 2025
@dreamorosi
Copy link
Contributor

I have started working on making changes to some of the schemas to address this bug.

I'll continue adding the .min(1) constraint where it makes sense over the next few PRs that I am planning on working on.

Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Jan 24, 2025
@dreamorosi dreamorosi moved this from Coming soon to Pending review in Powertools for AWS Lambda (TypeScript) Jan 24, 2025
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation and removed pending-release This item has been merged and will be released soon labels Jan 24, 2025
@dreamorosi
Copy link
Contributor

Reopening as there're still a few schemas to fix in #3521

@dreamorosi dreamorosi reopened this Jan 24, 2025
@github-project-automation github-project-automation bot moved this from Pending review to Coming soon in Powertools for AWS Lambda (TypeScript) Jan 24, 2025
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Jan 24, 2025
Copy link
Contributor

This is now released under v2.13.1 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Jan 28, 2025
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped parser This item relates to the Parser Utility
Projects
3 participants