cogsbox-state 0.5.475-canary.2 → 0.5.475-canary.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-state",
3
- "version": "0.5.475-canary.2",
3
+ "version": "0.5.475-canary.4",
4
4
  "description": "React state management library with form controls and server sync",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/CogsState.tsx CHANGED
@@ -2129,43 +2129,39 @@ function createProxyHandler<
2129
2129
  if (prop === '$validate') {
2130
2130
  return () => {
2131
2131
  const store = getGlobalStore.getState();
2132
- // 1. Get current data and schema
2132
+
2133
+ // 1. Get Data & Schema
2133
2134
  const { value } = getScopedData(stateKey, path, meta);
2134
2135
  const opts = store.getInitialOptions(stateKey);
2135
2136
  const schema =
2136
2137
  opts?.validation?.zodSchemaV4 || opts?.validation?.zodSchemaV3;
2137
2138
 
2139
+ // If no schema, assume valid
2138
2140
  if (!schema) {
2139
2141
  return { success: true, data: value };
2140
2142
  }
2141
2143
 
2142
- // 2. Run Zod
2144
+ // 2. Run Zod Validation
2143
2145
  const result = (schema as any).safeParse(value);
2144
2146
 
2145
- // 3. Clear ANY existing errors for this path first (reset state)
2146
- // You might want to be smarter about this for nested objects,
2147
- // but effectively we need to wipe previous red borders before applying new ones.
2148
- // (Using the logic from $clearZodValidation)
2149
- const clearPath = (currentPath: string[]) => {
2150
- const currentMeta =
2151
- store.getShadowMetadata(stateKey, currentPath) || {};
2152
- store.setShadowMetadata(stateKey, currentPath, {
2147
+ // 3. Update State Logic
2148
+ if (result.success) {
2149
+ const currentMeta = store.getShadowMetadata(stateKey, path) || {};
2150
+ store.setShadowMetadata(stateKey, path, {
2153
2151
  ...currentMeta,
2154
2152
  validation: {
2155
- status: 'NOT_VALIDATED',
2153
+ status: 'VALID',
2156
2154
  errors: [],
2157
2155
  lastValidated: Date.now(),
2158
2156
  },
2159
2157
  });
2160
- };
2161
- // Note: Ideally you recursively clear errors here, but for now we proceed to add new ones.
2158
+ } else {
2159
+ // Zod v4 uses 'issues', Zod v3 uses 'errors'
2160
+ const zodErrors =
2161
+ result.error?.issues || result.error?.errors || [];
2162
2162
 
2163
- // 4. If Invalid, apply errors to State (This turns the UI red)
2164
- if (!result.success) {
2165
- result.error.errors.forEach((error: any) => {
2166
- // Calculate the exact path to the field with the error
2163
+ zodErrors.forEach((error: any) => {
2167
2164
  const errorPath = [...path, ...error.path.map(String)];
2168
-
2169
2165
  const currentMeta =
2170
2166
  store.getShadowMetadata(stateKey, errorPath) || {};
2171
2167
 
@@ -2182,15 +2178,14 @@ function createProxyHandler<
2182
2178
  },
2183
2179
  ],
2184
2180
  lastValidated: Date.now(),
2185
- validatedValue: getShadowValue(stateKey, errorPath),
2181
+ validatedValue: store.getShadowValue(stateKey, errorPath),
2186
2182
  },
2187
2183
  });
2188
2184
  });
2189
-
2190
- // Notify components to re-render and show the errors
2191
- notifyComponents(stateKey);
2192
2185
  }
2193
2186
 
2187
+ notifyComponents(stateKey);
2188
+
2194
2189
  return result;
2195
2190
  };
2196
2191
  }