Validation options

Metafield definitions can have validation options. Validation options enable you to apply additional constraints to the data that a metafield can store, such as a minimum or maximum value, or a regular expression. The type of the metafield definition determines which validation options are available.

A validation option has two parts:

  • Name: The name of validation option. Refer to the list of available validation options.
  • Value: The value of the validation option. The value depends on the type of validation option.

Supported validation options

The following table lists the supported validation options by type.

Available validation options
Validation option Description Supported types Example
MIN_LENGTH An option for setting the minimum length of a text value.
  • single_line_text_field
  • multi_line_text_field
"validations": [{
  "name": "min",
  "value": "8"
}]
    
MAX_LENGTH An option for setting the maximum length of a text value.
  • single_line_text_field
  • multi_line_text_field
"validations": [{
  "name": "max",
  "value": "25"
}]
    
REGEX An option for setting a regular expression. Shopify supports RE2.
  • single_line_text_field
  • multi_line_text_field
"validations": [{
  "name": "regex",
  "value": "(@)(.+)$"
}]
    
ALLOWED_DOMAINS A list of allowed domains.
  • url
"validations": [{
  "name": "allowed_domains",
  "value": "[\"shopify.com\"]"
}]
    
FILE_TYPE_OPTIONS A list of file type options. Valid values: Image, GenericFile
  • file_reference
"validations": [{
  "name": "file_type_options",
  "value": "[\"Image\",\"GenericFile\"]"
}]
    
MAX_PRECISION Sets the maximum number of decimal places to store for a decimal number.
  • number_decimal
"validations": [{
  "name": "max_precision",
  "value": "2"
}]
    
MIN_DATE An option for setting the minimum date in ISO 8601 format.
  • date
"validations": [{
  "name": "min",
  "value": "2021-01-01"
}]
    
MAX_DATE An option for setting the maximum date in ISO 8601 format.
  • date
"validations": [{
  "name": "max",
  "value": "2021-03-03"
}]
    
MIN_DATE_TIME An option for setting the minimum date and time in ISO 8601 format.
  • date_time
"validations": [{
  "name": "min",
  "value": "2021-01-01T12:30:00"
}]
    
MAX_DATE_TIME An option for setting the maximum date and time in ISO 8601 format.
  • date_time
"validations": [{
  "name": "max",
  "value": "2021-03-03T16:30:00"
}]
    
MIN_WEIGHT An option for setting the minimum weight.
  • weight
"validations": [{
  "name": "min",
  "value": "{\"unit\":\"g\",\"value\":10}"
}]
    
MAX_WEIGHT An option for setting the maximum weight.
  • weight
"validations": [{
  "name": "max",
  "value": "{\"unit\":\"g\",\"value\":50}"
}]
    
MIN_VOLUME An option for setting the minimum volume.
  • volume
"validations": [{
  "name": "min",
  "value": "{\"unit\":\"ml\",\"value\":5}"
}]
    
MAX_VOLUME An option for setting the maximum volume.
  • volume
"validations": [{
  "name": "max",
  "value": "{\"unit\":\"ml\",\"value\":50}"
}]
    
MIN_DIMENSION An option for setting the minimum dimension.
  • dimension
"validations": [{
  "name": "min",
  "value": "{\"unit\":\"cm\",\"value\":5}"
}]
    
MAX_DIMENSION An option for setting the maximum dimension.
  • dimension
"validations": [{
  "name": "max",
  "value": "{\"unit\":\"cm\",\"value\":50}"
}]
    
MIN_INTEGER An option for setting the minimum integer number.
  • number_integer
"validations": [{
  "name": "min",
  "value": "9"
}]
    
MAX_INTEGER An option for setting the maximum integer number.
  • number_integer
"validations": [{
  "name": "max",
  "value": "15"
}]
    
MIN_DECIMAL An option for setting the minimum decimal number.
  • number_decimal
"validations": [{
  "name": "min",
  "value": "0.5"
}]
    
MAX_DECIMAL An option for setting the maximum decimal number.
  • number_decimal
"validations": [{
  "name": "max",
  "value": "1.99"
}]
    

Fetching supported validations from our API

You can also fetch the full list of supported validations for each type from the GraphQL Admin API, as shown in the following example.

POST /admin/api/unstable/graphql.json

Regular expressions

For certain definition types, you can add a regular expression as a validation option. Shopify supports RE2 syntax for regular expressions.

The following example creates a metafield definition called Email with the type single_line_text_field and adds a validation option to confirm that the value of the metafield contains the @ symbol with at least one character after it.

POST /admin/api/unstable/graphql.json

To learn more about regular expressions, you can visit a learning website such as RegexOne.

Adding multiple validation options to a definition

You can add multiple validation options to the same metafield definition. The following example creates a metafield definition called Employee code with a minimum character length of 4 and a maximum character length of 10.

POST /admin/api/unstable/graphql.json