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:
fields:
- name: title
type: string
required: true
- name: tags
type: string
list: trueField types
Keys for every field
A field with fields and no type is an 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:
fields:
- name: title
type: string
- name: body
type: rich-textLists
list: true turns any field into a list editors can add to, remove from and reorder:
- name: tags
type: string
list: trueA saved value in YAML or Markdown front matter might look like:
tags: [news, launch]Use an object to set limits and collapse long lists:
- name: faq
type: object
list:
min: 1
max: 10
collapsible:
collapsed: true
summary: '{index}. {question}'
fields:
- name: question
type: string
- name: answer
type: rich-textAn 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:
requiredlist.minandlist.max- type options such as
minlength,maxlength,minandmax - 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:
- name: slug
type: string
pattern: '^[a-z0-9-]+$'Add a message to explain the rule:
- name: sku
type: string
pattern:
regex: '^[A-Z]{3}-\d{4}$'
message: Use the format ABC-1234Patterns 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:
- 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.