String
A schema type for strings and a selectable lists of strings.
Properties
REQUIREDtypestring
Required. Value must be set to
string
.
Options
listarray
A list of predefined values that the user can choose from. The array can either include string values
['sci-fi', 'western']
or objects[{title: 'Sci-Fi', value: 'sci-fi'}, ...]
layoutstring
Controls how the items defined in the
list
option are presented. If set to'radio'
the list will render radio buttons. If set to'dropdown'
you'll get a dropdown menu instead. Default isdropdown
.directionstring
Controls how radio buttons are lined up. Use
direction: 'horizontal|vertical'
to render radio buttons in a row or a column. Default isvertical
. Will only take effect if thelayout
option is set toradio
.
Validation
Learn more about validationrequired()function
Ensures that this field exists.
min(minLength)function
Minimum length of string.
max(maxLength)function
Maximum length of string.
length(exactLength)function
Exact length of string.
uppercase()function
All characters must be uppercase.
lowercase()function
All characters must be lowercase.
regex(pattern[, options])function
String must match the given pattern.
options
is an optional object, currently you can setoptions.name
andoptions.invert
.Providing a
name
will make the message more understandable to the user ("Does not match the <name>-pattern").Set
invert
totrue
in order to allow any value that does NOT match the pattern.custom(fn)function
Creates a custom validation rule.
{
title: 'Title',
name: 'title',
type: 'string',
description: 'Make it catchy',
validation: Rule => Rule.max(120).warning(`A title shouldn't be more than 120 characters.`)
}
Input
{
title: 'Genre',
name: 'genre',
type: 'string',
options: {
list: [
{title: 'Sci-Fi', value: 'sci-fi'},
{title: 'Western', value: 'western'}
], // <-- predefined values
layout: 'radio' // <-- defaults to 'dropdown'
}
}
Output
{
"_type": "movie",
"_id": "23407q-qwerqyt12",
"genre": "sci-fi",
...
}
Protip
Want to make a multi-select for strings? Check out the Array schema type to see how you can build an array of strings, references, objects, and more.
You can use initial values to preset string fields on document creation:
export default {
name: 'post',
type: 'document',
title: 'Post',
initialValue: {
title: 'The initial title'
},
fields: [
{
name: 'title',
type: 'string',
title: 'Title'
}
]
}