cogsbox-state 0.5.483 → 0.5.484

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/src/store.ts CHANGED
@@ -316,16 +316,25 @@ function getTypeFromZodSchema(
316
316
 
317
317
  const typeIdentifier = def.typeName || def.type || current._type;
318
318
 
319
- // --- START: THE CRITICAL FIX FOR ZodUnion ---
320
319
  if (typeIdentifier === 'ZodUnion' || typeIdentifier === 'union') {
321
320
  if (def.options && def.options.length > 0) {
322
- current = def.options[0]; // Proceed by analyzing the FIRST option of the union
323
- continue; // Restart the loop with the new schema
321
+ const preferredOption = def.options.find((option: any) => {
322
+ const optionDef = option?.def || option?._def;
323
+ const optionType =
324
+ optionDef?.typeName || optionDef?.type || option?._type;
325
+ return (
326
+ optionType !== 'ZodNull' &&
327
+ optionType !== 'null' &&
328
+ optionType !== 'ZodUndefined' &&
329
+ optionType !== 'undefined'
330
+ );
331
+ });
332
+ current = preferredOption ?? def.options[0];
333
+ continue;
324
334
  } else {
325
- break; // Union with no options, cannot determine type
335
+ break;
326
336
  }
327
337
  }
328
- // --- END: THE CRITICAL FIX ---
329
338
 
330
339
  if (typeIdentifier === 'ZodOptional' || typeIdentifier === 'optional') {
331
340
  isOptional = true;
@@ -684,9 +693,10 @@ function getSchemaAtPath(schema: any, path: string[]): any {
684
693
  const typeIdentifier = def?.typeName || def?.type || containerSchema._type;
685
694
 
686
695
  if (typeIdentifier === 'ZodObject' || typeIdentifier === 'object') {
687
- // VITAL FIX: Check for `shape` inside `def` first, then on the schema itself.
696
+ const rawShape =
697
+ def?.shape ?? containerSchema.shape ?? containerSchema._shape;
688
698
  const shape =
689
- def?.shape || containerSchema.shape || containerSchema._shape;
699
+ typeof rawShape === 'function' ? rawShape() : rawShape;
690
700
  currentSchema = shape?.[segment];
691
701
  } else if (typeIdentifier === 'ZodArray' || typeIdentifier === 'array') {
692
702
  // For arrays, the next schema is always the element's schema.
@@ -1006,6 +1016,13 @@ export const getGlobalStore = create<CogsGlobalState>((set, get) => ({
1006
1016
 
1007
1017
  const storageKey = Array.isArray(initialState) ? `[${key}` : key;
1008
1018
  shadowStateStore.set(storageKey, newRoot);
1019
+
1020
+ const latestOptions = get().getInitialOptions(key);
1021
+ if (latestOptions?.validation?.zodSchemaV4) {
1022
+ updateShadowTypeInfo(key, latestOptions.validation.zodSchemaV4, 'zod4');
1023
+ } else if (latestOptions?.validation?.zodSchemaV3) {
1024
+ updateShadowTypeInfo(key, latestOptions.validation.zodSchemaV3, 'zod3');
1025
+ }
1009
1026
  },
1010
1027
  getShadowNode: (key: string, path: string[]): ShadowNode | undefined => {
1011
1028
  let current: any =