Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Conditional type of field depending on sibling data

default discord avatar
grandnainconnu2 years ago
5

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_not_jared
    2 years ago

    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
    grandnainconnu2 years ago

    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
    2 years ago

    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
    2 years ago

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

  • default discord avatar
    grandnainconnu2 years ago

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

    type
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.