#Condition for the required field type?

1 messages · Page 1 of 1 (latest)

wide sun
#
      name: 'media',
      type: 'upload',
      relationTo: 'media',
      required: true,
      admin: {
        condition: (_, { type } = {}) => ['highImpact'].includes(type),
      },
    },

    {
      name: 'lowImpactMedia',
      type: 'upload',
      relationTo: 'media',
      required: false,
      admin: {
        condition: (_, { type } = {}) => ['lowImpact'].includes(type),
      },
    },```

Is there a way to condense this code? For example, if I could set a condition for required, I could have it true if type is highImpact, false is lowImpact and cut out a few lines. But I don't think I can set conditions on the required field? What I'm trying to avoid is using another name more than anything, but you cannot have duplicate names.
tall wigeonBOT
buoyant kindle
#

hey @wide sun

#

Maybe you can define a function at the top like this

#
const conditionString = (string: string): Condition => {
    return (_, { type } = {}) => [string].includes(type)
}
#

then your condition would only be

#
      admin: {
        condition: conditionString('test')
      }
#

As for the required portion, you could try to instead use the validate property and pass in a custom validation function?

wide sun
#

hmm so I can't have "condition" in the required area?