Skip to main content
Version: v2

Any

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

import { any } from 'dynamodb-toolbox/schema/any'

const metadataSchema = any()

type Metadata = FormattedValue<typeof metadataSchema>
// => unknown

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

Omits schema values during formatting:

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

.savedAs(...)​

string

Renames schema values during the transformation step (within items or maps):

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

.castAs<TYPE>()​

(TypeScript only)

Overrides the resolved type of valid values:

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

.transform(...)​

Transformer<unknown>

Allows modifying schema values during the transformation step:

const jsonStringify = {
encode: JSON.stringify,
decode: JSON.parse
}

// JSON stringifies the value
const stringifiedSchema = any().transform(jsonStringify)
const stringifiedSchema = any({ transform: jsonStringify })

DynamoDB-Toolbox exposes on-the-shelf transformers (including jsonStringify), so feel free to use them!

.default(...)​

ValueOrGetter<unknown>

Specifies default values. 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({ putDefault: { any: 'value' } })

// πŸ™Œ 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 = item({
pokeTypes: string()
}).and(prevSchema => ({
metadata: any().link<typeof prevSchema>(
// πŸ™Œ Correctly typed!
item => item.pokeTypes.join('#')
)
}))

.validate(...)​

Validator<unknown>

Adds custom validation. 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({
putValidator: input => typeof input === 'object'
})