Skip to main content
Version: v2

SchemaDTO

Builds a Data Transfer Object of the schema.

A DTO is a JSON-stringifiable object representing the schema that can be transferred or saved for later use:

import { SchemaDTO } from 'dynamodb-toolbox/schema/actions/dto'

const pokemonSchemaDTO = pokemonSchema.build(SchemaDTO)

const pokemonSchemaJSON = JSON.stringify(pokemonSchemaDTO)

On DTO retrieval, you can use the fromDTO util to re-create the original schema:

import { fromDTO } from 'dynamodb-toolbox/schema/actions/fromDTO'

const pokemonSchemaDTO = JSON.parse(pokemonSchemaJSON)

// ๐Ÿ‘‡ Has a similar configuration to the original
const pokemonSchema = fromDTO(pokemonSchemaDTO)

The whole meta property (including JSON-safe custom keys) is serialized and rebuilt:

const levelSchema = number().meta({
title: 'Level',
description: 'The Pokรฉmon level',
deprecated: true
})

const rebuilt = fromDTO(levelSchema.build(SchemaDTO))

rebuilt.props.meta
// => {
// title: 'Level',
// description: 'The Pokรฉmon level',
// deprecated: true
// }
note

All TS types are lost in the process.

caution

Note that functions are not serializable, so parts of the schema may be lost in the process:

  • Getters defaults are not serialized.
  • Links are not serialized.
  • Validators are not serialized.
  • On-the-shelf transformers like prefix and jsonStringify are correctly serialized, but custom transformers are not.