@juantroconisf/lib 9.2.0 → 9.3.0

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/index.js CHANGED
@@ -227,6 +227,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
227
227
  type: "scalar" /* Scalar */,
228
228
  compositeKey: id,
229
229
  fieldPath: id,
230
+ realPath: id,
230
231
  value: getNestedValue2(state, id),
231
232
  id
232
233
  };
@@ -249,6 +250,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
249
250
  compositeKey: `${arrayKey2}.${itemId}.${field}`,
250
251
  fieldPath: arg0,
251
252
  // "array.field"
253
+ realPath: `${arrayKey2}.${index2}.${field}`,
252
254
  value,
253
255
  arrayKey: arrayKey2,
254
256
  itemId,
@@ -264,6 +266,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
264
266
  type: "primitiveArray" /* PrimitiveArray */,
265
267
  compositeKey: `${arrayKey}.@${index}`,
266
268
  fieldPath: `${arrayKey}`,
269
+ realPath: `${arrayKey}.${index}`,
267
270
  value: arr?.[index],
268
271
  arrayKey,
269
272
  index
@@ -281,6 +284,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
281
284
  compositeKey: `${arrayKey}.${itemId}.${field}`,
282
285
  fieldPath: `${arrayKey}.${field}`,
283
286
  // Normalized path for rules
287
+ realPath: `${arrayKey}.${index}.${field}`,
284
288
  value,
285
289
  arrayKey,
286
290
  itemId,
@@ -301,6 +305,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
301
305
  compositeKey: `${parentKey}.${parentId}.${field}.@${index}`,
302
306
  fieldPath: `${parentKey}.${field}`,
303
307
  // Roughly?
308
+ realPath: `${parentKey}.${parentIndex}.${field}.${index}`,
304
309
  value,
305
310
  parentKey,
306
311
  parentId,
@@ -403,10 +408,14 @@ function useForm(schema, {
403
408
  return rule;
404
409
  }, []);
