Skip to content
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

Add checks for parsing validate directive #3125

Open
wants to merge 32 commits into
base: feature/server-side-validation
Choose a base branch
from

Conversation

bobbyu99
Copy link
Contributor

@bobbyu99 bobbyu99 commented Jan 22, 2025

Description of changes

  • updated @validate to be repeatable in both definition and snapshot test
  • added validators.ts that checks for invalid use of validate directive, including:
    • the value for minLength maxLength is non-negative integer
    • specified validation type is compatible with the field type (string validation type for string field, numeric validation type for numeric field)
    • each validation type can only be used once on the same field
  • renamed __tests__ correctly
  • updated jest.config.js to ignore types.ts which has no executable codes, and to use globally defined coverage threshold

Description of how you validated changes

  • unit tests for the validators
    • test that length value passed into minLength maxLength is valid non-negative integers
    • test that validation type is applied to compatible field type
    • test that we disallow duplicate validation types on the same field

Checklist

  • PR description included
  • yarn test passes
  • E2E test run linked
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)
  • New AWS SDK calls or CloudFormation actions have been added to relevant test and service IAM policies
  • Any CDK or CloudFormation parameter changes are called out explicitly

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@bobbyu99 bobbyu99 changed the base branch from main to feature/server-side-validation January 22, 2025 05:17
`,
},
{
name: 'accepts length values of extremely large numbers beyond 64-bit range',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a surprising "pass" case to me. I assume that we will validate it because it parses into a number, but it will fail at runtime because it exceeds Java's Long.MAX_VALUE ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me check what the maximum/minimum value our resolver can accept

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This falls under a rare use case. We will use isNaN(parseInt(x)) until a customer complains about is.

describe('Valid usage', () => {
test.each([
{
name: 'accepts -Infinity value',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will also double check if Infinity is accepted in resolver

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This falls under a rare use case. We will use isNaN(parseFloat(x)) until a customer complains about is.

packages/amplify-graphql-validate-transformer/src/types.ts Outdated Show resolved Hide resolved
* Validates that length validation values (minLength, maxLength) are valid non-negative integers.
*/
const validateLengthValue = (config: ValidateDirectiveConfiguration): void => {
const value = parseInt(config.value, 10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend we use the Number() constructor rather than parseInt, since I assume we want to catch invalid constructions like "10z.0". parseInt("10z.0", 10) returns 10, but Number("10z.0") returns NaN, which I think is what we want?

Copy link
Contributor Author

@bobbyu99 bobbyu99 Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circling back to this conversation as I noticed that in default transformer, we do the following (link)

const validateInt = (x: string): boolean => !Number.isNaN(parseInt(x, 10));
const validateFloat = (x: string): boolean => !Number.isNaN(parseFloat(x));

This is currently the only place where we parse String to Number. I suggest that we keep numeric parsing consistent across both @default and @validate, so that it will not be a surprise to the customers that their parsing behaviors are different.

@bobbyu99 bobbyu99 marked this pull request as ready for review January 26, 2025 02:09
@bobbyu99 bobbyu99 requested review from a team as code owners January 26, 2025 02:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants