Skip to main content

Any

Defines an attribute containing any value. No validation is applied at run-time, and its type is resolved as unknown by default:

import { any } from 'dynamodb-toolbox/attributes/any';

const pokemonSchema = schema({
...
metadata: any(),
});

type FormattedPokemon = FormattedItem<typeof PokemonEntity>;
// => {
// ...
// metadata: unknown
// }

Options

.required()

string | undefined

Tags the attribute as required (at root level or within Maps). Possible values are:

  • 'atLeastOnce' (default): Required (starting value)
  • 'always': Always required (including updates)
  • 'never': Optional
// Equivalent
const metadataSchema = any()
const metadataSchema = any().required()
const metadataSchema = any({ required: 'atLeastOnce' })

// shorthand for `.required('never')`
const metadataSchema = any().optional()
const metadataSchema = any({ required: 'never' })

.hidden()

boolean | undefined

Skips the attribute when formatting items:

const metadataSchema = any().hidden()
const metadataSchema = any({ hidden: true })

.key()

boolean | undefined

Tags the attribute as needed to compute the primary key:

// 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 metadataSchema = any().key()
const metadataSchema = any({
key: true,
required: 'always'
})

.savedAs(...)

string

Renames the attribute during the transformation step (at root level or within Maps):

const metadataSchema = any().savedAs('meta')
const metadataSchema = any({ savedAs: 'meta' })

.default(...)

ValueOrGetter<unknown>

Specifies default values for the attribute. See Defaults and Links for more details:

Examples
const metadataSchema = any().default({ any: 'value' })
// 👇 Similar to
const metadataSchema = any().putDefault({ any: 'value' })
// 👇 ...or
const metadataSchema = any({
defaults: {
key: undefined,
put: { any: 'value' },
update: undefined
}
})

// 🙌 Getters also work!
const metadataSchema = any().default(() => ({
any: 'value'
}))

.link<Schema>(...)

Link<SCHEMA, unknown>

Similar to .default(...) but allows deriving the default value from other attributes. See Defaults and Links for more details:

const pokemonSchema = schema({
pokeTypes: string()
}).and(prevSchema => ({
metadata: any().link<typeof prevSchema>(
// 🙌 Correctly typed!
item => item.pokeTypes.join('#')
)
}))

.validate(...)

Validator<unknown>

Adds custom validation to the attribute. See Custom Validation for more details:

Examples
const metadataSchema = any().validate(
input => typeof input === 'object'
)
// 👇 Similar to
const metadataSchema = any().putValidate(
input => typeof input === 'object'
)
// 👇 ...or
const metadataSchema = any({
validators: {
key: undefined,
put: input => typeof input === 'object',
update: undefined
}
})

.castAs<TYPE>()

(TypeScript only)

Overrides the resolved type of the attribute:

const metadataSchema = any().castAs<{ foo: 'bar' }>()