# Create a metafield definition validation

> Add a validation rule to a metafield definition.

`POST /rest/api/metafield_definition_validations`

Creates a MetafieldDefinitionValidation resource.

Adds a validation rule to a metafield definition. Reference the `metafieldDefinition`, the validation `name` (type) and its `value`. The rule must be compatible with the definition’s type, otherwise it is rejected with `422`.

## Headers

| Name                         | Type     | Required | Description                                                                                                                                      |
| ---------------------------- | -------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `X-Flowkiwi-Organization-Id` | `string` | Yes      | The organization the request acts as. Determines the identity, permissions and ownership applied when reading or modifying data on the instance. |

## Request body

| Property              | Type     | Required | Description                                                            |
| --------------------- | -------- | -------- | ---------------------------------------------------------------------- |
| `metafieldDefinition` | `string` | Yes      | The metafield definition this validation belongs to.                   |
| `name`                | `string` | Yes      | The type of validation rule.                                           |
| `value`               | `string` | Yes      | The validation parameter value. Format depends on the validation type. |

## Response

**201** - MetafieldDefinitionValidation resource created

| Property              | Type     | Required | Description                                                            |
| --------------------- | -------- | -------- | ---------------------------------------------------------------------- |
| `@context`            | `object` | -        |                                                                        |
| `@id`                 | `string` | Yes      |                                                                        |
| `@type`               | `string` | Yes      |                                                                        |
| `id`                  | `string` | -        | The resource's unique identifier (UUID).                               |
| `metafieldDefinition` | `string` | Yes      | The metafield definition this validation belongs to                    |
| `name`                | `string` | Yes      | The type of validation rule                                            |
| `value`               | `string` | Yes      | The validation parameter value. Format depends on the validation type. |

## Errors

**400** - Invalid input

| Property   | Type             | Required | Description                                                                                                                          |
| ---------- | ---------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `@context` | `object`         | -        |                                                                                                                                      |
| `@id`      | `string`         | Yes      |                                                                                                                                      |
| `@type`    | `string`         | Yes      |                                                                                                                                      |
| `title`    | `string \| null` | -        | A short, human-readable summary of the problem.                                                                                      |
| `detail`   | `string \| null` | -        | A human-readable explanation specific to this occurrence of the problem.                                                             |
| `status`   | `number \| null` | -        |                                                                                                                                      |
| `instance` | `string \| null` | -        | A URI reference that identifies the specific occurrence of the problem. It may or may not yield further information if dereferenced. |
| `type`     | `string`         | -        | A URI reference that identifies the problem type                                                                                     |

**403** - Access denied. The caller is missing one or more identity permissions required for this operation.

| Property             | Type       | Required | Description                                                                                                                                |
| -------------------- | ---------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `@context`           | `object`   | -        |                                                                                                                                            |
| `@id`                | `string`   | Yes      |                                                                                                                                            |
| `@type`              | `string`   | Yes      |                                                                                                                                            |
| `type`               | `string`   | -        |                                                                                                                                            |
| `title`              | `string`   | -        |                                                                                                                                            |
| `status`             | `integer`  | -        |                                                                                                                                            |
| `detail`             | `string`   | -        |                                                                                                                                            |
| `missingPermissions` | `string[]` | -        | Identity permissions that the caller is missing for this operation. Present only when the 403 is caused by a denied identity:* permission. |

**422** - An error occurred

| Property     | Type             | Required | Description |
| ------------ | ---------------- | -------- | ----------- |
| `@context`   | `object`         | -        |             |
| `@id`        | `string`         | Yes      |             |
| `@type`      | `string`         | Yes      |             |
| `status`     | `integer`        | -        |             |
| `violations` | `object[]`       | -        |             |
| `detail`     | `string`         | -        |             |
| `type`       | `string`         | -        |             |
| `title`      | `string \| null` | -        |             |
| `instance`   | `string \| null` | -        |             |

## Examples

### Example request

```bash
curl -X POST 'https://<tenant>.product-management.flowkiwi.net/rest/api/metafield_definition_validations' \
  -H 'Authorization: Bearer {token}' \
  -H 'X-Flowkiwi-Organization-Id: <org_id>' \
  -H 'Accept: application/ld+json' \
  -H 'Content-Type: application/ld+json' \
  -d '{
  "metafieldDefinition": "/rest/api/metafield_definitions/019afb01-1a2b-7c3d-8e4f-0a1b2c3d4e5f",
  "name": "/rest/api/metafield_definition_validation_types/max_length",
  "value": "500"
}'
```

### Example response

```json
{
  "@context": "/contexts/MetafieldDefinitionValidation",
  "@id": "/rest/api/metafield_definition_validations/019afe01-1a2b-7c3d-8e4f-0a1b2c3d4e5f",
  "@type": "MetafieldDefinitionValidation",
  "id": "019afe01-1a2b-7c3d-8e4f-0a1b2c3d4e5f",
  "metafieldDefinition": "/rest/api/metafield_definitions/019afb01-1a2b-7c3d-8e4f-0a1b2c3d4e5f",
  "name": "/rest/api/metafield_definition_validation_types/max_length",
  "value": "500"
}
```
