Boolean
Defines a boolean attribute:
import { boolean } from 'dynamodb-toolbox/attributes/boolean';
const pokemonSchema = schema({
...
isLegendary: boolean(),
});
type FormattedPokemon = FormattedItem<typeof PokemonEntity>;
// => {
// ...
// isLegendary: boolean
// }
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 isLegendarySchema = boolean()
const isLegendarySchema = boolean().required()
const isLegendarySchema = boolean({
required: 'atLeastOnce'
})
// shorthand for `.required('never')`
const isLegendarySchema = boolean().optional()
const isLegendarySchema = boolean({ required: 'never' })
.hidden()
boolean | undefined
Skips the attribute when formatting items:
const isLegendarySchema = boolean().hidden()
const isLegendarySchema = boolean({ 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 isLegendarySchema = boolean().key()
const isLegendarySchema = boolean({
key: true,
required: 'always'
})
.savedAs(...)
string
Renames the attribute during the transformation step (at root level or within Maps):
const isLegendarySchema = boolean().savedAs('isLeg')
const isLegendarySchema = boolean({ savedAs: 'isLeg' })
.enum(...)
boolean[]
Provides a finite range of possible values:
const isLegendarySchema = boolean().enum(true, false)
// 👇 Equivalent to `.enum(false).default(false)`
const isLegendarySchema = boolean().const(false)
For type inference reasons, the enum
option is only available as a method and not as a constructor property.
Although it is not very useful, boolean
is a primitive, and as such inherits from the .enum
and .const
options.
.transform(...)
Transformer<boolean>
Allows modifying the attribute values during the transformation step:
const negate = {
parse: (input: boolean) => !input,
format: (saved: boolean) => !saved
}
// Saves the negated value
const isLegendarySchema = boolean().transform(negate)
const isLegendarySchema = boolean({ transform: negate })
DynamoDB-Toolbox exposes on-the-shelf transformers, so feel free to use them!
.default(...)
ValueOrGetter<boolean>
Specifies default values for the attribute. See Defaults and Links for more details:
- Put
- Key
- Update
const isLegendarySchema = boolean().default(false)
// 👇 Similar to
const isLegendarySchema = boolean().putDefault(false)
// 👇 ...or
const isLegendarySchema = boolean({
defaults: {
key: undefined,
put: false,
update: undefined
}
})
// 🙌 Getters also work!
const isLegendarySchema = boolean().default(() => false)
const isLegendarySchema = boolean().key().default(false)
// 👇 Similar to
const isLegendarySchema = boolean().key().keyDefault(false)
// 👇 ...or
const isLegendarySchema = boolean({
defaults: {
key: false,
// put & update defaults are not useful in `key` attributes
put: undefined,
update: undefined
},
key: true,
required: 'always'
})
const isUpdatedSchema = boolean().updateDefault(true)
// 👇 Similar to
const isUpdatedSchema = boolean({
defaults: {
key: undefined,
put: undefined,
update: true
}
})
.link<Schema>(...)
Link<SCHEMA, boolean>
Similar to .default(...)
but allows deriving the default value from other attributes. See Defaults and Links for more details:
const pokemonSchema = schema({
customName: string().optional()
}).and(prevSchema => ({
hasCustomName: boolean().link<typeof prevSchema>(
// 🙌 Correctly typed!
({ customName }) => customName !== undefined
)
}))
.validate(...)
Validator<boolean>
Adds custom validation to the attribute. See Custom Validation for more details:
const trueOrUndefinedSchema = boolean()
.optional()
.validate(input => input !== false)
// 👇 Similar to
const trueOrUndefinedSchema = boolean()
.optional()
.putValidate(input => input !== false)
// 👇 ...or
const trueOrUndefinedSchema = boolean({
validators: {
key: undefined,
put: input => input !== false,
update: undefined
},
required: 'never'
})