Skip to main content
Version: v2

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')
info

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:

Examples
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')

.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:

Examples
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
})