--- title: Validation Settings API description: >- The Validation Settings API lets you create complex validation rules for cart and checkout validation. Use this API to build custom settings interfaces for Shopify Functions that implement validation logic. api_version: 2026-01 api_name: admin-extensions source_url: html: >- https://shopify.dev/docs/api/admin-extensions/latest/target-apis/contextual-apis/validation-settings-api md: >- https://shopify.dev/docs/api/admin-extensions/latest/target-apis/contextual-apis/validation-settings-api.md --- # Validation Settings API **Requires the \[function settings]\(/docs/api/admin-extensions/latest/web-components/forms/function-settings) component.:** The Validation Settings API lets you [create complex validation rules](https://shopify.dev/docs/apps/build/checkout/cart-checkout-validation/create-admin-ui-validation) for cart and checkout validation. Use this API to build custom settings interfaces for [Shopify Functions](https://shopify.dev/docs/apps/build/functions) that implement validation logic. ### Use cases * **Validation configuration:** Build settings interfaces for cart and checkout validation Functions. * **Metafield management:** Store and update validation rule configuration in metafields. * **Settings UI:** Create custom settings experiences for validation logic configuration. * **Function integration:** Configure Shopify Functions that implement validation behavior. ### Support Targets (1) ### Supported targets * [admin.​settings.​validation.​render](https://shopify.dev/docs/api/admin-extensions/2026-01/targets/function-settings#validation-function-settings-) ## apply​Metafield​Change(**[change](#applymetafieldchange-propertydetail-change)**​) Applies a [metafield](https://shopify.dev/docs/apps/build/metafields) change to the validation settings. Use this method to update or remove metafields that store validation function configuration data. The method accepts a change object specifying the operation type, metafield key, namespace, value, and [value type](https://shopify.dev/docs/apps/build/metafields/list-of-data-types). Returns a promise that resolves to indicate success or provides an error message if the operation fails. ### Parameters * **change** **MetafieldChange** **required** ### Returns * **Promise\** ### MetafieldChange A metafield change operation that can either update or remove a metafield. Pass this to \`applyMetafieldChange\` to modify discount settings stored in metafields. ```ts MetafieldUpdateChange | MetafieldRemoveChange ``` ### MetafieldUpdateChange A metafield update or creation operation. Use this to set or modify metafield values that store discount function configuration data. * key The unique key identifying the metafield within its namespace. Use descriptive keys that indicate the setting's purpose (for example, \`'min\_purchase\_amount'\` or \`'eligible\_customer\_tags'\`). ```ts string ``` * namespace The namespace that organizes related metafields. When omitted, a default namespace is assigned. Use consistent namespaces to group related settings. ```ts string ``` * type Identifies this as an update operation. Always set to \`'updateMetafield'\` for updates. ```ts 'updateMetafield' ``` * value The metafield value to store. Can be a string or number depending on your configuration needs. ```ts string | number ``` * valueType The \[data type]\(/docs/apps/build/metafields/list-of-data-types) that defines how the value is formatted and validated. When omitted, preserves the existing type for updates or uses a default for new metafields. Choose a type that matches your value format. ```ts SupportedDefinitionType ``` ### SupportedDefinitionType The supported \[metafield definition types]\(/docs/apps/build/metafields/list-of-data-types) for storing extension configuration data. Use these types to specify how metafield values should be formatted, validated, and displayed. Types prefixed with \`list.\` store arrays of values, while other types store single values. Choose a type that matches your data format (for example, use \`'number\_integer'\` for whole numbers, \`'single\_line\_text\_field'\` for short text, or \`'json'\` for complex structured data). ```ts 'boolean' | 'collection_reference' | 'color' | 'date' | 'date_time' | 'dimension' | 'file_reference' | 'json' | 'metaobject_reference' | 'mixed_reference' | 'money' | 'multi_line_text_field' | 'number_decimal' | 'number_integer' | 'page_reference' | 'product_reference' | 'rating' | 'rich_text_field' | 'single_line_text_field' | 'product_taxonomy_value_reference' | 'url' | 'variant_reference' | 'volume' | 'weight' | 'list.collection_reference' | 'list.color' | 'list.date' | 'list.date_time' | 'list.dimension' | 'list.file_reference' | 'list.metaobject_reference' | 'list.mixed_reference' | 'list.number_decimal' | 'list.number_integer' | 'list.page_reference' | 'list.product_reference' | 'list.rating' | 'list.single_line_text_field' | 'list.url' | 'list.variant_reference' | 'list.volume' | 'list.weight' ``` ### MetafieldRemoveChange A metafield removal operation. Use this to delete metafields that are no longer needed for your discount configuration. * key The unique key of the metafield to remove. Must match the key used when the metafield was created. ```ts string ``` * namespace The namespace containing the metafield to remove. Required to ensure the correct metafield is targeted, as the same key can exist in different namespaces. ```ts string ``` * type Identifies this as a removal operation. Always set to \`'removeMetafield'\` for deletions. ```ts 'removeMetafield' ``` ### MetafieldChangeResult The result returned after attempting to change a metafield. Check the \`type\` property to determine if the operation succeeded (\`'success'\`) or failed (\`'error'\`), then handle the result appropriately in your extension. ```ts MetafieldChangeSuccess | MetafieldChangeResultError ``` ### MetafieldChangeSuccess A successful metafield change operation result. The metafield was updated or removed as requested and the changes are now saved. * type Indicates the operation succeeded. When this value is \`'success'\`, the metafield change was applied successfully. ```ts 'success' ``` ### MetafieldChangeResultError A failed metafield change operation result. Use the error message to understand what went wrong and fix the issue, such as validation errors, permission problems, or invalid metafield types. * message A human-readable error message explaining why the operation failed. Use this to debug issues or display feedback to merchants. ```ts string ``` * type Indicates the operation failed. Check this value to determine if you need to handle an error. ```ts 'error' ``` ### data The `data` object exposed to the extension containing the validation settings. Provides access to the validation object with its identifier and [metafields](https://shopify.dev/docs/apps/build/metafields), plus the [Shopify Function](https://shopify.dev/docs/apps/build/functions) identifier. Use this data to populate your settings UI and understand the current validation configuration in the `admin.settings.validation.render` target. * **shopifyFunction** **ShopifyFunction** **required** The [Shopify Function](https://shopify.dev/docs/apps/build/functions) that implements the validation logic. Use this ID to associate configuration changes with the correct function. * **validation** **Validation** The validation configuration containing the validation ID and metafields. Present when editing an existing validation, absent when creating a new validation. Use the presence of this value to determine if you're in create or edit mode. ### ShopifyFunction A \[Shopify Function]\(/docs/apps/build/functions) that implements cart and checkout validation logic. This identifies which function the settings interface is configuring. * id The \[Shopify Function's]\(/docs/apps/build/functions) unique global identifier (GID). Use this ID to associate settings changes with the correct function. ```ts string ``` ### Validation A validation configuration that exists and is active in the shop. Use this object to access the validation's current settings and metafields when merchants edit an existing validation. * id The validation's unique global identifier (GID). Use this ID to reference the validation in GraphQL operations or when saving updated settings. ```ts string ``` * metafields An array of \[metafields]\(/docs/apps/build/metafields) that store the validation's configuration values. Use these metafields to populate your settings UI with the current validation configuration. ```ts Metafield[] ``` ### Metafield A \[metafield]\(/docs/apps/build/metafields) that stores discount function configuration data. Use metafields to persist settings that control how your discount function behaves, such as discount thresholds, eligibility rules, or custom discount logic parameters. * description A human-readable description explaining the metafield's purpose and how it affects discount behavior. Use this to document your settings for other developers. ```ts string ``` * id The unique global identifier (GID) for this metafield. Use this ID to reference the metafield in GraphQL queries or updates. ```ts string ``` * key The unique key identifying this metafield within its namespace. This key determines how you access the metafield value (for example, \`'min\_purchase\_amount'\` or \`'eligible\_customer\_tags'\`). ```ts string ``` * namespace The namespace that organizes related metafields together. All metafields for a discount should use a consistent namespace to group related settings. ```ts string ``` * type The metafield \[definition type]\(/docs/apps/build/metafields/list-of-data-types) that specifies the value format and validation rules. Use this to determine how to parse and display the value. ```ts string ``` * value The metafield value stored as a string. Parse this value according to the metafield type to use it in your settings UI. ```ts string ``` Examples ### Examples * #### ##### Description Save a minimum quantity validation rule with a \[number field]\(/docs/api/admin-extensions/latest/web-components/forms/number-field) input. This example calls \`applyMetafieldChange\`, checks the result type, and displays success or error banners based on the response. ##### jsx ```tsx import {render} from 'preact'; import {useState} from 'preact/hooks'; export default async () => { render(, document.body); }; function Extension() { const [quantity, setQuantity] = useState('3'); const [result, setResult] = useState(null); const handleSave = async () => { const res = await shopify.applyMetafieldChange({ type: 'updateMetafield', namespace: 'validation', key: 'minimum_quantity', value: quantity, valueType: 'number_integer', }); setResult(res); }; return ( setQuantity(value)} /> Save Validation {result?.type === 'success' && ( Minimum quantity configured )} {result?.type === 'error' && ( {result.message} )} ); } ``` * #### ##### Description Block shipping to specific countries with custom error messages. This example saves blocked countries as a JSON array and a custom error message, then displays the validation and function IDs. ##### jsx ```tsx import {render} from 'preact'; import {useState} from 'preact/hooks'; export default async () => { render(, document.body); }; function Extension() { const {data} = shopify; const [countries, setCountries] = useState('US, CA, GB'); const [errorMsg, setErrorMsg] = useState('Shipping not available to your location'); const handleSave = async () => { const blockedCountries = countries.split(',').map((c) => c.trim()); await shopify.applyMetafieldChange({ type: 'updateMetafield', namespace: 'validation', key: 'blocked_shipping_countries', value: JSON.stringify(blockedCountries), valueType: 'json', }); await shopify.applyMetafieldChange({ type: 'updateMetafield', namespace: 'validation', key: 'error_message', value: errorMsg, valueType: 'single_line_text_field', }); }; return ( setCountries(value)} /> setErrorMsg(value)} /> Save Restrictions Validation ID: {data.validation?.id} Function ID: {data.shopifyFunction.id} ); } ``` * #### ##### Description Detect whether you're editing an existing validation or creating a new one. This example checks for \`data.validation\`, loads existing metafields if present, or initializes default settings for new validations. ##### jsx ```tsx import {render} from 'preact'; import {useState, useEffect} from 'preact/hooks'; export default async () => { render(, document.body); }; function Extension() { const {data} = shopify; const [mode, setMode] = useState('loading'); const [settings, setSettings] = useState({}); useEffect(() => { const initializeSettings = async () => { if (data.validation) { const config = data.validation.metafields.reduce((acc, field) => { acc[field.key] = field.value; return acc; }, {}); setSettings(config); setMode('edit'); } else { await shopify.applyMetafieldChange({ type: 'updateMetafield', namespace: 'validation', key: 'default_rule', value: 'require_minimum_cart_total', valueType: 'single_line_text_field', }); setMode('created'); } }; initializeSettings(); }, [data]); return ( {mode === 'loading' && } {mode === 'edit' && ( <> Editing existing validation {Object.entries(settings).map(([key, value]) => ( {key}: {value} ))} )} {mode === 'created' && Created new validation configuration} ); } ``` *** ## Best practices * **Check operation result type:** `applyMetafieldChange` returns `{ type: 'success' }` or `{ type: 'error', message: string }`. Errors don't throw exceptions, so always check the returned `type` property. *** ## Limitations * Metafields have [size limits](https://shopify.dev/docs/apps/build/metafields/metafield-limits). Individual values can't exceed 256KB, and total metafield storage per validation is limited. * The `applyMetafieldChange` method is sequential. Operations process one at a time. Rapid successive calls can cause race conditions where later updates overwrite earlier ones. * Metafield changes apply immediately. Unlike admin forms, metafield changes persist right away without waiting for merchants to save. * Your extension can't modify the Function ID. The `shopifyFunctionId` is read-only and determined when the validation rule is created. ***