Seitu - Type-Safe Utilities
Utils

Validation

repairValueObjectWithDefault() ⇒

Repair broken web storage value object if value was object but not all keys are in the default value object.

Returns: A new object with the existing keys in the value object that are not in the default value object.

Examples

import { createWebStorageValue } from 'seitu/web'
import { repairValueObjectWithDefault } from 'seitu/utils'
import * as z from 'zod'

const value = createWebStorageValue({
  type: 'localStorage',
  schema: z.object({ a: z.number(), b: z.string() }),
  key: 'storage-key',
  defaultValue: { a: 0, b: 'default' },
  onValidationError: repairValueObjectWithDefault,
})
value.get() // { a: 0, b: 'default' }
window.localStorage.setItem('storage-key', JSON.stringify({ a: 1 }))
value.get() // { a: 1, b: 'default' }

On this page