Conditional type of field depending on sibling data

default discord avatar
Grandnainconnu
last month
10

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.

  • discord user avatar
    Jarrod
    Payload Team
    last month

    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

  • default discord avatar
    Grandnainconnu
    last month

    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

  • discord user avatar
    denolfe
    Payload Team
    last month

    An alternative would be to use a Block field with number and text blocks



    You'd likely need to intelligently populate, though

  • discord user avatar
    jmikrut
    Payload Team
    last month

    yeah GraphQL will automatically format your data based on the schema type

  • default discord avatar
    Grandnainconnu
    last month

    Yeah I thought about that but not sure I can force only one type of block depending on

    type
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.