Report persisted schema validation failures
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||||
|
|
||||||
import {Schema, schema} from '#/state/persisted/schema'
|
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
|
import {Schema, schema} from '#/state/persisted/schema'
|
||||||
|
|
||||||
const BSKY_STORAGE = 'BSKY_STORAGE'
|
const BSKY_STORAGE = 'BSKY_STORAGE'
|
||||||
|
|
||||||
@@ -13,8 +13,17 @@ export async function write(value: Schema) {
|
|||||||
export async function read(): Promise<Schema | undefined> {
|
export async function read(): Promise<Schema | undefined> {
|
||||||
const rawData = await AsyncStorage.getItem(BSKY_STORAGE)
|
const rawData = await AsyncStorage.getItem(BSKY_STORAGE)
|
||||||
const objData = rawData ? JSON.parse(rawData) : undefined
|
const objData = rawData ? JSON.parse(rawData) : undefined
|
||||||
if (schema.safeParse(objData).success) {
|
const parsed = schema.safeParse(objData)
|
||||||
|
if (parsed.success) {
|
||||||
return objData
|
return objData
|
||||||
|
} else {
|
||||||
|
const errors = parsed.error.errors.map(e => ({
|
||||||
|
code: e.code,
|
||||||
|
expected: e.expected,
|
||||||
|
path: e.path,
|
||||||
|
}))
|
||||||
|
logger.error(`persisted store: data failed validation on read`, {errors})
|
||||||
|
return undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user