Set validation rules
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. This guide shows you how to manage validation options using the GraphQL Admin API.
Requirements
Anchor link to section titled "Requirements"- Your app can make authenticated requests to the GraphQL Admin API.
- You're using the GraphQL Admin API version 2021-10 or higher.
- Your app has access to the namespace and the owner type associated with the metafield definition.
Step 1: Retrieve a list of supported validation options
Anchor link to section titled "Step 1: Retrieve 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:
Step 2: Create a metafield definition with validation options
Anchor link to section titled "Step 2: Create a metafield definition with validation options"The following example creates a metafield definition called Pizza size
with validation for the metafield to have a minimum size of 9 inches and a maximum size of 15 inches:
Step 3 (Optional): Use regular expressions for validation options
Anchor link to section titled "Step 3 (Optional): 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:
- Learn about the supported validation options for metafields.