# Fields overview

Fields turn the keys in your files into form inputs. Each field has a `name`, the key in the file, and a `type`:

```yaml
fields:
  - name: title
    type: string
    required: true
  - name: tags
    type: string
    list: true
```

## Field types

| Type                                            | For                                            | Stored as                    |
| ----------------------------------------------- | ---------------------------------------------- | ---------------------------- |
| [`string`](/configuration/fields/string/)       | Short text: titles, names, slugs, links        | String                       |
| [`text`](/configuration/fields/text/)           | Plain multi-line text: summaries, notes        | String                       |
| [`rich-text`](/configuration/fields/rich-text/) | Formatted content                              | Markdown or HTML string      |
| [`code`](/configuration/fields/code/)           | Code and snippets, with syntax highlighting    | String                       |
| [`number`](/configuration/fields/number/)       | Prices, weights, ordering                      | Number                       |
| [`boolean`](/configuration/fields/boolean/)     | On/off flags                                   | `true` or `false`            |
| [`date`](/configuration/fields/date/)           | Dates and times                                | Formatted string             |
| [`select`](/configuration/fields/select/)       | A choice from a fixed list                     | String, or a list of strings |
| [`reference`](/configuration/fields/reference/) | A link to an entry in another collection       | String, or a list of strings |
| [`image`](/configuration/fields/image/)         | Images from a media library                    | Path, or a list of paths     |
| [`file`](/configuration/fields/file/)           | Documents and other files                      | Path, or a list of paths     |
| [`object`](/configuration/fields/object/)       | A group of nested fields                       | Object                       |
| [`block`](/configuration/fields/block/)         | One of several field groups, for page builders | Object with a type key       |
| [`uuid`](/configuration/fields/uuid/)           | A stable unique ID                             | String                       |

## Keys for every field

