Boolean
Describes boolean values:
import { boolean } from 'dynamodb-toolbox/schema/boolean'
const isLegendarySchema = boolean()
type IsLegendary = FormattedValue<typeof isLegendarySchema>
// => boolean
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 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
Omits schema values during formatting:
const isLegendarySchema = boolean().hidden()
const isLegendarySchema = boolean({ 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 isLegendarySchema = boolean().key()
const isLegendarySchema = boolean({
key: true,
required: 'always'
})
.savedAs(...)
string
Renames schema values during the transformation step (within items
or 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 input props.
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 schema 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. 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({ putDefault: false })
// 🙌 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({
key: true,
required: 'always',
keyDefault: false
})
const isUpdatedSchema = boolean().updateDefault(true)
// 👇 Similar to
const isUpdatedSchema = boolean({ updateDefault: 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 = item({
customName: string().optional()
}).and(prevSchema => ({
hasCustomName: boolean().link<typeof prevSchema>(
// 🙌 Correctly typed!
({ customName }) => customName !== undefined
)
}))
.validate(...)
Validator<boolean>
Adds custom validation. 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({
required: 'never',
putValidator: input => input !== false
})