JSONStringify
Applies JSON.stringify
to any value:
import { jsonStringify } from 'dynamodb-toolbox/transformers/jsonStringify'
const jsonStringifier = jsonStringify()
const stringifiedSchema = any().transform(jsonStringifier)
prefixer.encode({ foo: 'bar' }) // => '{"foo":"bar"}'
prefixer.decode('{"foo":"bar"}') // => { foo: 'bar' }
If needed, you can provide custom space
, replacer
and reviver
options:
import { jsonStringify } from 'dynamodb-toolbox/transformers/jsonStringify'
const priceJSONStringifier = jsonStringify({
space: 2,
// Save prices as cents
replacer: (_, dollars) =>
typeof dollars === 'number'
? Math.round(dollars * 100)
: value,
// Revive cents as dollars
reviver: (_, cents) =>
typeof cents === 'number' ? cents / 100 : cents
})
info
The replacer
and reviver
options are not serialized when building a DTO
.
You can pipe a string
transformer with the .pipe(...)
method:
import { prefix } from 'dynamodb-toolbox/transformers/prefix'
const piped = jsonStringify().pipe(prefix('PREFIX'))
const schema = any().transform(piped)