String
Describes string values:
import { string } from 'dynamodb-toolbox/schema/string'
const nameSchema = string()
type Name = FormattedValue<typeof nameSchema>
// => string
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 nameSchema = string()
const nameSchema = string().required()
const nameSchema = string({ required: 'atLeastOnce' })
// shorthand for `.required('never')`
const nameSchema = string().optional()
const nameSchema = string({ required: 'never' })
.hidden()
boolean | undefined
Omits schema values during formatting:
const nameSchema = string().hidden()
const nameSchema = string({ 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 nameSchema = string().key()
const nameSchema = string({
key: true,
required: 'always'
})
.savedAs(...)
string
Renames schema values during the transformation step (within items
or maps
):
const nameSchema = string().savedAs('n')
const nameSchema = string({ savedAs: 'n' })
.enum(...)
string[]
Provides a finite range of possible values:
const pokeTypeSchema = string().enum('fire', 'water', ...)
// 👇 Equivalent to `.enum('fire').default('fire')`
const pokeTypeSchema = string().const('fire')
For type inference reasons, the enum
option is only available as a method and not as input props.
.transform(...)
Transformer<string>
Allows modifying schema values during the transformation step:
const PREFIX = 'PREFIX#'
const prefix = {
parse: (input: string) => [PREFIX, input].join(''),
format: (saved: string) => saved.slice(PREFIX.length)
}
// Prefixes the value
const prefixedStrSchema = string().transform(prefix)
const prefixedStrSchema = string({ transform: prefix })
DynamoDB-Toolbox exposes on-the-shelf transformers (including prefix
), so feel free to use them!
.default(...)
ValueOrGetter<string>
Specifies default values. See Defaults and Links for more details:
- Put
- Key
- Update
const nameSchema = string().default('Pikachu')
// 👇 Similar to
const nameSchema = string().putDefault('Pikachu')
// 👇 ...or
const nameSchema = string({ putDefault: 'Pikachu' })
// 🙌 Getters also work!
const nameSchema = string().default(() => 'Pikachu')
const nameSchema = string().key().default('Pikachu')
// 👇 Similar to
const nameSchema = string().key().keyDefault('Pikachu')
// 👇 ...or
const nameSchema = string({
key: true,
required: 'always',
keyDefault: 'Pikachu'
})
// 👇 Records the date at each update
const lastUpdatedSchema = string().updateDefault(() =>
new Date().toISOString()
)
// 👇 Similar to
const lastUpdatedSchema = string({
updateDefault: () => new Date().toISOString()
})
.link<Schema>(...)
Link<SCHEMA, string>
Similar to .default(...)
but allows deriving the default value from other attributes. See Defaults and Links for more details:
const pokemonSchema = item({
level: string()
}).and(prevSchema => ({
captureLevel: string().link<typeof prevSchema>(
// 🙌 Correctly typed!
item => item.level
)
}))
.validate(...)
Validator<string>
Adds custom validation. See Custom Validation for more details:
const longStrSchema = string().validate(
input => input.length > 3
)
// 👇 Similar to
const longStrSchema = string().putValidate(
input => input.length > 3
)
// 👇 ...or
const longStrSchema = string({
putValidator: input => input.length > 3
})