Skip to main content

Formatter

Given a saved item, validates that it respects the schema and formats it:

import { Formatter } from 'dynamodb-toolbox/schema/actions/format'

const formattedPikachu = pokemonSchema
.build(Formatter)
.format(savedPikachu)

Note that:

  • Additional and hidden fields are omitted, but inputs are not mutated
  • The formatting throw an error if the saved item is invalid
  • Transformations (i.e. savedAs and transforms) are applied in reverse

Methods

format(...)

(savedValue: unknown, options?: FormatValueOptions) => FormattedValue<SCHEMA>

Formats a saved item:

const formattedValue = pokemonSchema.build(Formatter).format(savedValue)

You can provide formatting options as a second argument. Available options:

OptionTypeDefaultDescription
partialbooleanfalseAllow every attribute (flat or deep) to be optional while formatting.
attributesPath<Schema>[]-To specify a list of attributes to format (other attributes are omitted).

See the PathParser action for more details on how to write attribute paths.
Examples
const saved = {
pokemonId: 'pikachu1',
name: 'Pikachu'
}

// 🙌 Typed as `DeepPartial<Pokemon>`
const formatted = pokemonSchema
.build(Formatter)
.format(saved, { partial: true })

You can use the FormattedValue type to explicitly type an object as a formatting output object:

import type { FormattedValue } from 'dynamodb-toolbox/format'

const formattedValue: FormattedValue<
typeof pokemonSchema,
// 👇 Optional options
{ partial: false; attributes: 'name' | 'level' }
// ❌ Throws a type error
> = { invalid: 'output' }