I'm currently working for an e-commerce platform, trying to migrate our products attributes.
I have a schema like this:
{
name: "options",
label: {
en: "Options",
fr: "Options",
},
type: "array",
minRows: 0,
fields: [
{
type: "text",
name: "textValue",
label: {
en: "Value",
fr: "Valeur",
},
admin: {
condition: (data) => {
return (
data &&
typeof data === "object" &&
"type" in data &&
typeof data.type === "string" &&
["text", "color"].includes(data.type)
);
},
},
},
{
type: "number",
name: "numberValue",
label: {
en: "Value",
fr: "Valeur",
},
admin: {
condition: (data) => {
return (
data &&
typeof data === "object" &&
"type" in data &&
typeof data.type === "string" &&
["number"].includes(data.type)
);
},
},
},
{
type: "text",
name: "label",
label: {
en: "label",
fr: "label",
},
admin: {
condition: (data) => {
return (
data &&
typeof data === "object" &&
"type" in data &&
typeof data.type === "string" &&
["color"].includes(data.type)
);
},
},
},
],
},
Would like to know if there is a way to avoid creating
textValue
and
numberValue
and simply have something like:
{
name: "value",
type: (data) => {
if (data?.type === "number") {
return "number";
}
return "text";
},
label: "Value"
}
Couldn't find anything in the doc though.
No this is not something that payload supports. The field type is meant to be static in the config.
Instead I would clean your data before sending it into payload, so if you want the field to be a number, be sure it is a number 👍
you could use a beforeValidate hook to ensure a value is of a type before inserting it into the db
Ok I see, it's not that our data is dirty, is just sometimes we have like text attributes, and sometimes numbers
Yeah I tried but the output generated type is incorrect so not the best
and for some reason the graphql API still sends a text even after Number() conversion
An alternative would be to use a Block field with number and text blocks
You'd likely need to intelligently populate, though
yeah GraphQL will automatically format your data based on the schema type
Yeah I thought about that but not sure I can force only one type of block depending on
type
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.