Skip to main content

Null

Defines a null attribute:

// `null` is a reserved keyword 🤷‍♂️
import { nul } from 'dynamodb-toolbox/attributes/nul';

const pokemonSchema = schema({
...
nullValue: nul(),
});

type FormattedPokemon = FormattedItem<typeof PokemonEntity>;
// => {
// ...
// nullValue: null
// }
info

Not very useful on itself, nul is more likely to be used in conjunction with anyOf to define nullable attributes:

const pokemonSchema = schema({
...
nullableString: anyOf(string(), nul()),
});

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 nullishSchema = nul()
const nullishSchema = nul().required()
const nullishSchema = nul({ required: 'atLeastOnce' })

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

.hidden()

boolean | undefined

Skips the attribute when formatting items:

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

.savedAs(...)

string

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

const nullishSchema = nul().savedAs('_n')
const nullishSchema = nul({ savedAs: '_n' })

.default(...)

ValueOrGetter<null>

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

Examples
const nullishSchema = nul().default(null)
// 👇 Similar to
const nullishSchema = nul().putDefault(null)
// 👇 ...or
const nullishSchema = nul({
defaults: {
key: undefined,
put: null,
update: undefined
}
})

.link<Schema>(...)

Link<SCHEMA, null>

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

const pokemonSchema = schema({
boolean: boolean()
}).and(prevSchema => ({
nullIfTrue: nul()
.optional()
.link<typeof prevSchema>(
// 🙌 Correctly typed!
({ boolean }) => (boolean ? null : undefined)
)
}))

.validate(...)

Validator<null>

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