List
Describes list values, containing elements of any type:
import { list } from 'dynamodb-toolbox/schema/list';
import { string } from 'dynamodb-toolbox/schema/string';
const pokeTypeSchema = string().enum('fire', ...)
const pokemonTypesSchema = list(pokeTypeSchema)
type PokemonType = FormattedValue<typeof pokemonTypesSchema>;
// => ('fire' | ...)[]
List elements must respect some constraints:
- They cannot be
optional
or always required - They cannot be
hidden
orkey
(tagging thelist
itself askey
is enough) - They cannot have
default
orlinks
// ❌ Raises a type AND a run-time error
const strList = list(string().optional())
const strList = list(string().hidden())
const strList = list(string().key())
const strList = list(string().default('foo'))
Properties
.required()
string | undefined
Tags schema values as required (within items
or maps
). Possible values are:
'atLeastOnce' (default)
: Required (starting value)'always'
: Always required (including updates)'never'
: Optional
// Equivalent
const pokeTypesSchema = list(pokeTypeSchema)
const pokeTypesSchema = list(pokeTypeSchema).required()
const pokeTypesSchema = list(
pokeTypeSchema,
// Options can be provided as 2nd argument
{ required: 'atLeastOnce' }
)
// shorthand for `.required('never')`
const pokeTypesSchema = list(pokeTypeSchema).optional()
const pokeTypesSchema = list(..., { required: 'never' })
.hidden()
boolean | undefined
Omits schema values during formatting:
const pokeTypesSchema = list(pokeTypeSchema).hidden()
const pokeTypesSchema = list(..., { hidden: true })
.key()
boolean | undefined
Tags schema values as a primary key attribute or linked to a primary key attribute:
// Note: The method also sets the `required` property to 'always'
// (it is often the case in practice, you can still use `.optional()` if needed)
const pokeTypesSchema = list(pokeTypeSchema).key()
const pokeTypesSchema = list(..., {
key: true,
required: 'always'
})
.savedAs(...)
string
Renames schema values during the transformation step (within items
or maps
):
const pokeTypesSchema = list(pokeTypeSchema).savedAs('pt')
const pokeTypesSchema = list(..., { savedAs: 'pt' })
.default(...)
ValueOrGetter<ELEMENTS[]>
Specifies default values. See Defaults and Links for more details:
- Put/Update
- Key
const now = () => new Date().toISOString()
const timestampsSchema = list(string())
.default(() => [now()])
.updateDefault(() => $append(now()))
// 👇 Similar to
const timestampsSchema = list(...)
.putDefault(() => [now()])
.updateDefault(() => $append(now()))
// 👇 ...or
const timestampsSchema = list(..., {
putDefault: () => [now()],
})
const defaultSpecifiers = ['POKEMON']
const specifiersSchema = list(string())
.key()
.default(defaultSpecifiers)
// 👇 Similar to
const specifiersSchema = list(...)
.key()
.keyDefault(defaultSpecifiers)
// 👇 ...or
const specifiersSchema = list(..., {
key: true,
required: 'always',
keyDefault: defaultSpecifiers,
})
☝️ On key attributes, .default(...)
should be applied after .key()
.
.link<Schema>(...)
Link<SCHEMA, ELEMENTS[]>
Similar to .default(...)
but allows deriving the default value from other attributes. See Defaults and Links for more details:
const pokemonSchema = item({
pokeTypeSet: set(pokeTypeSchema)
}).and(prevSchema => ({
pokeTypeList: set(pokeTypeSchema).link<typeof prevSchema>(
// 🙌 Correctly typed!
({ pokeTypeSet }) => [...pokeTypeSet.values()]
)
}))
☝️ On key attributes, .link(...)
should be applied after .key()
.
.validate(...)
Validator<ELEMENTS[]>
Adds custom validation. See Custom Validation for more details:
const nonEmptyListSchema = list(string()).validate(
input => input.length > 0
)
// 👇 Similar to
const nonEmptyListSchema = list(string()).putValidate(
input => input.length > 0
)
// 👇 ...or
const nonEmptyListSchema = list(string(), {
putValidator: input => input.length > 0
})