| Key           | Description                                                                           |
| ------------- | ------------------------------------------------------------------------------------- |
| `name`        | Required. The key in the file. Letters, numbers, `-` and `_`.                         |
| `type`        | Required, unless `component` is set. One of the types above.                          |
| `component`   | Use a [component](/configuration/components/) instead of `type`.                      |
| `label`       | Label shown in the form. Defaults to `name`. `false` hides the label.                 |
| `description` | Help text shown under the label.                                                      |
| `required`    | `true` to require a value before saving.                                              |
| `default`     | Value for new entries.                                                                |
| `readonly`    | `true` to show the value without allowing edits. Applies to everything nested inside. |
| `hidden`      | `true` to hide the field from the form. Its value is still saved.                     |
| `list`        | Repeat the field. See [lists](#lists).                                                |
| `pattern`     | A regular expression a text value must match. See [validation](#validation).          |
| `localized`   | Store one value per language. See [localization](/configuration/localization/).       |
| `options`     | Settings for the type. See each type's page.                                          |

A field with `fields` and no `type` is an [object](/configuration/fields/object/).

SP CMS warns about unknown keys on a field, but not about most keys inside `options`. A misspelt option is silently ignored, so check option names against each type's page.

## The `body` field

In Markdown and other front matter files, a field named `body` holds the content below the front matter, not a front matter key. Any field type works, but `rich-text` is the usual choice:

```yaml
fields:
  - name: title
    type: string
  - name: body
    type: rich-text
```

## Lists

`list: true` turns any field into a list editors can add to, remove from and reorder:

```yaml
- name: tags
  type: string
  list: true
```

A saved value in YAML or Markdown front matter might look like:

```yaml
tags: [news, launch]
```

Use an object to set limits and collapse long lists:

```yaml
- name: faq
  type: object
  list:
    min: 1
    max: 10
    collapsible:
      collapsed: true
      summary: '{index}. {question}'
  fields:
    - name: question
      type: string
    - name: answer
      type: rich-text
```

| `list` key              | Description                                                                                                               |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| `min`, `max`            | Minimum and maximum number of items.                                                                                      |
| `collapsible`           | For `object` and `block` lists, whose items can collapse by default: `false` to turn it off, or `{ collapsed, summary }`. |
| `collapsible.collapsed` | `true` to start with items collapsed.                                                                                     |
| `collapsible.summary`   | Text shown on a collapsed item. Use `{index}` for its position (from 1) and `{field}` or `{fields.field}` for its values. |

An empty list item can't be saved: editors fill it in or remove it.

## Validation

SP CMS checks every rule before saving, both in the form and again on the server. Content with fields can only be saved through its form, never as raw source, so these rules always apply:

- `required`
- `list.min` and `list.max`
- type options such as `minlength`, `maxlength`, `min` and `max`
- allowed file types and media folders for image and file fields
- `pattern`

Add `pattern` to check a text value, usually on `string` and `text` fields:

```yaml
- name: slug
  type: string
  pattern: '^[a-z0-9-]+

Add a message to explain the rule:

```yaml
- name: sku
  type: string
  pattern:
    regex: '^[A-Z]{3}-\d{4}

Patterns use JavaScript regular expression syntax, without flags. They match anywhere in the value, so anchor them with `^` and `$` when the whole value must fit.

### Links

For any field that becomes a link, accept only `https` addresses, so a saved value can never put a `javascript:` link on the page:

```yaml
- name: link
  type: string
  pattern:
    regex: '^https://'
    message: A full address, starting https://
```

Make the site's schema accept only `https` too. In Astro, use `z.url({ protocol: /^https$/ })`. The site's build is the final check on content, so it catches anything saved outside SP CMS.

```

Add a message to explain the rule:
 RSD_FENCE_6 

Patterns use JavaScript regular expression syntax, without flags. They match anywhere in the value, so anchor them with `^` and `$` when the whole value must fit.

### Links

For any field that becomes a link, accept only `https` addresses, so a saved value can never put a `javascript:` link on the page:

```yaml
- name: link
  type: string
  pattern:
    regex: '^https://'
    message: A full address, starting https://
```

Make the site's schema accept only `https` too. In Astro, use `z.url({ protocol: /^https$/ })`. The site's build is the final check on content, so it catches anything saved outside SP CMS.

    message: Use the format ABC-1234
```

Patterns use JavaScript regular expression syntax, without flags. They match anywhere in the value, so anchor them with `^` and `$` when the whole value must fit.

### Links

For any field that becomes a link, accept only `https` addresses, so a saved value can never put a `javascript:` link on the page:

```yaml
- name: link
  type: string
  pattern:
    regex: '^https://'
    message: A full address, starting https://
```

Make the site's schema accept only `https` too. In Astro, use `z.url({ protocol: /^https$/ })`. The site's build is the final check on content, so it catches anything saved outside SP CMS.

```

Add a message to explain the rule:

```yaml
- name: sku
  type: string
  pattern:
    regex: '^[A-Z]{3}-\d{4}

Patterns use JavaScript regular expression syntax, without flags. They match anywhere in the value, so anchor them with `^` and `$` when the whole value must fit.

### Links

For any field that becomes a link, accept only `https` addresses, so a saved value can never put a `javascript:` link on the page:

```yaml
- name: link
  type: string
  pattern:
    regex: '^https://'
    message: A full address, starting https://
```

Make the site's schema accept only `https` too. In Astro, use `z.url({ protocol: /^https$/ })`. The site's build is the final check on content, so it catches anything saved outside SP CMS.

    message: Use the format ABC-1234
```

Patterns use JavaScript regular expression syntax, without flags. They match anywhere in the value, so anchor them with `^` and `$` when the whole value must fit.

### Links

For any field that becomes a link, accept only `https` addresses, so a saved value can never put a `javascript:` link on the page:

```yaml
- name: link
  type: string
  pattern:
    regex: '^https://'
    message: A full address, starting https://
```

Make the site's schema accept only `https` too. In Astro, use `z.url({ protocol: /^https$/ })`. The site's build is the final check on content, so it catches anything saved outside SP CMS.
