aws-amplify / aws-amplify/amplify-data
Can't set customType as required
- Dominant language
- TypeScript
- Stars
- 18
- Forks
- 23
- Avg merge
- 26m
- Merged PRs (30d)
- 1
Description
### Environment information
```plain text
System:
OS: macOS 14.5
CPU: (10) arm64 Apple M1 Pro
Memory: 155.58 MB / 32.00 GB
Shell: /bin/zsh
Binaries:
Node: 22.2.0 - ~/.nvm/versions/node/v22.2.0/bin/node
Yarn: undefined - undefined
npm: 10.7.0 - ~/.nvm/versions/node/v22.2.0/bin/npm
pnpm: undefined - undefined
NPM Packages:
@aws-amplify/backend: 1.0.3
@aws-amplify/backend-cli: 1.0.4
aws-amplify: 6.3.7
aws-cdk: 2.147.0
aws-cdk-lib: 2.147.0
typescript: 5.4.5
AWS environment variables:
AWS_PROFILE = amplify-deploy
AWS_STS_REGIONAL_ENDPOINTS = regional
AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables
```
### Data packages
```plain text
entrancecode@0.0.0 /Users/jpangburn/Documents/vsc_workspaces/entrancecode
├─┬ @aws-amplify/backend-cli@1.0.4
│ └─┬ @aws-amplify/schema-generator@1.0.0
│ └── @aws-amplify/graphql-schema-generator@0.8.6
└─┬ @aws-amplify/backend@1.0.3
└─┬ @aws-amplify/backend-data@1.0.2
└── @aws-amplify/data-construct@1.8.5
```
### Description
Suppose I have the following model or customType:
```
NotificationSubscription: a
.customType({
endpoint: a.string().required(),
keys: a.customType({
p256dh: a.string().required(),
auth: a.string().required(),
}),
}),
```
I want the `keys` property to be required, but there's no `required()` method on `customType`. So the only way to achieve this is to extract it to its own customType and use a ref to it:
```
SubscriptionKeys: a
.customType({
p256dh: a.string().required(),
auth: a.string().required(),
}),
NotificationSubscription: a
.customType({
endpoint: a.string().required(),
keys: a.ref('SubscriptionKeys').required(),
}),
```
As discussed heavily in other issues, this is also the only way to have a nested customType be an array or to set authorization rules on it.
If a data model is even moderately hierarchical this rapidly becomes hard to read, hard to maintain, and generally unwieldy. I think this really needs to be setup so we can do:
```
NotificationSubscription: a
.customType({
endpoint: a.string().required(),
keys: a.customType({
p256dh: a.string().required(),
auth: a.string().required(),
}).array().required().authorization(), // etc.
}),
```
If it's possible with the verbose syntax, it should be possible with simplified syntax allowing a more complicated model that's stored in one table to be expressed much more succinctly.
Thank you for your consideration!
Contributor guide
Research direction
Start by reading the customType and field-builder APIs in the TypeScript codebase, then trace how nested custom types are represented and validated. The issue names no source files or tests, so locate existing coverage for customType, required fields, arrays, and authorization before assessing scope. Done would mean nested customType values support the requested chaining behavior without requiring extraction into a separate type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend, database
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 34/100