Is it possible to make a group field optional? Despite even an explicit
required: false
on the group, the type is generated as required. Am I missing anything?
I believe that group fields are set to required or not based on if there are any fields
withina group field that are required or not
if your group contains ONLY optional fields, it should be optionally typed
I see 🤔 So there is no way to say "This whole thing can either be undefined, or if it exists, you are guaranteed to have these fields in it"?
Especially for reused fields, that would help a lot
In my specific case, we got a custom file-input that connects to an external service for files (beyond what upload is capable of, sadly), and we'd love to express an optional file
But if a file is present, stuff like ID, filename and download URL should be required for instance
put a condition on the group
i think that could do it?
FYI - to calculate if a field should be marked as required in TS, here's the code:
https://github.com/payloadcms/payload/blob/master/src/utilities/entityToJSONSchema.ts#L10
notice how if the field has a condition, that will tell Payload to mark it as optional
Feels like a hack but that does indeed work, so I'll take it, thanks!
Out of curiosity: Why was the decision made to ignore the required field on things like groups?
just because a "group" doesn't really accept data on its own, it's up to the child fields to specify data
and
required
is really not relevant to most things that
required
typically means - like, validation, form state, etc
but i could see how it could affect the typescript types specifically, so i would not be opposed to respecting it within TS type gen
I see, thanks for the insight!
Maybe I'll find some time in the next days to give the TS gen thing a stab then!
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.