CustomFields

CustomFields

CustomFields

Most entities can have additional fields added to them by defining an array of CustomFieldConfig objects on against the corresponding key.

Configuration options

All custom fields share some common properties:

  • name: string: The name of the field
  • type: string: A string of type CustomFieldType
  • label?: LocalizedString[]: An array of localized labels for the field.
  • description?: LocalizedString[]: An array of localized descriptions for the field.
  • public?: boolean: Whether or not the custom field is available via the Shop API. Defaults to true
  • defaultValue?: any: The default value when an Entity is created with this field.
  • nullable?: boolean: Whether the field is nullable in the database. If set to false, then a defaultValue should be provided.
  • validate?: (value: any) => string | LocalizedString[] | void: A custom validation function.

The LocalizedString type looks like this:

type LocalizedString = {
  languageCode: LanguageCode;
  value: string;
};

In addition to the common properties, the following field types have some type-specific properties:

string type

  • pattern?: string: A regex pattern which the field value must match
  • options?: { value: string; label?: LocalizedString[]; };: An array of pre-defined options for the field.

localeString type

  • pattern?: string: A regex pattern which the field value must match

int & float type

  • min?: number: The minimum permitted value
  • max?: number: The maximum permitted value
  • step?: number: The step value

datetime type

The min, max & step properties for datetime fields are intended to be used as described in the datetime-local docs

  • min?: string: The earliest permitted date
  • max?: string: The latest permitted date
  • step?: string: The step value

Example

bootstrap({
    // ...
    customFields: {
        Product: [
            { name: 'infoUrl', type: 'string' },
            { name: 'downloadable', type: 'boolean', defaultValue: false },
            { name: 'shortName', type: 'localeString' },
        ],
        User: [
            { name: 'socialLoginToken', type: 'string', public: false },
        ],
    },
})

Signature

interface CustomFields {
  Address?: CustomFieldConfig[];
  Collection?: CustomFieldConfig[];
  Customer?: CustomFieldConfig[];
  Facet?: CustomFieldConfig[];
  FacetValue?: CustomFieldConfig[];
  GlobalSettings?: CustomFieldConfig[];
  OrderLine?: CustomFieldConfig[];
  Product?: CustomFieldConfig[];
  ProductOption?: CustomFieldConfig[];
  ProductOptionGroup?: CustomFieldConfig[];
  ProductVariant?: CustomFieldConfig[];
  User?: CustomFieldConfig[];
}

Members

Address

property

Collection

property

Customer

property

Facet

property

FacetValue

property

GlobalSettings

property

OrderLine

property

Product

property

ProductOption

property

ProductOptionGroup

property

ProductVariant

property

User

property

Contents: