Correct structure for validators in fields?

Am attempting to create image and gallery field types, but am having trouble getting the structure right and see no examples in the docs.

Here’s what I’m doing:

const componentImage = await client.fields.create(component.id, {
    label: 'Image',
    apiKey: 'image',
    fieldType: 'file',
    hint: 'Square assets work best',
    validators: {
      extension: 'image',
      requiredAltTile: {
        alt: true
      }
    },
    appearance: {
      editor: 'file',
      addons: [],
      parameters:{},
    },
  }).then((field) => {
    console.log(field);
  })
  .catch((error) => {
    console.error(error);
  });

The error I’m getting is this:

ApiException: 422 INVALID_FIELD (details: {"field":"validators","code":"VALIDATION_INVALID","message":"validators options must be an hash"})

I think you should look into either the file option:

file - Asset upload

  • editor: file
  • parameters: {}
  • validators:
    • required : {} make field required.
    • fileSize : an object with minValue , maxValue and minUnit , maxUnit . The units can be B , KB or MB . You can express only minimum values, only maximum values or both.
    • imageDimensions : an object with heightMaxValue , heightMinValue , widthMaxValue widthMinValue expressed in pixels. You can express only minimum values, only maximum values or both.
    • extension : either an object with predefinedList : image or video or document . Otherwise it should be an array of extensions allowed.
    • requiredAltTitle : an object with alt and title attributes that can have true or false values.

or the gallery one:

gallery - Gallery of assets

  • editor: gallery
  • parameters: {}
  • validators:
    • required : {} make field required.
    • fileSize : an object with minValue , maxValue and minUnit , maxUnit . The units can be B , KB or MB . You can express only minimum values, only maximum values or both.
    • imageDimensions : an object with heightMaxValue , heightMinValue , widthMaxValue widthMinValue expressed in pixels. You can express only minimum values, only maximum values or both.
    • extension : either an object with predefinedList : image or video or document . Otherwise it should be an array of extensions allowed.
    • requiredAltTitle : an object with alt and title attributes that can have true or false values.

does this help?

Looks like I had two mistakes in my request:

validators: {
      extension: {
        predefinedList: 'image'
      },
      requiredAltTitle: {
        alt: true,
        title: false
      }
    },

The validator for extension needed to be formatted differently, by supplying the object with predefinedList, and for requiredAltTitle I had to provide the title as well.

1 Like