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.
You can include a validation option for a metafield definition using the validation name
and a corresponding value
. The appropriate value depends on the metafield type that the validation applies to. For more information about the supported formats and units of measurement available for each type, refer to Metafield types.
Learn more about creating a metafield definition with validation options and refer the to table below for more examples.
Supported validation options
Anchor link to section titled "Supported validation options"The following table describes the validation options available for metafield definitions:
Validation option | Description | Supported types | Example |
---|---|---|---|
Minimum length | A validation for setting the minimum length of a text value. Example use cases:
|
|
validations: |
Maximum length | A validation for setting the maximum length of a text value. Example use cases:
|
|
validations: |
Regular expression | A validation for setting a regular expression. Shopify supports RE2. Example use cases:
|
|
validations: |
Allowed domains | A list of allowed domains. Example use cases:
|
url |
validations: |
Choices | A list of up to 128 predefined options that limits the values allowed for the metafield. Example use cases:
|
single_line_text_field |
validations: |
File type options | A list of file type options. Leaving this empty allows all files, regardless of type. Valid values: Image , Video .Example use cases:
|
file_reference |
validations: |
Maximum precision | Sets the maximum number of decimal places to store for a decimal number. Example use cases:
|
number_decimal |
validations: |
Minimum date | A validation for setting the minimum date in ISO 8601 format. Example use case: The date field for an expiry date or a product release needs to be after a specific date. |
date |
validations: |
Maximum date | A validation for setting the maximum date in ISO 8601 format. Example use case: The date for future product availability can't be later than a certain date. |
date |
validations: |
Minimum datetime | A validation for setting the minimum date and time in ISO 8601 format. Example use case: A product release occurs on a specific date at midnight, and the product won't display before that date and time. |
date_time |
validations: |
Maximum datetime | A validation for setting the maximum date and time in ISO 8601 format. Example use case: A customer loyalty reward expires on a specific date and time. The reward can't be used after that date and time. |
date_time |
validations: |
Minimum weight | A validation for setting the minimum weight. Example use case: A bundled product weight field needs to have a maximum allowed weight. |
weight |
validations: |
Maximum weight | A validation for setting the maximum weight. Example use case: An order requires a minimum weight for shipping. |
weight |
validations: |
Minimum volume | A validation for setting the minimum volume. Example use cases:
|
volume |
validations: |
Maximum volume | A validation for setting the maximum volume. Example use cases:
|
volume |
validations: |
Minimum dimension | A validation for setting the minimum dimension. Example use case: A product needs to have a minimum length, width, or height. |
dimension |
validations: |
Maximum dimension | A validation for setting the maximum dimension. Example use case: A product needs to have a minimum length, width, or height. |
dimension |
validations: |
Minimum integer | A validation for setting the minimum integer number. Example use case: A product number needs to be within a specific range of numbers, so you can use both the minimum and maximum integer validation to set the range. |
number_integer |
validations: |
Maximum integer | A validation for setting the maximum integer number. Example use case: A product number needs to be within a specific range of numbers, so you use both the minimum and maximum integer validation to set the range. |
number_integer |
validations: |
Minimum decimal | A validation for setting the minimum decimal number. Example use case: A product version field needs to accept a version greater than a specific decimal number, such as 0.5 . |
number_decimal |
validations: |
Maximum decimal | A validation for setting the maximum decimal number. Example use case: A product version field needs to accept a version number less than a specific decimal numbner, such as 1.99 . |
number_decimal |
validations: |
Query the GraphQL Admin API for a list of supported validation options
Anchor link to section titled "Query the GraphQL Admin API for a list of supported validation options"The following example query returns the full list of supported validations for each type from the GraphQL Admin API:
POST /admin/api/2022-07/graphql.json
For an example query response that shows the complete list of types and supported validations, refer to the GraphQL reference documentation.
Use regular expressions for validation options
Anchor link to section titled "Use regular expressions for validation options"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/2022-07/graphql.json
Variables
JSON response
Add multiple validation options to a definition
Anchor link to section titled "Add 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/2022-07/graphql.json
Variables
JSON response