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

Disable additional .array() modifier #406

Conversation

zxl629
Copy link
Contributor

@zxl629 zxl629 commented Dec 2, 2024

Related issues:

Description of changes:

This change disables customers from chaining .array() modifier in model field definition. It adds .array() method to the used method list after being called.

Example of why we need this:

Currently customers can declare a double array for a data field, which will cause errors and is not what we intended.

MatrixTest: a.model({ values: a.string().required().array().required().array().required() })
.authorization((allow) =>[allow.publicApiKey()])

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link

changeset-bot bot commented Dec 2, 2024

🦋 Changeset detected

Latest commit: 155fa09

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@zxl629 zxl629 marked this pull request as ready for review December 2, 2024 21:42
@zxl629 zxl629 requested review from a team as code owners December 2, 2024 21:42
Copy link
Member

@svidgen svidgen left a comment

Choose a reason for hiding this comment

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

In addition to addressing the comment on the internal symbol, let's also please add some testing. (Ideally in defined-behavior.)

Thanks!

Comment on lines 106 to 110
/**
* Internal non-omittable method to pass the test, won't be used by customers.
*/
[internal](): ModelField<T, UsedMethod>;
//TODO: should not be needed. Fix later
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand. 😅

What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that making every builder method omittable will break BaseModelField, as there is no method signature in BaseModelField type. Adding an internal method here solves the issue. However, this is not a clean solution and I am not sure how to work around it.

Copy link
Member

@stocaaro stocaaro Dec 3, 2024

Choose a reason for hiding this comment

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

I think this is the same problem being solved elsewhere with branding. Would a brand work to distinguish this?

Copy link
Member

Choose a reason for hiding this comment

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

OK. So, the fact that this is a method and not just a regular property is kind of incidental?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a brand property in ModelField. I could be wrong here but the errors showed up in ModelField.test-d.ts suggest TypeScript cannot infer the correct type any more unless there is a method in place for ModelField.

Copy link
Member

@iartemiev iartemiev Dec 4, 2024

Choose a reason for hiding this comment

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

@stocaaro - not the same problem. By making all methods omittable, we're breaking BaseModelField's ability to retain generic type information from ModelField. I'll try to illustrate below.

before this change:
image
Note: that there's nothing special about the array method, we just need at least one property on this type to reference ModelField's T type arg.

If we just omit array without adding another property:
image
This causes BaseModelField to lose the type information in T that was assigned or modified by the modifier methods on ModelField and the type argument defaults to ModelFieldTypeParamOuter. So any consumers of BaseModelField cannot infer any specific type metadata out of it, e.g. the underlying field type, nullability, "arrayness".

Adding another property that references T, keeps BaseModelField intact.
image

@svidgen

OK. So, the fact that this is a method and not just a regular property is kind of incidental?

Bingo. We just need some non-omittable property that maintains a reference to T. Simplest form would be:

{
  [internal]: T;
}

But that makes the implementation side a lot clunkier. I was only able to satisfy that type with a double assertion.

We can do e.g.

{
  [internal]: ModelType<T>;
}

And then something like

{
  ...,
  [internal]: _field(ModelFieldType.String)
}

In the implementation, but that still looks worse/more arbitrary than what we've got now with the method.

Copy link
Member

Choose a reason for hiding this comment

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

Ack. I think this makes sense.

@zxl629
Copy link
Contributor Author

zxl629 commented Dec 3, 2024

I have added a unit test in ModelField.test.ts file and a behavior test in 1-patterns/add-fields.ts file. I wonder if we need it in the defined behavior test since technically the errors shows up when user is writing the schema.

In addition to addressing the comment on the internal symbol, let's also please add some testing. (Ideally in defined-behavior.)

@zxl629 zxl629 requested a review from svidgen December 4, 2024 21:32
@zxl629 zxl629 merged commit abb0569 into aws-amplify:feat/reinvent-blocked-days/main Dec 6, 2024
7 checks passed
@zxl629 zxl629 deleted the lzhouq/fix/disallow-array-modifier branch December 6, 2024 17:23
iartemiev added a commit that referenced this pull request Dec 9, 2024
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.

4 participants