405
410
  const runValidation = (0, import_react2.useCallback)(
406
- (ruleDef, value, compositeKey) => {
411
+ (ruleDef, value, compositeKey, realPath, fullState) => {
407
412
  if ((0, import_yup2.isSchema)(ruleDef)) {
408
413
  try {
409
- ruleDef.validateSync(value);
414
+ if (validationSchema && realPath && fullState !== void 0) {
415
+ validationSchema.validateSyncAt(realPath, fullState);
416
+ } else {
417
+ ruleDef.validateSync(value);
418
+ }
410
419
  return false;
411
420
  } catch (err) {
412
421
  const error = {
@@ -428,13 +437,14 @@ function useForm(schema, {
428
437
  }
429
438
  return false;
430
439
  },
431
- []
440
+ [validationSchema]
432
441
  );
433
442
  const validateField = (0, import_react2.useCallback)(
434
- (compositeKey, fieldPath, value) => {
443
+ (compositeKey, fieldPath, realPath, value, fullState) => {
435
444
  let schemaRule = getRule(fieldPath, validationSchema);
436
445
  if (schemaRule) {
437
- if (runValidation(schemaRule, value, compositeKey)) return true;
446
+ if (runValidation(schemaRule, value, compositeKey, realPath, fullState))
447
+ return true;
438
448
  }
439
449
  setMetadata((prev) => {
440
450
  const newMap = new Map(prev);
@@ -530,53 +540,56 @@ function useForm(schema, {
530
540
  } catch {
531
541
  }
532
542
  }
533
- setState((prev) => {
534
- if (type === "scalar" /* Scalar */) {
535
- return handleNestedChange({
536
- state: prev,
537
- id: compositeKey,
538
- value: finalValue,
539
- hasNestedValues: compositeKey.includes(".")
540
- });
541
- }
542
- if (type === "primitiveArray" /* PrimitiveArray */) {
543
- return handleNestedChange({
544
- state: prev,
545
- id: `${arrayKey}.${index}`,
546
- value: finalValue,
547
- hasNestedValues: true
548
- });
549
- }
550
- if (type === "objectArray" /* ObjectArray */) {
551
- return handleArrayItemChange({
552
- state: prev,
553
- arrayKey,
554
- index,
555
- field: resolution.field,
556
- value: finalValue
557
- });
558
- }
559
- if (type === "nestedPrimitiveArray" /* NestedPrimitiveArray */) {
560
- const pIndex = getIndex(parentKey, parentId);
561
- if (pIndex === void 0) return prev;
562
- const parentArr = [...prev[parentKey]];
543
+ let nextState = stateRef.current;
544
+ if (type === "scalar" /* Scalar */) {
545
+ nextState = handleNestedChange({
546
+ state: nextState,
547
+ id: compositeKey,
548
+ value: finalValue,
549
+ hasNestedValues: compositeKey.includes(".")
550
+ });
551
+ } else if (type === "primitiveArray" /* PrimitiveArray */) {
552
+ nextState = handleNestedChange({
553
+ state: nextState,
554
+ id: `${arrayKey}.${index}`,
555
+ value: finalValue,
556
+ hasNestedValues: true
557
+ });
558
+ } else if (type === "objectArray" /* ObjectArray */) {
559
+ nextState = handleArrayItemChange({
560
+ state: nextState,
561
+ arrayKey,
562
+ index,
563
+ field: resolution.field,
564
+ value: finalValue
565
+ });
566
+ } else if (type === "nestedPrimitiveArray" /* NestedPrimitiveArray */) {
567
+ const pIndex = getIndex(parentKey, parentId);
568
+ if (pIndex !== void 0) {
569
+ const parentArr = [...nextState[parentKey]];
563
570
  const pItem = { ...parentArr[pIndex] };
564
571
  const nestedArr = [...getNestedValue(pItem, nestedField) || []];
565
572
  nestedArr[index] = finalValue;
566
573
  pItem[nestedField] = nestedArr;
567
574
  parentArr[pIndex] = pItem;
568
- return { ...prev, [parentKey]: parentArr };
575
+ nextState = { ...nextState, [parentKey]: parentArr };
569
576
  }
570
- return prev;
571
- });
572
- validateField(compositeKey, fieldPath, finalValue);
577
+ }
578
+ setState(nextState);
579
+ validateField(
580
+ compositeKey,
581
+ fieldPath,
582
+ resolution.realPath,
583
+ finalValue,
584
+ nextState
585
+ );
573
586
  },
574
587
  [getIndex, validateField, getRule, validationSchema]
575
588
  );
576
589
  const createHandlers = (0, import_react2.useCallback)(
577
590
  (resolution) => {
578
591
  if (!resolution) return {};
579
- const { compositeKey, fieldPath, value } = resolution;
592
+ const { compositeKey, fieldPath, realPath, value } = resolution;
580
593
  const meta = metadataRef.current.get(compositeKey);
581
594
  const isTouched = meta?.isTouched;
582
595
  return {
@@ -585,7 +598,13 @@ function useForm(schema, {
585
598
  errorMessage: isTouched ? meta?.errorMessage || "" : "",
586
599
  onBlur: () => {
587
600
  if (metadataRef.current.get(compositeKey)?.isTouched) return;
588
- validateField(compositeKey, fieldPath, value);
601
+ validateField(
602
+ compositeKey,
603
+ fieldPath,
604
+ realPath,
605
+ value,
606
+ stateRef.current
607
+ );
589
608
  setMetadata((prev) => {
590
609
  const newMap = new Map(prev);
591
610
  const current = newMap.get(compositeKey) || {
@@ -759,7 +778,13 @@ function useForm(schema, {
759
778
  );
760
779
  const onBlur = (0, import_react2.useCallback)(
761
780
  (id) => {
762
- validateField(String(id), String(id), stateRef.current[id]);
781
+ validateField(
782
+ String(id),
783
+ String(id),
784
+ String(id),
785
+ stateRef.current[id],
786
+ stateRef.current
787
+ );
763
788
  setMetadata((prev) => {
764
789
  const newMap = new Map(prev);
765
790
  const current = newMap.get(String(id)) || {
@@ -790,8 +815,14 @@ function useForm(schema, {
790
815
  const polymorphicOnSelectionChange = (0, import_react2.useCallback)(
791
816
  (id, val) => {
792
817
  const fixed = typeof val === "string" || val === null ? val : Array.from(val);
793
- setState((prev) => handleNestedChange({ state: prev, id, value: fixed }));
794
- validateField(String(id), String(id), fixed);
818
+ let nextState = handleNestedChange({
819
+ state: stateRef.current,
820
+ id,
821
+ value: fixed,
822
+ hasNestedValues: String(id).includes(".")
823
+ });
824
+ setState(nextState);
825
+ validateField(String(id), String(id), String(id), fixed, nextState);
795
826
  },
796
827
  [validateField]
797
828
  );
package/dist/index.mjs CHANGED
@@ -201,6 +201,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
201
201
  type: "scalar" /* Scalar */,
202
202
  compositeKey: id,
203
203
  fieldPath: id,
204
+ realPath: id,
204
205
  value: getNestedValue2(state, id),
205
206
  id
206
207
  };
@@ -223,6 +224,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
223
224
  compositeKey: `${arrayKey2}.${itemId}.${field}`,
224
225
  fieldPath: arg0,
225
226
  // "array.field"
227
+ realPath: `${arrayKey2}.${index2}.${field}`,
226
228
  value,
227
229
  arrayKey: arrayKey2,
228
230
  itemId,
@@ -238,6 +240,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
238
240
  type: "primitiveArray" /* PrimitiveArray */,
239
241
  compositeKey: `${arrayKey}.@${index}`,
240
242
  fieldPath: `${arrayKey}`,
243
+ realPath: `${arrayKey}.${index}`,
241
244
  value: arr?.[index],
242
245
  arrayKey,
243
246
  index
@@ -255,6 +258,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
255
258
  compositeKey: `${arrayKey}.${itemId}.${field}`,
256
259
  fieldPath: `${arrayKey}.${field}`,
257
260
  // Normalized path for rules
261
+ realPath: `${arrayKey}.${index}.${field}`,
258
262
  value,
259
263
  arrayKey,
260
264
  itemId,
@@ -275,6 +279,7 @@ function resolveFieldData(args, state, getIndex, getNestedValue2) {
275
279
  compositeKey: `${parentKey}.${parentId}.${field}.@${index}`,
276
280
  fieldPath: `${parentKey}.${field}`,
277
281
  // Roughly?
282
+ realPath: `${parentKey}.${parentIndex}.${field}.${index}`,
278
283
  value,
279
284
  parentKey,
280
285
  parentId,
@@ -377,10 +382,14 @@ function useForm(schema, {
377
382
  return rule;
378
383
  }, []);
379
384
  const runValidation = useCallback(
380
- (ruleDef, value, compositeKey) => {
385
+ (ruleDef, value, compositeKey, realPath, fullState) => {
381
386
  if (isSchema(ruleDef)) {
382
387
  try {
383
- ruleDef.validateSync(value);
388
+ if (validationSchema && realPath && fullState !== void 0) {
389
+ validationSchema.validateSyncAt(realPath, fullState);
390
+ } else {
391
+ ruleDef.validateSync(value);
392
+ }
384
393
  return false;
385
394
  } catch (err) {
386
395
  const error = {
@@ -402,13 +411,14 @@ function useForm(schema, {
402
411
  }
403
412
  return false;
404
413
  },
405
- []
414
+ [validationSchema]
406
415
  );
407
416
  const validateField = useCallback(
408
- (compositeKey, fieldPath, value) => {
417
+ (compositeKey, fieldPath, realPath, value, fullState) => {
409
418
  let schemaRule = getRule(fieldPath, validationSchema);
410
419
  if (schemaRule) {
411
- if (runValidation(schemaRule, value, compositeKey)) return true;
420
+ if (runValidation(schemaRule, value, compositeKey, realPath, fullState))
421
+ return true;
412
422
  }
413
423
  setMetadata((prev) => {
414
424
  const newMap = new Map(prev);
@@ -504,53 +514,56 @@ function useForm(schema, {
504
514
  } catch {
505
515
  }
506
516
  }
507
- setState((prev) => {
508
- if (type === "scalar" /* Scalar */) {
509
- return handleNestedChange({
510
- state: prev,
511
- id: compositeKey,
512
- value: finalValue,
513
- hasNestedValues: compositeKey.includes(".")
514
- });
515
- }
516
- if (type === "primitiveArray" /* PrimitiveArray */) {
517
- return handleNestedChange({
518
- state: prev,
519
- id: `${arrayKey}.${index}`,
520
- value: finalValue,
521
- hasNestedValues: true
522
- });
523
- }
524
- if (type === "objectArray" /* ObjectArray */) {
525
- return handleArrayItemChange({
526
- state: prev,
527
- arrayKey,
528
- index,
529
- field: resolution.field,
530
- value: finalValue
531
- });
532
- }
533
- if (type === "nestedPrimitiveArray" /* NestedPrimitiveArray */) {
534
- const pIndex = getIndex(parentKey, parentId);
535
- if (pIndex === void 0) return prev;
536
- const parentArr = [...prev[parentKey]];
517
+ let nextState = stateRef.current;
518
+ if (type === "scalar" /* Scalar */) {
519
+ nextState = handleNestedChange({
520
+ state: nextState,
521
+ id: compositeKey,
522
+ value: finalValue,
523
+ hasNestedValues: compositeKey.includes(".")
524
+ });
525
+ } else if (type === "primitiveArray" /* PrimitiveArray */) {
526
+ nextState = handleNestedChange({
527
+ state: nextState,
528
+ id: `${arrayKey}.${index}`,
529
+ value: finalValue,
530
+ hasNestedValues: true
531
+ });
532
+ } else if (type === "objectArray" /* ObjectArray */) {
533
+ nextState = handleArrayItemChange({
534
+ state: nextState,
535
+ arrayKey,
536
+ index,
537
+ field: resolution.field,
538
+ value: finalValue
539
+ });
540
+ } else if (type === "nestedPrimitiveArray" /* NestedPrimitiveArray */) {
541
+ const pIndex = getIndex(parentKey, parentId);
542
+ if (pIndex !== void 0) {
543
+ const parentArr = [...nextState[parentKey]];
537
544
  const pItem = { ...parentArr[pIndex] };
538
545
  const nestedArr = [...getNestedValue(pItem, nestedField) || []];
539
546
  nestedArr[index] = finalValue;
540
547
  pItem[nestedField] = nestedArr;
541
548
  parentArr[pIndex] = pItem;
542
- return { ...prev, [parentKey]: parentArr };
549
+ nextState = { ...nextState, [parentKey]: parentArr };
543
550
  }
544
- return prev;
545
- });
546
- validateField(compositeKey, fieldPath, finalValue);
551
+ }
552
+ setState(nextState);
553
+ validateField(
554
+ compositeKey,
555
+ fieldPath,
556
+ resolution.realPath,
557
+ finalValue,
558
+ nextState
559
+ );
547
560
  },
548
561
  [getIndex, validateField, getRule, validationSchema]
549
562
  );
550
563
  const createHandlers = useCallback(
551
564
  (resolution) => {
552
565
  if (!resolution) return {};
553
- const { compositeKey, fieldPath, value } = resolution;
566
+ const { compositeKey, fieldPath, realPath, value } = resolution;
554
567
  const meta = metadataRef.current.get(compositeKey);
555
568
  const isTouched = meta?.isTouched;
556
569
  return {
@@ -559,7 +572,13 @@ function useForm(schema, {
559
572
  errorMessage: isTouched ? meta?.errorMessage || "" : "",
560
573
  onBlur: () => {
561
574
  if (metadataRef.current.get(compositeKey)?.isTouched) return;
562
- validateField(compositeKey, fieldPath, value);
575
+ validateField(
576
+ compositeKey,
577
+ fieldPath,
578
+ realPath,
579
+ value,
580
+ stateRef.current
581
+ );
563
582
  setMetadata((prev) => {
564
583
  const newMap = new Map(prev);
565
584
  const current = newMap.get(compositeKey) || {
@@ -733,7 +752,13 @@ function useForm(schema, {
733
752
  );
734
753
  const onBlur = useCallback(
735
754
  (id) => {
736
- validateField(String(id), String(id), stateRef.current[id]);
755
+ validateField(
756
+ String(id),
757
+ String(id),
758
+ String(id),
759
+ stateRef.current[id],
760
+ stateRef.current
761
+ );
737
762
  setMetadata((prev) => {
738
763
  const newMap = new Map(prev);
739
764
  const current = newMap.get(String(id)) || {
@@ -764,8 +789,14 @@ function useForm(schema, {
764
789
  const polymorphicOnSelectionChange = useCallback(
765
790
  (id, val) => {
766
791
  const fixed = typeof val === "string" || val === null ? val : Array.from(val);
767
- setState((prev) => handleNestedChange({ state: prev, id, value: fixed }));
768
- validateField(String(id), String(id), fixed);
792
+ let nextState = handleNestedChange({
793
+ state: stateRef.current,
794
+ id,
795
+ value: fixed,
796
+ hasNestedValues: String(id).includes(".")
797
+ });
798
+ setState(nextState);
799
+ validateField(String(id), String(id), String(id), fixed, nextState);
769
800
  },
770
801
  [validateField]
771
802
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juantroconisf/lib",
3
- "version": "9.2.0",
3
+ "version": "9.3.0",
4
4
  "description": "A form validation library for HeroUI.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",