cogsbox-state 0.5.389 → 0.5.390
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CogsState.d.ts +5 -0
- package/dist/CogsState.jsx +500 -490
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +28 -0
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -261,8 +261,13 @@ export type ObjectEndType<T> = EndType<T> & {
|
|
|
261
261
|
stateObject: (callbackfn: (value: T, setter: StateObject<T>) => void) => any;
|
|
262
262
|
delete: () => void;
|
|
263
263
|
};
|
|
264
|
+
export type ValidationError = {
|
|
265
|
+
path: (string | number)[];
|
|
266
|
+
message: string;
|
|
267
|
+
};
|
|
264
268
|
type EffectFunction<T, R> = (state: T) => R;
|
|
265
269
|
export type EndType<T, IsArrayElement = false> = {
|
|
270
|
+
addValidation: (errors: ValidationError[]) => void;
|
|
266
271
|
applyJsonPatch: (patches: any[]) => void;
|
|
267
272
|
update: UpdateType<T>;
|
|
268
273
|
_path: string[];
|
|
@@ -2658,6 +2663,29 @@ function createProxyHandler<T>(
|
|
|
2658
2663
|
};
|
|
2659
2664
|
}
|
|
2660
2665
|
if (path.length == 0) {
|
|
2666
|
+
if (prop === "addValidation") {
|
|
2667
|
+
return (errors: ValidationError[]) => {
|
|
2668
|
+
const init = getGlobalStore
|
|
2669
|
+
.getState()
|
|
2670
|
+
.getInitialOptions(stateKey)?.validation;
|
|
2671
|
+
|
|
2672
|
+
if (!init?.key) {
|
|
2673
|
+
throw new Error("Validation key not found");
|
|
2674
|
+
}
|
|
2675
|
+
|
|
2676
|
+
// Clear existing errors for this validation key
|
|
2677
|
+
removeValidationError(init.key);
|
|
2678
|
+
|
|
2679
|
+
// Add each new error
|
|
2680
|
+
errors.forEach((error) => {
|
|
2681
|
+
const fullErrorPath = [init.key, ...error.path].join(".");
|
|
2682
|
+
addValidationError(fullErrorPath, error.message);
|
|
2683
|
+
});
|
|
2684
|
+
|
|
2685
|
+
// Notify components to update
|
|
2686
|
+
notifyComponents(stateKey);
|
|
2687
|
+
};
|
|
2688
|
+
}
|
|
2661
2689
|
if (prop === "applyJsonPatch") {
|
|
2662
2690
|
return (patches: any[]) => {
|
|
2663
2691
|
// This part is correct.
|