react-rooks
Version:
Store management for React and React Native with simple hooks !
25 lines (24 loc) • 957 B
JavaScript
import { createRook } from "./create";
export const createZodRook = (schema, { storeReducer: customStoreReducer, onValidationError, } = {}) => {
console.log("Default store:", schema);
const defaultStore = schema.parse({});
const storeReducer = (values, currentStore) => {
let valuesToValidate;
if (customStoreReducer) {
valuesToValidate = customStoreReducer(values, currentStore);
}
else {
valuesToValidate = Object.assign(Object.assign({}, currentStore), values);
}
const parsedStore = schema.safeParse(valuesToValidate);
if (!parsedStore.success) {
onValidationError === null || onValidationError === void 0 ? void 0 : onValidationError(parsedStore.error, valuesToValidate, currentStore);
return currentStore;
}
return parsedStore.data;
};
return createRook({
defaultStore,
storeReducer,
});
};