@fuf-stack/uniform 1.6.1 → 1.6.2

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.
Files changed (41) hide show
  1. package/dist/Checkboxes/index.cjs +1 -1
  2. package/dist/Checkboxes/index.js +1 -1
  3. package/dist/FieldArray/index.cjs +1 -1
  4. package/dist/FieldArray/index.js +1 -1
  5. package/dist/Form/index.cjs +1 -1
  6. package/dist/Form/index.js +1 -1
  7. package/dist/Input/index.cjs +1 -1
  8. package/dist/Input/index.js +1 -1
  9. package/dist/RadioBoxes/index.cjs +1 -1
  10. package/dist/RadioBoxes/index.js +1 -1
  11. package/dist/RadioTabs/index.cjs +1 -1
  12. package/dist/RadioTabs/index.js +1 -1
  13. package/dist/Radios/index.cjs +1 -1
  14. package/dist/Radios/index.js +1 -1
  15. package/dist/Select/index.cjs +1 -1
  16. package/dist/Select/index.js +1 -1
  17. package/dist/SubmitButton/index.cjs +1 -1
  18. package/dist/SubmitButton/index.js +1 -1
  19. package/dist/Switch/index.cjs +1 -1
  20. package/dist/Switch/index.js +1 -1
  21. package/dist/TextArea/index.cjs +1 -1
  22. package/dist/TextArea/index.js +1 -1
  23. package/dist/{chunk-TEIXBRF5.cjs → chunk-D5UUY4BD.cjs} +12 -3
  24. package/dist/chunk-D5UUY4BD.cjs.map +1 -0
  25. package/dist/{chunk-CHNYQVWO.js → chunk-L5TXRTPB.js} +12 -3
  26. package/dist/chunk-L5TXRTPB.js.map +1 -0
  27. package/dist/hooks/index.cjs +2 -2
  28. package/dist/hooks/index.d.cts +1 -1
  29. package/dist/hooks/index.d.ts +1 -1
  30. package/dist/hooks/index.js +1 -1
  31. package/dist/hooks/useClientValidation/index.cjs +2 -2
  32. package/dist/hooks/useClientValidation/index.d.cts +103 -34
  33. package/dist/hooks/useClientValidation/index.d.ts +103 -34
  34. package/dist/hooks/useClientValidation/index.js +1 -1
  35. package/dist/index.cjs +2 -2
  36. package/dist/index.d.cts +1 -1
  37. package/dist/index.d.ts +1 -1
  38. package/dist/index.js +1 -1
  39. package/package.json +1 -1
  40. package/dist/chunk-CHNYQVWO.js.map +0 -1
  41. package/dist/chunk-TEIXBRF5.cjs.map +0 -1
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -12,7 +12,7 @@ require('../chunk-NHEZXA4H.cjs');
12
12
  require('../chunk-JMFDNTVC.cjs');
13
13
  require('../chunk-PCTYJUY7.cjs');
14
14
  require('../chunk-OE5BOGGX.cjs');
15
- require('../chunk-TEIXBRF5.cjs');
15
+ require('../chunk-D5UUY4BD.cjs');
16
16
  require('../chunk-NZBO4N3S.cjs');
17
17
  require('../chunk-Z353BLWI.cjs');
18
18
  require('../chunk-555JRYCS.cjs');
@@ -12,7 +12,7 @@ import "../chunk-ELYGQTXB.js";
12
12
  import "../chunk-CK362NWK.js";
13
13
  import "../chunk-AHJJIANM.js";
14
14
  import "../chunk-NTDKZW4E.js";
15
- import "../chunk-CHNYQVWO.js";
15
+ import "../chunk-L5TXRTPB.js";
16
16
  import "../chunk-PO5UQAU6.js";
17
17
  import "../chunk-76KOVUDN.js";
18
18
  import "../chunk-K2V4ULA2.js";
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -11,7 +11,7 @@ require('../chunk-NHEZXA4H.cjs');
11
11
  require('../chunk-JMFDNTVC.cjs');
12
12
  require('../chunk-PCTYJUY7.cjs');
13
13
  require('../chunk-OE5BOGGX.cjs');
14
- require('../chunk-TEIXBRF5.cjs');
14
+ require('../chunk-D5UUY4BD.cjs');
15
15
  require('../chunk-NZBO4N3S.cjs');
16
16
  require('../chunk-Z353BLWI.cjs');
17
17
  require('../chunk-555JRYCS.cjs');
@@ -11,7 +11,7 @@ import "../chunk-ELYGQTXB.js";
11
11
  import "../chunk-CK362NWK.js";
12
12
  import "../chunk-AHJJIANM.js";
13
13
  import "../chunk-NTDKZW4E.js";
14
- import "../chunk-CHNYQVWO.js";
14
+ import "../chunk-L5TXRTPB.js";
15
15
  import "../chunk-PO5UQAU6.js";
16
16
  import "../chunk-76KOVUDN.js";
17
17
  import "../chunk-K2V4ULA2.js";
@@ -3,18 +3,23 @@
3
3
  var _chunkNZBO4N3Scjs = require('./chunk-NZBO4N3S.cjs');
4
4
 
5
5
 
6
+ var _chunkZ353BLWIcjs = require('./chunk-Z353BLWI.cjs');
7
+
8
+
6
9
  var _chunk555JRYCScjs = require('./chunk-555JRYCS.cjs');
7
10
 
8
11
  // src/hooks/useClientValidation/useClientValidation.ts
9
12
  var _react = require('react');
10
13
  var _veto = require('@fuf-stack/veto');
11
- var useClientValidation = (data, schemaFactory) => {
14
+ var useClientValidation = (data, schemaFactory, options) => {
15
+ var _a;
12
16
  const {
13
17
  formState: { touchedFields },
14
18
  validation: { setClientValidationSchema },
15
19
  trigger
16
20
  } = _chunkNZBO4N3Scjs.useFormContext.call(void 0, );
17
- const key = _react.useId.call(void 0, );
21
+ const autoGeneratedKey = _react.useId.call(void 0, );
22
+ const key = (_a = options == null ? void 0 : options.key) != null ? _a : autoGeneratedKey;
18
23
  const dataHash = JSON.stringify(data);
19
24
  _react.useEffect.call(void 0, () => {
20
25
  if (data != null) {
@@ -40,6 +45,10 @@ var clientValidationSchemaByName = (name, fieldSchema) => {
40
45
  for (let i = segments.length - 1; i >= 0; i -= 1) {
41
46
  const segment = segments[i];
42
47
  const isNumeric = /^\d+$/.test(segment);
48
+ const isFlatArrayKey = segment === _chunkZ353BLWIcjs.flatArrayKey;
49
+ if (isFlatArrayKey) {
50
+ continue;
51
+ }
43
52
  if (isNumeric) {
44
53
  schema = _veto.array.call(void 0, schema).optional();
45
54
  } else {
@@ -56,4 +65,4 @@ var clientValidationSchemaByName = (name, fieldSchema) => {
56
65
 
57
66
 
58
67
  exports.useClientValidation = useClientValidation; exports.clientValidationSchemaByName = clientValidationSchemaByName;
59
- //# sourceMappingURL=chunk-TEIXBRF5.cjs.map
68
+ //# sourceMappingURL=chunk-D5UUY4BD.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/home/runner/work/pixels/pixels/packages/uniform/dist/chunk-D5UUY4BD.cjs","../src/hooks/useClientValidation/useClientValidation.ts"],"names":[],"mappings":"AAAA;AACE;AACF,wDAA6B;AAC7B;AACE;AACF,wDAA6B;AAC7B;AACE;AACF,wDAA6B;AAC7B;AACA;ACRA,8BAAiC;AAEjC,uCAAmC;AA8D5B,IAAM,oBAAA,EAAsB,CACjC,IAAA,EACA,aAAA,EACA,OAAA,EAAA,GACS;AAtEX,EAAA,IAAA,EAAA;AAuEE,EAAA,MAAM;AAAA,IACJ,SAAA,EAAW,EAAE,cAAc,CAAA;AAAA,IAC3B,UAAA,EAAY,EAAE,0BAA0B,CAAA;AAAA,IACxC;AAAA,EACF,EAAA,EAAI,8CAAA,CAAe;AAGnB,EAAA,MAAM,iBAAA,EAAmB,0BAAA,CAAM;AAC/B,EAAA,MAAM,IAAA,EAAA,CAAM,GAAA,EAAA,QAAA,GAAA,KAAA,EAAA,KAAA,EAAA,EAAA,OAAA,CAAS,GAAA,EAAA,GAAT,KAAA,EAAA,GAAA,EAAgB,gBAAA;AAI5B,EAAA,MAAM,SAAA,EAAW,IAAA,CAAK,SAAA,CAAU,IAAI,CAAA;AAGpC,EAAA,8BAAA,CAAU,EAAA,GAAM;AAEd,IAAA,GAAA,CAAI,KAAA,GAAQ,IAAA,EAAM;AAChB,MAAA,MAAM,iBAAA,EAAmB,aAAA,CAAc,IAAI,CAAA;AAC3C,MAAA,yBAAA,CAA0B,GAAA,EAAK,gBAAgB,CAAA;AAAA,IACjD,EAAA,KAAO;AAEL,MAAA,yBAAA,CAA0B,GAAA,EAAK,IAAI,CAAA;AAAA,IACrC;AAGA,IAAA,MAAM,kBAAA,EAAoB,MAAA,CAAO,IAAA,CAAK,aAAa,CAAA;AACnD,IAAA,GAAA,CAAI,iBAAA,CAAkB,OAAA,EAAS,CAAA,EAAG;AAGhC,MAAA,UAAA,CAAW,CAAA,EAAA,GAAY,uCAAA,IAAA,EAAA,IAAA,EAAA,QAAA,EAAA,CAAA,EAAA;AACrB,QAAA,OAAO,OAAA,CAAQ,iBAAiB,CAAA;AAAA,MAClC,CAAA,CAAA,EAAG,CAAC,CAAA;AAAA,IACN;AAGA,IAAA,OAAO,CAAA,EAAA,GAAM;AACX,MAAA,yBAAA,CAA0B,GAAA,EAAK,IAAI,CAAA;AAAA,IACrC,CAAA;AAAA,EAGF,CAAA,EAAG,CAAC,QAAA,EAAU,GAAG,CAAC,CAAA;AACpB,CAAA;AAmFO,IAAM,6BAAA,EAA+B,CAC1C,IAAA,EACA,WAAA,EAAA,GACG;AACH,EAAA,MAAM,SAAA,EAAW,IAAA,CAAK,KAAA,CAAM,GAAG,CAAA;AAG/B,EAAA,IAAI,OAAA,EAAsB,WAAA;AAG1B,EAAA,IAAA,CAAA,IAAS,EAAA,EAAI,QAAA,CAAS,OAAA,EAAS,CAAA,EAAG,EAAA,GAAK,CAAA,EAAG,EAAA,GAAK,CAAA,EAAG;AAChD,IAAA,MAAM,QAAA,EAAU,QAAA,CAAS,CAAC,CAAA;AAE1B,IAAA,MAAM,UAAA,EAAY,OAAA,CAAQ,IAAA,CAAK,OAAO,CAAA;AACtC,IAAA,MAAM,eAAA,EAAiB,QAAA,IAAY,8BAAA;AAEnC,IAAA,GAAA,CAAI,cAAA,EAAgB;AAIlB,MAAA,QAAA;AAAA,IACF;AAEA,IAAA,GAAA,CAAI,SAAA,EAAW;AAEb,MAAA,OAAA,EAAS,yBAAA,MAAY,CAAA,CAAE,QAAA,CAAS,CAAA;AAAA,IAClC,EAAA,KAAO;AAGL,MAAA,MAAM,IAAA,EAAM,+BAAA;AAAY,QACtB,CAAC,OAAO,CAAA,EAAG;AAAA,MACb,CAAC,CAAA;AACD,MAAA,OAAA,EAAS,EAAA,EAAI,EAAA,EAAI,GAAA,CAAI,QAAA,CAAS,EAAA,EAAI,GAAA;AAAA,IACpC;AAAA,EACF;AAEA,EAAA,OAAO,MAAA;AACT,CAAA;AD3KA;AACA;AACE;AACA;AACF,uHAAC","file":"/home/runner/work/pixels/pixels/packages/uniform/dist/chunk-D5UUY4BD.cjs","sourcesContent":[null,"import type { VetoTypeAny } from '@fuf-stack/veto';\n\nimport { useEffect, useId } from 'react';\n\nimport { array, objectLoose } from '@fuf-stack/veto';\n\nimport { flatArrayKey } from '../../helpers';\nimport { useFormContext } from '../useFormContext/useFormContext';\n\n/**\n * Hook for adding dynamic client-side validation to forms.\n *\n * Client validation is used for context-dependent validation rules that require external data\n * (e.g., checking if a username already exists). It complements base validation (passed to Form),\n * which handles static rules like \"required\", min/max length, format, etc.\n *\n * **Typical pattern:**\n * - Base validation: Static, always-on rules (required, format, length constraints)\n * - Client validation: Dynamic rules based on external data (uniqueness, business logic)\n * - Client schemas should use `.nullish()` to allow empty values (base validation handles \"required\")\n *\n * Automatically manages validation schema lifecycle: sets schema when data is provided,\n * clears schema when data is null/undefined, and cleans up on unmount. When the client\n * validation schema changes, automatically re-validates all touched fields.\n *\n * @param data - Data to create validation schema from (or null/undefined to clear validation)\n * @param schemaFactory - Function that creates a validation schema from the data. Schemas should typically use `.nullish()`.\n * @param options - Optional configuration object\n * @param options.key - Optional custom key for registration. Useful when multiple instances should share validation (e.g., in field arrays)\n *\n * @example\n * ```tsx\n * // Base validation handles \"required\" and format\n * const baseSchema = veto({ username: vt.string({ min: 3 }) });\n *\n * // Client validation adds dynamic uniqueness check\n * const { data: teamData } = useTeamQuery(teamId);\n * useClientValidation(teamData, (data) =>\n * vt.objectLoose({\n * username: vt.string()\n * .refine(\n * (value) => !data.existingUsers.includes(value),\n * { message: 'Username already exists' }\n * )\n * .nullish() // Allow empty - base validation handles \"required\"\n * })\n * );\n * ```\n *\n * @example\n * ```tsx\n * // Using custom key in a field array to avoid multiple registrations\n * fields.map((field, index) => (\n * useClientValidation(\n * userData,\n * (data) => clientValidationSchemaByName(\n * `items.${index}.name`,\n * vt.string()\n * .refine((val) => !data.forbidden.includes(val), { message: 'Name not allowed' })\n * .nullish()\n * ),\n * { key: 'items-validation' } // All instances share the same key\n * )\n * ));\n * ```\n */\nexport const useClientValidation = <TData = unknown>(\n data: TData | null | undefined,\n schemaFactory: (data: TData) => VetoTypeAny,\n options?: { key?: string },\n): void => {\n const {\n formState: { touchedFields },\n validation: { setClientValidationSchema },\n trigger,\n } = useFormContext();\n\n // Use provided key or auto-generate unique key\n const autoGeneratedKey = useId();\n const key = options?.key ?? autoGeneratedKey;\n\n // Use data hash instead of object reference to avoid unnecessary re-runs\n // when data object reference changes but data remains the same\n const dataHash = JSON.stringify(data);\n\n // Single effect to manage validation schema lifecycle\n useEffect(() => {\n // Set validation schema when data is available\n if (data != null) {\n const validationSchema = schemaFactory(data);\n setClientValidationSchema(key, validationSchema);\n } else {\n // Clear validation schema when no data\n setClientValidationSchema(key, null);\n }\n\n // Re-validate all touched fields when client validation schema changes\n const touchedFieldNames = Object.keys(touchedFields);\n if (touchedFieldNames.length > 0) {\n // Use setTimeout to ensure the client validation schema update has propagated\n // before triggering re-validation (fixes race condition)\n setTimeout(async () => {\n return trigger(touchedFieldNames);\n }, 1);\n }\n\n // Cleanup on unmount or when dependencies change\n return () => {\n setClientValidationSchema(key, null);\n };\n // Only re-run when data (dataHash) or key changes\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [dataHash, key]);\n};\n\n/**\n * Helper function to create a veto looseObject schema for a single field with nested path support.\n *\n * Parses field paths (dot-notation) and creates properly nested loose object schemas with array support.\n * All intermediate objects and arrays are marked as optional to allow partial data structures.\n * Numeric segments in the path are treated as array indices, and the parent field is wrapped in an array schema.\n * Supports flat arrays (arrays of primitives) via the special `__FLAT__` key.\n *\n * **Important:** The fieldSchema should typically use `.nullish()` to allow empty values. This is because:\n * - Base validation (passed to Form) handles \"required\" constraints\n * - Client validation adds dynamic, context-dependent rules (e.g., \"username already exists\")\n * - Form data processing converts empty strings in flat arrays to `null` (not `undefined`)\n * - `.nullish()` accepts both `null` and `undefined`, while `.optional()` only accepts `undefined`\n *\n * This pattern ensures client validation only validates non-empty values, while base validation\n * enforces required fields and basic constraints (min length, format, etc.).\n *\n * @param name - The dot-separated path to the field (e.g., 'username', 'user.profile.email', 'items.0.name', 'tags.0.__FLAT__')\n * @param fieldSchema - The veto validation schema for the field. Should typically use `.nullish()` to allow empty values.\n * @returns A nested loose object schema matching the field path structure with proper type inference\n *\n * @example\n * ```tsx\n * // Typical usage: base validation handles \"required\", client validation adds dynamic rules\n * // Base validation (in Form component)\n * const baseSchema = veto({ username: vt.string({ min: 3 }) }); // Required + min length\n *\n * // Client validation (dynamic check for existing usernames)\n * const { data: userData } = useUserQuery(userId);\n * useClientValidation(userData, (data) =>\n * clientValidationSchemaByName(\n * 'username',\n * vt.string()\n * .refine(\n * (value) => !data.existingUsernames.includes(value),\n * { message: 'Username already taken' }\n * )\n * .nullish() // Allow empty - base validation handles \"required\"\n * )\n * );\n * ```\n *\n * @example\n * ```tsx\n * // Nested field path with dynamic validation\n * clientValidationSchemaByName(\n * 'user.profile.email',\n * vt.string()\n * .refine(\n * (value) => !reservedEmails.includes(value),\n * { message: 'This email is reserved' }\n * )\n * .nullish()\n * )\n * // => objectLoose({ user: objectLoose({ profile: objectLoose({ email: ... }).optional() }).optional() })\n * ```\n *\n * @example\n * ```tsx\n * // Array of objects with dynamic validation\n * clientValidationSchemaByName(\n * 'items.0.tag',\n * vt.string()\n * .refine((value) => !forbiddenTags.includes(value), { message: 'Tag not allowed' })\n * .nullish()\n * )\n * // => objectLoose({ items: array(objectLoose({ tag: ... })).optional() })\n * ```\n *\n * @example\n * ```tsx\n * // Flat array (array of primitives) with dynamic validation\n * clientValidationSchemaByName(\n * 'tags.0.__FLAT__',\n * vt.string()\n * .refine((value) => !forbiddenWords.includes(value), { message: 'Word not allowed' })\n * .nullish()\n * )\n * // => objectLoose({ tags: array(vt.string().refine(...).nullish()).optional() })\n * ```\n */\nexport const clientValidationSchemaByName = <T extends VetoTypeAny>(\n name: string,\n fieldSchema: T,\n) => {\n const segments = name.split('.');\n\n // Build schema from innermost to outermost\n let schema: VetoTypeAny = fieldSchema;\n\n // Process segments in reverse order to build nested structure\n for (let i = segments.length - 1; i >= 0; i -= 1) {\n const segment = segments[i];\n\n const isNumeric = /^\\d+$/.test(segment);\n const isFlatArrayKey = segment === flatArrayKey;\n\n if (isFlatArrayKey) {\n // Skip the __FLAT__ key - it's just a marker for flat arrays\n // The schema is already correct (primitive type), just continue\n // eslint-disable-next-line no-continue\n continue;\n }\n\n if (isNumeric) {\n // Current segment is an array index - wrap current schema in optional array\n schema = array(schema).optional();\n } else {\n // Current segment is a field name - wrap in objectLoose\n // Make it optional if it's an intermediate object (not the root)\n const obj = objectLoose({\n [segment]: schema,\n });\n schema = i > 0 ? obj.optional() : obj;\n }\n }\n\n return schema;\n};\n"]}
@@ -1,6 +1,9 @@
1
1
  import {
2
2
  useFormContext
3
3
  } from "./chunk-PO5UQAU6.js";
4
+ import {
5
+ flatArrayKey
6
+ } from "./chunk-76KOVUDN.js";
4
7
  import {
5
8
  __async
6
9
  } from "./chunk-K2V4ULA2.js";
@@ -8,13 +11,15 @@ import {
8
11
  // src/hooks/useClientValidation/useClientValidation.ts
9
12
  import { useEffect, useId } from "react";
10
13
  import { array, objectLoose } from "@fuf-stack/veto";
11
- var useClientValidation = (data, schemaFactory) => {
14
+ var useClientValidation = (data, schemaFactory, options) => {
15
+ var _a;
12
16
  const {
13
17
  formState: { touchedFields },
14
18
  validation: { setClientValidationSchema },
15
19
  trigger
16
20
  } = useFormContext();
17
- const key = useId();
21
+ const autoGeneratedKey = useId();
22
+ const key = (_a = options == null ? void 0 : options.key) != null ? _a : autoGeneratedKey;
18
23
  const dataHash = JSON.stringify(data);
19
24
  useEffect(() => {
20
25
  if (data != null) {
@@ -40,6 +45,10 @@ var clientValidationSchemaByName = (name, fieldSchema) => {
40
45
  for (let i = segments.length - 1; i >= 0; i -= 1) {
41
46
  const segment = segments[i];
42
47
  const isNumeric = /^\d+$/.test(segment);
48
+ const isFlatArrayKey = segment === flatArrayKey;
49
+ if (isFlatArrayKey) {
50
+ continue;
51
+ }
43
52
  if (isNumeric) {
44
53
  schema = array(schema).optional();
45
54
  } else {
@@ -56,4 +65,4 @@ export {
56
65
  useClientValidation,
57
66
  clientValidationSchemaByName
58
67
  };
59
- //# sourceMappingURL=chunk-CHNYQVWO.js.map
68
+ //# sourceMappingURL=chunk-L5TXRTPB.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/useClientValidation/useClientValidation.ts"],"sourcesContent":["import type { VetoTypeAny } from '@fuf-stack/veto';\n\nimport { useEffect, useId } from 'react';\n\nimport { array, objectLoose } from '@fuf-stack/veto';\n\nimport { flatArrayKey } from '../../helpers';\nimport { useFormContext } from '../useFormContext/useFormContext';\n\n/**\n * Hook for adding dynamic client-side validation to forms.\n *\n * Client validation is used for context-dependent validation rules that require external data\n * (e.g., checking if a username already exists). It complements base validation (passed to Form),\n * which handles static rules like \"required\", min/max length, format, etc.\n *\n * **Typical pattern:**\n * - Base validation: Static, always-on rules (required, format, length constraints)\n * - Client validation: Dynamic rules based on external data (uniqueness, business logic)\n * - Client schemas should use `.nullish()` to allow empty values (base validation handles \"required\")\n *\n * Automatically manages validation schema lifecycle: sets schema when data is provided,\n * clears schema when data is null/undefined, and cleans up on unmount. When the client\n * validation schema changes, automatically re-validates all touched fields.\n *\n * @param data - Data to create validation schema from (or null/undefined to clear validation)\n * @param schemaFactory - Function that creates a validation schema from the data. Schemas should typically use `.nullish()`.\n * @param options - Optional configuration object\n * @param options.key - Optional custom key for registration. Useful when multiple instances should share validation (e.g., in field arrays)\n *\n * @example\n * ```tsx\n * // Base validation handles \"required\" and format\n * const baseSchema = veto({ username: vt.string({ min: 3 }) });\n *\n * // Client validation adds dynamic uniqueness check\n * const { data: teamData } = useTeamQuery(teamId);\n * useClientValidation(teamData, (data) =>\n * vt.objectLoose({\n * username: vt.string()\n * .refine(\n * (value) => !data.existingUsers.includes(value),\n * { message: 'Username already exists' }\n * )\n * .nullish() // Allow empty - base validation handles \"required\"\n * })\n * );\n * ```\n *\n * @example\n * ```tsx\n * // Using custom key in a field array to avoid multiple registrations\n * fields.map((field, index) => (\n * useClientValidation(\n * userData,\n * (data) => clientValidationSchemaByName(\n * `items.${index}.name`,\n * vt.string()\n * .refine((val) => !data.forbidden.includes(val), { message: 'Name not allowed' })\n * .nullish()\n * ),\n * { key: 'items-validation' } // All instances share the same key\n * )\n * ));\n * ```\n */\nexport const useClientValidation = <TData = unknown>(\n data: TData | null | undefined,\n schemaFactory: (data: TData) => VetoTypeAny,\n options?: { key?: string },\n): void => {\n const {\n formState: { touchedFields },\n validation: { setClientValidationSchema },\n trigger,\n } = useFormContext();\n\n // Use provided key or auto-generate unique key\n const autoGeneratedKey = useId();\n const key = options?.key ?? autoGeneratedKey;\n\n // Use data hash instead of object reference to avoid unnecessary re-runs\n // when data object reference changes but data remains the same\n const dataHash = JSON.stringify(data);\n\n // Single effect to manage validation schema lifecycle\n useEffect(() => {\n // Set validation schema when data is available\n if (data != null) {\n const validationSchema = schemaFactory(data);\n setClientValidationSchema(key, validationSchema);\n } else {\n // Clear validation schema when no data\n setClientValidationSchema(key, null);\n }\n\n // Re-validate all touched fields when client validation schema changes\n const touchedFieldNames = Object.keys(touchedFields);\n if (touchedFieldNames.length > 0) {\n // Use setTimeout to ensure the client validation schema update has propagated\n // before triggering re-validation (fixes race condition)\n setTimeout(async () => {\n return trigger(touchedFieldNames);\n }, 1);\n }\n\n // Cleanup on unmount or when dependencies change\n return () => {\n setClientValidationSchema(key, null);\n };\n // Only re-run when data (dataHash) or key changes\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [dataHash, key]);\n};\n\n/**\n * Helper function to create a veto looseObject schema for a single field with nested path support.\n *\n * Parses field paths (dot-notation) and creates properly nested loose object schemas with array support.\n * All intermediate objects and arrays are marked as optional to allow partial data structures.\n * Numeric segments in the path are treated as array indices, and the parent field is wrapped in an array schema.\n * Supports flat arrays (arrays of primitives) via the special `__FLAT__` key.\n *\n * **Important:** The fieldSchema should typically use `.nullish()` to allow empty values. This is because:\n * - Base validation (passed to Form) handles \"required\" constraints\n * - Client validation adds dynamic, context-dependent rules (e.g., \"username already exists\")\n * - Form data processing converts empty strings in flat arrays to `null` (not `undefined`)\n * - `.nullish()` accepts both `null` and `undefined`, while `.optional()` only accepts `undefined`\n *\n * This pattern ensures client validation only validates non-empty values, while base validation\n * enforces required fields and basic constraints (min length, format, etc.).\n *\n * @param name - The dot-separated path to the field (e.g., 'username', 'user.profile.email', 'items.0.name', 'tags.0.__FLAT__')\n * @param fieldSchema - The veto validation schema for the field. Should typically use `.nullish()` to allow empty values.\n * @returns A nested loose object schema matching the field path structure with proper type inference\n *\n * @example\n * ```tsx\n * // Typical usage: base validation handles \"required\", client validation adds dynamic rules\n * // Base validation (in Form component)\n * const baseSchema = veto({ username: vt.string({ min: 3 }) }); // Required + min length\n *\n * // Client validation (dynamic check for existing usernames)\n * const { data: userData } = useUserQuery(userId);\n * useClientValidation(userData, (data) =>\n * clientValidationSchemaByName(\n * 'username',\n * vt.string()\n * .refine(\n * (value) => !data.existingUsernames.includes(value),\n * { message: 'Username already taken' }\n * )\n * .nullish() // Allow empty - base validation handles \"required\"\n * )\n * );\n * ```\n *\n * @example\n * ```tsx\n * // Nested field path with dynamic validation\n * clientValidationSchemaByName(\n * 'user.profile.email',\n * vt.string()\n * .refine(\n * (value) => !reservedEmails.includes(value),\n * { message: 'This email is reserved' }\n * )\n * .nullish()\n * )\n * // => objectLoose({ user: objectLoose({ profile: objectLoose({ email: ... }).optional() }).optional() })\n * ```\n *\n * @example\n * ```tsx\n * // Array of objects with dynamic validation\n * clientValidationSchemaByName(\n * 'items.0.tag',\n * vt.string()\n * .refine((value) => !forbiddenTags.includes(value), { message: 'Tag not allowed' })\n * .nullish()\n * )\n * // => objectLoose({ items: array(objectLoose({ tag: ... })).optional() })\n * ```\n *\n * @example\n * ```tsx\n * // Flat array (array of primitives) with dynamic validation\n * clientValidationSchemaByName(\n * 'tags.0.__FLAT__',\n * vt.string()\n * .refine((value) => !forbiddenWords.includes(value), { message: 'Word not allowed' })\n * .nullish()\n * )\n * // => objectLoose({ tags: array(vt.string().refine(...).nullish()).optional() })\n * ```\n */\nexport const clientValidationSchemaByName = <T extends VetoTypeAny>(\n name: string,\n fieldSchema: T,\n) => {\n const segments = name.split('.');\n\n // Build schema from innermost to outermost\n let schema: VetoTypeAny = fieldSchema;\n\n // Process segments in reverse order to build nested structure\n for (let i = segments.length - 1; i >= 0; i -= 1) {\n const segment = segments[i];\n\n const isNumeric = /^\\d+$/.test(segment);\n const isFlatArrayKey = segment === flatArrayKey;\n\n if (isFlatArrayKey) {\n // Skip the __FLAT__ key - it's just a marker for flat arrays\n // The schema is already correct (primitive type), just continue\n // eslint-disable-next-line no-continue\n continue;\n }\n\n if (isNumeric) {\n // Current segment is an array index - wrap current schema in optional array\n schema = array(schema).optional();\n } else {\n // Current segment is a field name - wrap in objectLoose\n // Make it optional if it's an intermediate object (not the root)\n const obj = objectLoose({\n [segment]: schema,\n });\n schema = i > 0 ? obj.optional() : obj;\n }\n }\n\n return schema;\n};\n"],"mappings":";;;;;;;;;;;AAEA,SAAS,WAAW,aAAa;AAEjC,SAAS,OAAO,mBAAmB;AA8D5B,IAAM,sBAAsB,CACjC,MACA,eACA,YACS;AAtEX;AAuEE,QAAM;AAAA,IACJ,WAAW,EAAE,cAAc;AAAA,IAC3B,YAAY,EAAE,0BAA0B;AAAA,IACxC;AAAA,EACF,IAAI,eAAe;AAGnB,QAAM,mBAAmB,MAAM;AAC/B,QAAM,OAAM,wCAAS,QAAT,YAAgB;AAI5B,QAAM,WAAW,KAAK,UAAU,IAAI;AAGpC,YAAU,MAAM;AAEd,QAAI,QAAQ,MAAM;AAChB,YAAM,mBAAmB,cAAc,IAAI;AAC3C,gCAA0B,KAAK,gBAAgB;AAAA,IACjD,OAAO;AAEL,gCAA0B,KAAK,IAAI;AAAA,IACrC;AAGA,UAAM,oBAAoB,OAAO,KAAK,aAAa;AACnD,QAAI,kBAAkB,SAAS,GAAG;AAGhC,iBAAW,MAAY;AACrB,eAAO,QAAQ,iBAAiB;AAAA,MAClC,IAAG,CAAC;AAAA,IACN;AAGA,WAAO,MAAM;AACX,gCAA0B,KAAK,IAAI;AAAA,IACrC;AAAA,EAGF,GAAG,CAAC,UAAU,GAAG,CAAC;AACpB;AAmFO,IAAM,+BAA+B,CAC1C,MACA,gBACG;AACH,QAAM,WAAW,KAAK,MAAM,GAAG;AAG/B,MAAI,SAAsB;AAG1B,WAAS,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG;AAChD,UAAM,UAAU,SAAS,CAAC;AAE1B,UAAM,YAAY,QAAQ,KAAK,OAAO;AACtC,UAAM,iBAAiB,YAAY;AAEnC,QAAI,gBAAgB;AAIlB;AAAA,IACF;AAEA,QAAI,WAAW;AAEb,eAAS,MAAM,MAAM,EAAE,SAAS;AAAA,IAClC,OAAO;AAGL,YAAM,MAAM,YAAY;AAAA,QACtB,CAAC,OAAO,GAAG;AAAA,MACb,CAAC;AACD,eAAS,IAAI,IAAI,IAAI,SAAS,IAAI;AAAA,IACpC;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -22,7 +22,7 @@ require('../chunk-OE5BOGGX.cjs');
22
22
 
23
23
 
24
24
 
25
- var _chunkTEIXBRF5cjs = require('../chunk-TEIXBRF5.cjs');
25
+ var _chunkD5UUY4BDcjs = require('../chunk-D5UUY4BD.cjs');
26
26
 
27
27
 
28
28
 
@@ -40,5 +40,5 @@ require('../chunk-555JRYCS.cjs');
40
40
 
41
41
 
42
42
 
43
- exports.checkFieldIsRequired = _chunkNZBO4N3Scjs.checkFieldIsRequired; exports.clientValidationSchemaByName = _chunkTEIXBRF5cjs.clientValidationSchemaByName; exports.useClientValidation = _chunkTEIXBRF5cjs.useClientValidation; exports.useController = _chunkJMFDNTVCcjs.useController; exports.useFormContext = _chunkNZBO4N3Scjs.useFormContext; exports.useInput = _chunkPCTYJUY7cjs.useInput; exports.useInputValueDebounce = _chunkBZ5OMN4Ycjs.useInputValueDebounce; exports.useInputValueTransform = _chunk4WYC2RBEcjs.useInputValueTransform; exports.useUniformField = _chunkTGJJZEHBcjs.useUniformField; exports.useUniformFieldArray = _chunkAACO4OEKcjs.useUniformFieldArray;
43
+ exports.checkFieldIsRequired = _chunkNZBO4N3Scjs.checkFieldIsRequired; exports.clientValidationSchemaByName = _chunkD5UUY4BDcjs.clientValidationSchemaByName; exports.useClientValidation = _chunkD5UUY4BDcjs.useClientValidation; exports.useController = _chunkJMFDNTVCcjs.useController; exports.useFormContext = _chunkNZBO4N3Scjs.useFormContext; exports.useInput = _chunkPCTYJUY7cjs.useInput; exports.useInputValueDebounce = _chunkBZ5OMN4Ycjs.useInputValueDebounce; exports.useInputValueTransform = _chunk4WYC2RBEcjs.useInputValueTransform; exports.useUniformField = _chunkTGJJZEHBcjs.useUniformField; exports.useUniformFieldArray = _chunkAACO4OEKcjs.useUniformFieldArray;
44
44
  //# sourceMappingURL=index.cjs.map
@@ -6,7 +6,7 @@ export { useInput } from '@heroui/input';
6
6
  export { UseInputValueDebounceOptions, UseInputValueDebounceReturn, useInputValueDebounce } from './useInputValueDebounce/index.cjs';
7
7
  export { InputValueTransform, UseInputValueTransformOptions, UseInputValueTransformReturn, useInputValueTransform } from './useInputValueTransform/index.cjs';
8
8
  export { UseUniformFieldParams, UseUniformFieldReturn, useUniformField } from './useUniformField/index.cjs';
9
- import '@fuf-stack/veto/dist/types.d-CNPgNK-V';
9
+ import '@fuf-stack/veto/dist/types.d-Dzd6j9xO';
10
10
  import '@fuf-stack/veto';
11
11
  import 'react-hook-form';
12
12
  import 'react';
@@ -6,7 +6,7 @@ export { useInput } from '@heroui/input';
6
6
  export { UseInputValueDebounceOptions, UseInputValueDebounceReturn, useInputValueDebounce } from './useInputValueDebounce/index.js';
7
7
  export { InputValueTransform, UseInputValueTransformOptions, UseInputValueTransformReturn, useInputValueTransform } from './useInputValueTransform/index.js';
8
8
  export { UseUniformFieldParams, UseUniformFieldReturn, useUniformField } from './useUniformField/index.js';
9
- import '@fuf-stack/veto/dist/types.d-CNPgNK-V';
9
+ import '@fuf-stack/veto/dist/types.d-Dzd6j9xO';
10
10
  import '@fuf-stack/veto';
11
11
  import 'react-hook-form';
12
12
  import 'react';
@@ -22,7 +22,7 @@ import "../chunk-NTDKZW4E.js";
22
22
  import {
23
23
  clientValidationSchemaByName,
24
24
  useClientValidation
25
- } from "../chunk-CHNYQVWO.js";
25
+ } from "../chunk-L5TXRTPB.js";
26
26
  import {
27
27
  checkFieldIsRequired,
28
28
  useFormContext
@@ -1,12 +1,12 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
 
4
- var _chunkTEIXBRF5cjs = require('../../chunk-TEIXBRF5.cjs');
4
+ var _chunkD5UUY4BDcjs = require('../../chunk-D5UUY4BD.cjs');
5
5
  require('../../chunk-NZBO4N3S.cjs');
6
6
  require('../../chunk-Z353BLWI.cjs');
7
7
  require('../../chunk-555JRYCS.cjs');
8
8
 
9
9
 
10
10
 
11
- exports.clientValidationSchemaByName = _chunkTEIXBRF5cjs.clientValidationSchemaByName; exports.useClientValidation = _chunkTEIXBRF5cjs.useClientValidation;
11
+ exports.clientValidationSchemaByName = _chunkD5UUY4BDcjs.clientValidationSchemaByName; exports.useClientValidation = _chunkD5UUY4BDcjs.useClientValidation;
12
12
  //# sourceMappingURL=index.cjs.map
@@ -1,78 +1,147 @@
1
- import * as _fuf_stack_veto_dist_types_d_CNPgNK_V from '@fuf-stack/veto/dist/types.d-CNPgNK-V';
1
+ import * as _fuf_stack_veto_dist_types_d_Dzd6j9xO from '@fuf-stack/veto/dist/types.d-Dzd6j9xO';
2
2
  import { VetoTypeAny } from '@fuf-stack/veto';
3
3
 
4
4
  /**
5
5
  * Hook for adding dynamic client-side validation to forms.
6
6
  *
7
- * Automatically manages validation schema lifecycle: sets schema when data is provided,
8
- * clears schema when data is null/undefined, and cleans up on unmount.
7
+ * Client validation is used for context-dependent validation rules that require external data
8
+ * (e.g., checking if a username already exists). It complements base validation (passed to Form),
9
+ * which handles static rules like "required", min/max length, format, etc.
10
+ *
11
+ * **Typical pattern:**
12
+ * - Base validation: Static, always-on rules (required, format, length constraints)
13
+ * - Client validation: Dynamic rules based on external data (uniqueness, business logic)
14
+ * - Client schemas should use `.nullish()` to allow empty values (base validation handles "required")
9
15
  *
10
- * When the client validation schema changes, automatically re-validates all touched
11
- * fields to ensure they are validated against the new schema.
16
+ * Automatically manages validation schema lifecycle: sets schema when data is provided,
17
+ * clears schema when data is null/undefined, and cleans up on unmount. When the client
18
+ * validation schema changes, automatically re-validates all touched fields.
12
19
  *
13
20
  * @param data - Data to create validation schema from (or null/undefined to clear validation)
14
- * @param schemaFactory - Function that creates a validation schema from the data.
21
+ * @param schemaFactory - Function that creates a validation schema from the data. Schemas should typically use `.nullish()`.
22
+ * @param options - Optional configuration object
23
+ * @param options.key - Optional custom key for registration. Useful when multiple instances should share validation (e.g., in field arrays)
15
24
  *
16
25
  * @example
17
26
  * ```tsx
18
- * const { data: teamData } = useTeamQuery(teamId);
27
+ * // Base validation handles "required" and format
28
+ * const baseSchema = veto({ username: vt.string({ min: 3 }) });
19
29
  *
30
+ * // Client validation adds dynamic uniqueness check
31
+ * const { data: teamData } = useTeamQuery(teamId);
20
32
  * useClientValidation(teamData, (data) =>
21
- * vt.object({
22
- * username: vt.string().refine(
23
- * (value) => !data.existingUsers.includes(value),
24
- * { message: 'Username already exists' }
25
- * )
33
+ * vt.objectLoose({
34
+ * username: vt.string()
35
+ * .refine(
36
+ * (value) => !data.existingUsers.includes(value),
37
+ * { message: 'Username already exists' }
38
+ * )
39
+ * .nullish() // Allow empty - base validation handles "required"
26
40
  * })
27
41
  * );
28
42
  * ```
43
+ *
44
+ * @example
45
+ * ```tsx
46
+ * // Using custom key in a field array to avoid multiple registrations
47
+ * fields.map((field, index) => (
48
+ * useClientValidation(
49
+ * userData,
50
+ * (data) => clientValidationSchemaByName(
51
+ * `items.${index}.name`,
52
+ * vt.string()
53
+ * .refine((val) => !data.forbidden.includes(val), { message: 'Name not allowed' })
54
+ * .nullish()
55
+ * ),
56
+ * { key: 'items-validation' } // All instances share the same key
57
+ * )
58
+ * ));
59
+ * ```
29
60
  */
30
- declare const useClientValidation: <TData = unknown>(data: TData | null | undefined, schemaFactory: (data: TData) => VetoTypeAny) => void;
61
+ declare const useClientValidation: <TData = unknown>(data: TData | null | undefined, schemaFactory: (data: TData) => VetoTypeAny, options?: {
62
+ key?: string;
63
+ }) => void;
31
64
  /**
32
65
  * Helper function to create a veto looseObject schema for a single field with nested path support.
33
66
  *
34
67
  * Parses field paths (dot-notation) and creates properly nested loose object schemas with array support.
35
68
  * All intermediate objects and arrays are marked as optional to allow partial data structures.
36
69
  * Numeric segments in the path are treated as array indices, and the parent field is wrapped in an array schema.
70
+ * Supports flat arrays (arrays of primitives) via the special `__FLAT__` key.
71
+ *
72
+ * **Important:** The fieldSchema should typically use `.nullish()` to allow empty values. This is because:
73
+ * - Base validation (passed to Form) handles "required" constraints
74
+ * - Client validation adds dynamic, context-dependent rules (e.g., "username already exists")
75
+ * - Form data processing converts empty strings in flat arrays to `null` (not `undefined`)
76
+ * - `.nullish()` accepts both `null` and `undefined`, while `.optional()` only accepts `undefined`
37
77
  *
38
- * @param name - The dot-separated path to the field (e.g., 'username', 'user.profile.email', 'items.0.name')
39
- * @param fieldSchema - The veto validation schema for the field
78
+ * This pattern ensures client validation only validates non-empty values, while base validation
79
+ * enforces required fields and basic constraints (min length, format, etc.).
80
+ *
81
+ * @param name - The dot-separated path to the field (e.g., 'username', 'user.profile.email', 'items.0.name', 'tags.0.__FLAT__')
82
+ * @param fieldSchema - The veto validation schema for the field. Should typically use `.nullish()` to allow empty values.
40
83
  * @returns A nested loose object schema matching the field path structure with proper type inference
41
84
  *
42
85
  * @example
43
86
  * ```tsx
44
- * // Simple field
45
- * clientValidationSchemaByName('username', vt.string())
46
- * // => objectLoose({ username: vt.string() })
87
+ * // Typical usage: base validation handles "required", client validation adds dynamic rules
88
+ * // Base validation (in Form component)
89
+ * const baseSchema = veto({ username: vt.string({ min: 3 }) }); // Required + min length
90
+ *
91
+ * // Client validation (dynamic check for existing usernames)
92
+ * const { data: userData } = useUserQuery(userId);
93
+ * useClientValidation(userData, (data) =>
94
+ * clientValidationSchemaByName(
95
+ * 'username',
96
+ * vt.string()
97
+ * .refine(
98
+ * (value) => !data.existingUsernames.includes(value),
99
+ * { message: 'Username already taken' }
100
+ * )
101
+ * .nullish() // Allow empty - base validation handles "required"
102
+ * )
103
+ * );
47
104
  * ```
48
105
  *
49
106
  * @example
50
107
  * ```tsx
51
- * // Nested field (with optional intermediate objects)
52
- * clientValidationSchemaByName('user.profile.email', vt.string().email())
53
- * // => objectLoose({ user: objectLoose({ profile: objectLoose({ email: vt.string().email() }).optional() }).optional() })
108
+ * // Nested field path with dynamic validation
109
+ * clientValidationSchemaByName(
110
+ * 'user.profile.email',
111
+ * vt.string()
112
+ * .refine(
113
+ * (value) => !reservedEmails.includes(value),
114
+ * { message: 'This email is reserved' }
115
+ * )
116
+ * .nullish()
117
+ * )
118
+ * // => objectLoose({ user: objectLoose({ profile: objectLoose({ email: ... }).optional() }).optional() })
54
119
  * ```
55
120
  *
56
121
  * @example
57
122
  * ```tsx
58
- * // Array field (with optional array)
59
- * clientValidationSchemaByName('items.0.name', vt.string())
60
- * // => objectLoose({ items: array(objectLoose({ name: vt.string() })).optional() })
123
+ * // Array of objects with dynamic validation
124
+ * clientValidationSchemaByName(
125
+ * 'items.0.tag',
126
+ * vt.string()
127
+ * .refine((value) => !forbiddenTags.includes(value), { message: 'Tag not allowed' })
128
+ * .nullish()
129
+ * )
130
+ * // => objectLoose({ items: array(objectLoose({ tag: ... })).optional() })
61
131
  * ```
62
132
  *
63
133
  * @example
64
134
  * ```tsx
65
- * // Usage with useClientValidation
66
- * const { data: userData } = useUserQuery(userId);
67
- *
68
- * useClientValidation(userData, (data) =>
69
- * clientValidationSchemaByName('username', vt.string().refine(
70
- * (value) => !data.existingUsernames.includes(value),
71
- * { message: 'Username already taken' }
72
- * ))
73
- * );
135
+ * // Flat array (array of primitives) with dynamic validation
136
+ * clientValidationSchemaByName(
137
+ * 'tags.0.__FLAT__',
138
+ * vt.string()
139
+ * .refine((value) => !forbiddenWords.includes(value), { message: 'Word not allowed' })
140
+ * .nullish()
141
+ * )
142
+ * // => objectLoose({ tags: array(vt.string().refine(...).nullish()).optional() })
74
143
  * ```
75
144
  */
76
- declare const clientValidationSchemaByName: <T extends VetoTypeAny>(name: string, fieldSchema: T) => _fuf_stack_veto_dist_types_d_CNPgNK_V.Z;
145
+ declare const clientValidationSchemaByName: <T extends VetoTypeAny>(name: string, fieldSchema: T) => _fuf_stack_veto_dist_types_d_Dzd6j9xO.Z;
77
146
 
78
147
  export { clientValidationSchemaByName, useClientValidation };
@@ -1,78 +1,147 @@
1
- import * as _fuf_stack_veto_dist_types_d_CNPgNK_V from '@fuf-stack/veto/dist/types.d-CNPgNK-V';
1
+ import * as _fuf_stack_veto_dist_types_d_Dzd6j9xO from '@fuf-stack/veto/dist/types.d-Dzd6j9xO';
2
2
  import { VetoTypeAny } from '@fuf-stack/veto';
3
3
 
4
4
  /**
5
5
  * Hook for adding dynamic client-side validation to forms.
6
6
  *
7
- * Automatically manages validation schema lifecycle: sets schema when data is provided,
8
- * clears schema when data is null/undefined, and cleans up on unmount.
7
+ * Client validation is used for context-dependent validation rules that require external data
8
+ * (e.g., checking if a username already exists). It complements base validation (passed to Form),
9
+ * which handles static rules like "required", min/max length, format, etc.
10
+ *
11
+ * **Typical pattern:**
12
+ * - Base validation: Static, always-on rules (required, format, length constraints)
13
+ * - Client validation: Dynamic rules based on external data (uniqueness, business logic)
14
+ * - Client schemas should use `.nullish()` to allow empty values (base validation handles "required")
9
15
  *
10
- * When the client validation schema changes, automatically re-validates all touched
11
- * fields to ensure they are validated against the new schema.
16
+ * Automatically manages validation schema lifecycle: sets schema when data is provided,
17
+ * clears schema when data is null/undefined, and cleans up on unmount. When the client
18
+ * validation schema changes, automatically re-validates all touched fields.
12
19
  *
13
20
  * @param data - Data to create validation schema from (or null/undefined to clear validation)
14
- * @param schemaFactory - Function that creates a validation schema from the data.
21
+ * @param schemaFactory - Function that creates a validation schema from the data. Schemas should typically use `.nullish()`.
22
+ * @param options - Optional configuration object
23
+ * @param options.key - Optional custom key for registration. Useful when multiple instances should share validation (e.g., in field arrays)
15
24
  *
16
25
  * @example
17
26
  * ```tsx
18
- * const { data: teamData } = useTeamQuery(teamId);
27
+ * // Base validation handles "required" and format
28
+ * const baseSchema = veto({ username: vt.string({ min: 3 }) });
19
29
  *
30
+ * // Client validation adds dynamic uniqueness check
31
+ * const { data: teamData } = useTeamQuery(teamId);
20
32
  * useClientValidation(teamData, (data) =>
21
- * vt.object({
22
- * username: vt.string().refine(
23
- * (value) => !data.existingUsers.includes(value),
24
- * { message: 'Username already exists' }
25
- * )
33
+ * vt.objectLoose({
34
+ * username: vt.string()
35
+ * .refine(
36
+ * (value) => !data.existingUsers.includes(value),
37
+ * { message: 'Username already exists' }
38
+ * )
39
+ * .nullish() // Allow empty - base validation handles "required"
26
40
  * })
27
41
  * );
28
42
  * ```
43
+ *
44
+ * @example
45
+ * ```tsx
46
+ * // Using custom key in a field array to avoid multiple registrations
47
+ * fields.map((field, index) => (
48
+ * useClientValidation(
49
+ * userData,
50
+ * (data) => clientValidationSchemaByName(
51
+ * `items.${index}.name`,
52
+ * vt.string()
53
+ * .refine((val) => !data.forbidden.includes(val), { message: 'Name not allowed' })
54
+ * .nullish()
55
+ * ),
56
+ * { key: 'items-validation' } // All instances share the same key
57
+ * )
58
+ * ));
59
+ * ```
29
60
  */
30
- declare const useClientValidation: <TData = unknown>(data: TData | null | undefined, schemaFactory: (data: TData) => VetoTypeAny) => void;
61
+ declare const useClientValidation: <TData = unknown>(data: TData | null | undefined, schemaFactory: (data: TData) => VetoTypeAny, options?: {
62
+ key?: string;
63
+ }) => void;
31
64
  /**
32
65
  * Helper function to create a veto looseObject schema for a single field with nested path support.
33
66
  *
34
67
  * Parses field paths (dot-notation) and creates properly nested loose object schemas with array support.
35
68
  * All intermediate objects and arrays are marked as optional to allow partial data structures.
36
69
  * Numeric segments in the path are treated as array indices, and the parent field is wrapped in an array schema.
70
+ * Supports flat arrays (arrays of primitives) via the special `__FLAT__` key.
71
+ *
72
+ * **Important:** The fieldSchema should typically use `.nullish()` to allow empty values. This is because:
73
+ * - Base validation (passed to Form) handles "required" constraints
74
+ * - Client validation adds dynamic, context-dependent rules (e.g., "username already exists")
75
+ * - Form data processing converts empty strings in flat arrays to `null` (not `undefined`)
76
+ * - `.nullish()` accepts both `null` and `undefined`, while `.optional()` only accepts `undefined`
37
77
  *
38
- * @param name - The dot-separated path to the field (e.g., 'username', 'user.profile.email', 'items.0.name')
39
- * @param fieldSchema - The veto validation schema for the field
78
+ * This pattern ensures client validation only validates non-empty values, while base validation
79
+ * enforces required fields and basic constraints (min length, format, etc.).
80
+ *
81
+ * @param name - The dot-separated path to the field (e.g., 'username', 'user.profile.email', 'items.0.name', 'tags.0.__FLAT__')
82
+ * @param fieldSchema - The veto validation schema for the field. Should typically use `.nullish()` to allow empty values.
40
83
  * @returns A nested loose object schema matching the field path structure with proper type inference
41
84
  *
42
85
  * @example
43
86
  * ```tsx
44
- * // Simple field
45
- * clientValidationSchemaByName('username', vt.string())
46
- * // => objectLoose({ username: vt.string() })
87
+ * // Typical usage: base validation handles "required", client validation adds dynamic rules
88
+ * // Base validation (in Form component)
89
+ * const baseSchema = veto({ username: vt.string({ min: 3 }) }); // Required + min length
90
+ *
91
+ * // Client validation (dynamic check for existing usernames)
92
+ * const { data: userData } = useUserQuery(userId);
93
+ * useClientValidation(userData, (data) =>
94
+ * clientValidationSchemaByName(
95
+ * 'username',
96
+ * vt.string()
97
+ * .refine(
98
+ * (value) => !data.existingUsernames.includes(value),
99
+ * { message: 'Username already taken' }
100
+ * )
101
+ * .nullish() // Allow empty - base validation handles "required"
102
+ * )
103
+ * );
47
104
  * ```
48
105
  *
49
106
  * @example
50
107
  * ```tsx
51
- * // Nested field (with optional intermediate objects)
52
- * clientValidationSchemaByName('user.profile.email', vt.string().email())
53
- * // => objectLoose({ user: objectLoose({ profile: objectLoose({ email: vt.string().email() }).optional() }).optional() })
108
+ * // Nested field path with dynamic validation
109
+ * clientValidationSchemaByName(
110
+ * 'user.profile.email',
111
+ * vt.string()
112
+ * .refine(
113
+ * (value) => !reservedEmails.includes(value),
114
+ * { message: 'This email is reserved' }
115
+ * )
116
+ * .nullish()
117
+ * )
118
+ * // => objectLoose({ user: objectLoose({ profile: objectLoose({ email: ... }).optional() }).optional() })
54
119
  * ```
55
120
  *
56
121
  * @example
57
122
  * ```tsx
58
- * // Array field (with optional array)
59
- * clientValidationSchemaByName('items.0.name', vt.string())
60
- * // => objectLoose({ items: array(objectLoose({ name: vt.string() })).optional() })
123
+ * // Array of objects with dynamic validation
124
+ * clientValidationSchemaByName(
125
+ * 'items.0.tag',
126
+ * vt.string()
127
+ * .refine((value) => !forbiddenTags.includes(value), { message: 'Tag not allowed' })
128
+ * .nullish()
129
+ * )
130
+ * // => objectLoose({ items: array(objectLoose({ tag: ... })).optional() })
61
131
  * ```
62
132
  *
63
133
  * @example
64
134
  * ```tsx
65
- * // Usage with useClientValidation
66
- * const { data: userData } = useUserQuery(userId);
67
- *
68
- * useClientValidation(userData, (data) =>
69
- * clientValidationSchemaByName('username', vt.string().refine(
70
- * (value) => !data.existingUsernames.includes(value),
71
- * { message: 'Username already taken' }
72
- * ))
73
- * );
135
+ * // Flat array (array of primitives) with dynamic validation
136
+ * clientValidationSchemaByName(
137
+ * 'tags.0.__FLAT__',
138
+ * vt.string()
139
+ * .refine((value) => !forbiddenWords.includes(value), { message: 'Word not allowed' })
140
+ * .nullish()
141
+ * )
142
+ * // => objectLoose({ tags: array(vt.string().refine(...).nullish()).optional() })
74
143
  * ```
75
144
  */
76
- declare const clientValidationSchemaByName: <T extends VetoTypeAny>(name: string, fieldSchema: T) => _fuf_stack_veto_dist_types_d_CNPgNK_V.Z;
145
+ declare const clientValidationSchemaByName: <T extends VetoTypeAny>(name: string, fieldSchema: T) => _fuf_stack_veto_dist_types_d_Dzd6j9xO.Z;
77
146
 
78
147
  export { clientValidationSchemaByName, useClientValidation };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  clientValidationSchemaByName,
3
3
  useClientValidation
4
- } from "../../chunk-CHNYQVWO.js";
4
+ } from "../../chunk-L5TXRTPB.js";
5
5
  import "../../chunk-PO5UQAU6.js";
6
6
  import "../../chunk-76KOVUDN.js";
7
7
  import "../../chunk-K2V4ULA2.js";
package/dist/index.cjs CHANGED
@@ -62,7 +62,7 @@ var _chunkOE5BOGGXcjs = require('./chunk-OE5BOGGX.cjs');
62
62
 
63
63
 
64
64
 
65
- var _chunkTEIXBRF5cjs = require('./chunk-TEIXBRF5.cjs');
65
+ var _chunkD5UUY4BDcjs = require('./chunk-D5UUY4BD.cjs');
66
66
 
67
67
 
68
68
 
@@ -105,5 +105,5 @@ require('./chunk-555JRYCS.cjs');
105
105
 
106
106
 
107
107
 
108
- exports.Checkboxes = _chunkRMCPVDACcjs.Checkboxes_default; exports.FieldArray = _chunkIYULWK4Hcjs.FieldArray_default; exports.FieldCopyTestIdButton = _chunkOE5BOGGXcjs.FieldCopyTestIdButton_default; exports.FieldValidationError = _chunkNHEZXA4Hcjs.FieldValidationError_default; exports.Form = _chunkWFO4XPA3cjs.Form_default; exports.Grid = _chunkTTD3KL6Ecjs.Grid_default; exports.Input = _chunkF6JQYB53cjs.Input_default; exports.RadioBoxes = _chunkVPUMTPD3cjs.RadioBoxes_default; exports.RadioTabs = _chunk345LYO4Hcjs.RadioTabs_default; exports.Radios = _chunkE4DCVJS5cjs.Radios_default; exports.Select = _chunkA5Z75X7Mcjs.Select_default; exports.SubmitButton = _chunkOJHD2XHDcjs.SubmitButton_default; exports.Switch = _chunkXJBGXFVXcjs.Switch_default; exports.TextArea = _chunkNZIAIYPScjs.TextArea_default; exports.checkFieldIsRequired = _chunkNZBO4N3Scjs.checkFieldIsRequired; exports.clientValidationSchemaByName = _chunkTEIXBRF5cjs.clientValidationSchemaByName; exports.flatArrayKey = _chunkZ353BLWIcjs.flatArrayKey; exports.fromNullishString = _chunkZ353BLWIcjs.fromNullishString; exports.toFormFormat = _chunkZ353BLWIcjs.toFormFormat; exports.toNullishString = _chunkZ353BLWIcjs.toNullishString; exports.toValidationFormat = _chunkZ353BLWIcjs.toValidationFormat; exports.useClientValidation = _chunkTEIXBRF5cjs.useClientValidation; exports.useController = _chunkJMFDNTVCcjs.useController; exports.useFormContext = _chunkNZBO4N3Scjs.useFormContext; exports.useInput = _chunkPCTYJUY7cjs.useInput; exports.useInputValueDebounce = _chunkBZ5OMN4Ycjs.useInputValueDebounce; exports.useInputValueTransform = _chunk4WYC2RBEcjs.useInputValueTransform; exports.useUniformField = _chunkTGJJZEHBcjs.useUniformField; exports.useUniformFieldArray = _chunkAACO4OEKcjs.useUniformFieldArray;
108
+ exports.Checkboxes = _chunkRMCPVDACcjs.Checkboxes_default; exports.FieldArray = _chunkIYULWK4Hcjs.FieldArray_default; exports.FieldCopyTestIdButton = _chunkOE5BOGGXcjs.FieldCopyTestIdButton_default; exports.FieldValidationError = _chunkNHEZXA4Hcjs.FieldValidationError_default; exports.Form = _chunkWFO4XPA3cjs.Form_default; exports.Grid = _chunkTTD3KL6Ecjs.Grid_default; exports.Input = _chunkF6JQYB53cjs.Input_default; exports.RadioBoxes = _chunkVPUMTPD3cjs.RadioBoxes_default; exports.RadioTabs = _chunk345LYO4Hcjs.RadioTabs_default; exports.Radios = _chunkE4DCVJS5cjs.Radios_default; exports.Select = _chunkA5Z75X7Mcjs.Select_default; exports.SubmitButton = _chunkOJHD2XHDcjs.SubmitButton_default; exports.Switch = _chunkXJBGXFVXcjs.Switch_default; exports.TextArea = _chunkNZIAIYPScjs.TextArea_default; exports.checkFieldIsRequired = _chunkNZBO4N3Scjs.checkFieldIsRequired; exports.clientValidationSchemaByName = _chunkD5UUY4BDcjs.clientValidationSchemaByName; exports.flatArrayKey = _chunkZ353BLWIcjs.flatArrayKey; exports.fromNullishString = _chunkZ353BLWIcjs.fromNullishString; exports.toFormFormat = _chunkZ353BLWIcjs.toFormFormat; exports.toNullishString = _chunkZ353BLWIcjs.toNullishString; exports.toValidationFormat = _chunkZ353BLWIcjs.toValidationFormat; exports.useClientValidation = _chunkD5UUY4BDcjs.useClientValidation; exports.useController = _chunkJMFDNTVCcjs.useController; exports.useFormContext = _chunkNZBO4N3Scjs.useFormContext; exports.useInput = _chunkPCTYJUY7cjs.useInput; exports.useInputValueDebounce = _chunkBZ5OMN4Ycjs.useInputValueDebounce; exports.useInputValueTransform = _chunk4WYC2RBEcjs.useInputValueTransform; exports.useUniformField = _chunkTGJJZEHBcjs.useUniformField; exports.useUniformFieldArray = _chunkAACO4OEKcjs.useUniformFieldArray;
109
109
  //# sourceMappingURL=index.cjs.map
package/dist/index.d.cts CHANGED
@@ -28,7 +28,7 @@ import 'react';
28
28
  import '@fuf-stack/veto';
29
29
  import 'react-hook-form';
30
30
  import './FormContext-LRho0tno.cjs';
31
- import '@fuf-stack/veto/dist/types.d-CNPgNK-V';
31
+ import '@fuf-stack/veto/dist/types.d-Dzd6j9xO';
32
32
  import '@fuf-stack/pixels';
33
33
  import 'react-select';
34
34
  import '@heroui/switch';
package/dist/index.d.ts CHANGED
@@ -28,7 +28,7 @@ import 'react';
28
28
  import '@fuf-stack/veto';
29
29
  import 'react-hook-form';
30
30
  import './FormContext-LRho0tno.js';
31
- import '@fuf-stack/veto/dist/types.d-CNPgNK-V';
31
+ import '@fuf-stack/veto/dist/types.d-Dzd6j9xO';
32
32
  import '@fuf-stack/pixels';
33
33
  import 'react-select';
34
34
  import '@heroui/switch';
package/dist/index.js CHANGED
@@ -62,7 +62,7 @@ import {
62
62
  import {
63
63
  clientValidationSchemaByName,
64
64
  useClientValidation
65
- } from "./chunk-CHNYQVWO.js";
65
+ } from "./chunk-L5TXRTPB.js";
66
66
  import {
67
67
  checkFieldIsRequired,
68
68
  useFormContext
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fuf-stack/uniform",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "description": "fuf react form library",
5
5
  "author": "Fröhlich ∧ Frei",
6
6
  "homepage": "https://github.com/fuf-stack/pixels#readme",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/useClientValidation/useClientValidation.ts"],"sourcesContent":["import type { VetoTypeAny } from '@fuf-stack/veto';\n\nimport { useEffect, useId } from 'react';\n\nimport { array, objectLoose } from '@fuf-stack/veto';\n\nimport { useFormContext } from '../useFormContext/useFormContext';\n\n/**\n * Hook for adding dynamic client-side validation to forms.\n *\n * Automatically manages validation schema lifecycle: sets schema when data is provided,\n * clears schema when data is null/undefined, and cleans up on unmount.\n *\n * When the client validation schema changes, automatically re-validates all touched\n * fields to ensure they are validated against the new schema.\n *\n * @param data - Data to create validation schema from (or null/undefined to clear validation)\n * @param schemaFactory - Function that creates a validation schema from the data.\n *\n * @example\n * ```tsx\n * const { data: teamData } = useTeamQuery(teamId);\n *\n * useClientValidation(teamData, (data) =>\n * vt.object({\n * username: vt.string().refine(\n * (value) => !data.existingUsers.includes(value),\n * { message: 'Username already exists' }\n * )\n * })\n * );\n * ```\n */\nexport const useClientValidation = <TData = unknown>(\n data: TData | null | undefined,\n schemaFactory: (data: TData) => VetoTypeAny,\n): void => {\n const {\n formState: { touchedFields },\n validation: { setClientValidationSchema },\n trigger,\n } = useFormContext();\n\n // Auto-generate unique key\n const key = useId();\n\n // Use data hash instead of object reference to avoid unnecessary re-runs\n // when data object reference changes but data remains the same\n const dataHash = JSON.stringify(data);\n\n // Single effect to manage validation schema lifecycle\n useEffect(() => {\n // Set validation schema when data is available\n if (data != null) {\n const validationSchema = schemaFactory(data);\n setClientValidationSchema(key, validationSchema);\n } else {\n // Clear validation schema when no data\n setClientValidationSchema(key, null);\n }\n\n // Re-validate all touched fields when client validation schema changes\n const touchedFieldNames = Object.keys(touchedFields);\n if (touchedFieldNames.length > 0) {\n // Use setTimeout to ensure the client validation schema update has propagated\n // before triggering re-validation (fixes race condition)\n setTimeout(async () => {\n return trigger(touchedFieldNames);\n }, 1);\n }\n\n // Cleanup on unmount or when dependencies change\n return () => {\n setClientValidationSchema(key, null);\n };\n // Only re-run when data (dataHash) or key changes\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [dataHash, key]);\n};\n\n/**\n * Helper function to create a veto looseObject schema for a single field with nested path support.\n *\n * Parses field paths (dot-notation) and creates properly nested loose object schemas with array support.\n * All intermediate objects and arrays are marked as optional to allow partial data structures.\n * Numeric segments in the path are treated as array indices, and the parent field is wrapped in an array schema.\n *\n * @param name - The dot-separated path to the field (e.g., 'username', 'user.profile.email', 'items.0.name')\n * @param fieldSchema - The veto validation schema for the field\n * @returns A nested loose object schema matching the field path structure with proper type inference\n *\n * @example\n * ```tsx\n * // Simple field\n * clientValidationSchemaByName('username', vt.string())\n * // => objectLoose({ username: vt.string() })\n * ```\n *\n * @example\n * ```tsx\n * // Nested field (with optional intermediate objects)\n * clientValidationSchemaByName('user.profile.email', vt.string().email())\n * // => objectLoose({ user: objectLoose({ profile: objectLoose({ email: vt.string().email() }).optional() }).optional() })\n * ```\n *\n * @example\n * ```tsx\n * // Array field (with optional array)\n * clientValidationSchemaByName('items.0.name', vt.string())\n * // => objectLoose({ items: array(objectLoose({ name: vt.string() })).optional() })\n * ```\n *\n * @example\n * ```tsx\n * // Usage with useClientValidation\n * const { data: userData } = useUserQuery(userId);\n *\n * useClientValidation(userData, (data) =>\n * clientValidationSchemaByName('username', vt.string().refine(\n * (value) => !data.existingUsernames.includes(value),\n * { message: 'Username already taken' }\n * ))\n * );\n * ```\n */\nexport const clientValidationSchemaByName = <T extends VetoTypeAny>(\n name: string,\n fieldSchema: T,\n) => {\n const segments = name.split('.');\n\n // Build schema from innermost to outermost\n let schema: VetoTypeAny = fieldSchema;\n\n // Process segments in reverse order to build nested structure\n for (let i = segments.length - 1; i >= 0; i -= 1) {\n const segment = segments[i];\n const isNumeric = /^\\d+$/.test(segment);\n\n if (isNumeric) {\n // Current segment is an array index - wrap current schema in optional array\n schema = array(schema).optional();\n } else {\n // Current segment is a field name - wrap in objectLoose\n // Make it optional if it's an intermediate object (not the root)\n const obj = objectLoose({\n [segment]: schema,\n });\n schema = i > 0 ? obj.optional() : obj;\n }\n }\n\n return schema;\n};\n"],"mappings":";;;;;;;;AAEA,SAAS,WAAW,aAAa;AAEjC,SAAS,OAAO,mBAAmB;AA8B5B,IAAM,sBAAsB,CACjC,MACA,kBACS;AACT,QAAM;AAAA,IACJ,WAAW,EAAE,cAAc;AAAA,IAC3B,YAAY,EAAE,0BAA0B;AAAA,IACxC;AAAA,EACF,IAAI,eAAe;AAGnB,QAAM,MAAM,MAAM;AAIlB,QAAM,WAAW,KAAK,UAAU,IAAI;AAGpC,YAAU,MAAM;AAEd,QAAI,QAAQ,MAAM;AAChB,YAAM,mBAAmB,cAAc,IAAI;AAC3C,gCAA0B,KAAK,gBAAgB;AAAA,IACjD,OAAO;AAEL,gCAA0B,KAAK,IAAI;AAAA,IACrC;AAGA,UAAM,oBAAoB,OAAO,KAAK,aAAa;AACnD,QAAI,kBAAkB,SAAS,GAAG;AAGhC,iBAAW,MAAY;AACrB,eAAO,QAAQ,iBAAiB;AAAA,MAClC,IAAG,CAAC;AAAA,IACN;AAGA,WAAO,MAAM;AACX,gCAA0B,KAAK,IAAI;AAAA,IACrC;AAAA,EAGF,GAAG,CAAC,UAAU,GAAG,CAAC;AACpB;AA+CO,IAAM,+BAA+B,CAC1C,MACA,gBACG;AACH,QAAM,WAAW,KAAK,MAAM,GAAG;AAG/B,MAAI,SAAsB;AAG1B,WAAS,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG;AAChD,UAAM,UAAU,SAAS,CAAC;AAC1B,UAAM,YAAY,QAAQ,KAAK,OAAO;AAEtC,QAAI,WAAW;AAEb,eAAS,MAAM,MAAM,EAAE,SAAS;AAAA,IAClC,OAAO;AAGL,YAAM,MAAM,YAAY;AAAA,QACtB,CAAC,OAAO,GAAG;AAAA,MACb,CAAC;AACD,eAAS,IAAI,IAAI,IAAI,SAAS,IAAI;AAAA,IACpC;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["/home/runner/work/pixels/pixels/packages/uniform/dist/chunk-TEIXBRF5.cjs","../src/hooks/useClientValidation/useClientValidation.ts"],"names":[],"mappings":"AAAA;AACE;AACF,wDAA6B;AAC7B;AACE;AACF,wDAA6B;AAC7B;AACA;ACLA,8BAAiC;AAEjC,uCAAmC;AA8B5B,IAAM,oBAAA,EAAsB,CACjC,IAAA,EACA,aAAA,EAAA,GACS;AACT,EAAA,MAAM;AAAA,IACJ,SAAA,EAAW,EAAE,cAAc,CAAA;AAAA,IAC3B,UAAA,EAAY,EAAE,0BAA0B,CAAA;AAAA,IACxC;AAAA,EACF,EAAA,EAAI,8CAAA,CAAe;AAGnB,EAAA,MAAM,IAAA,EAAM,0BAAA,CAAM;AAIlB,EAAA,MAAM,SAAA,EAAW,IAAA,CAAK,SAAA,CAAU,IAAI,CAAA;AAGpC,EAAA,8BAAA,CAAU,EAAA,GAAM;AAEd,IAAA,GAAA,CAAI,KAAA,GAAQ,IAAA,EAAM;AAChB,MAAA,MAAM,iBAAA,EAAmB,aAAA,CAAc,IAAI,CAAA;AAC3C,MAAA,yBAAA,CAA0B,GAAA,EAAK,gBAAgB,CAAA;AAAA,IACjD,EAAA,KAAO;AAEL,MAAA,yBAAA,CAA0B,GAAA,EAAK,IAAI,CAAA;AAAA,IACrC;AAGA,IAAA,MAAM,kBAAA,EAAoB,MAAA,CAAO,IAAA,CAAK,aAAa,CAAA;AACnD,IAAA,GAAA,CAAI,iBAAA,CAAkB,OAAA,EAAS,CAAA,EAAG;AAGhC,MAAA,UAAA,CAAW,CAAA,EAAA,GAAY,uCAAA,IAAA,EAAA,IAAA,EAAA,QAAA,EAAA,CAAA,EAAA;AACrB,QAAA,OAAO,OAAA,CAAQ,iBAAiB,CAAA;AAAA,MAClC,CAAA,CAAA,EAAG,CAAC,CAAA;AAAA,IACN;AAGA,IAAA,OAAO,CAAA,EAAA,GAAM;AACX,MAAA,yBAAA,CAA0B,GAAA,EAAK,IAAI,CAAA;AAAA,IACrC,CAAA;AAAA,EAGF,CAAA,EAAG,CAAC,QAAA,EAAU,GAAG,CAAC,CAAA;AACpB,CAAA;AA+CO,IAAM,6BAAA,EAA+B,CAC1C,IAAA,EACA,WAAA,EAAA,GACG;AACH,EAAA,MAAM,SAAA,EAAW,IAAA,CAAK,KAAA,CAAM,GAAG,CAAA;AAG/B,EAAA,IAAI,OAAA,EAAsB,WAAA;AAG1B,EAAA,IAAA,CAAA,IAAS,EAAA,EAAI,QAAA,CAAS,OAAA,EAAS,CAAA,EAAG,EAAA,GAAK,CAAA,EAAG,EAAA,GAAK,CAAA,EAAG;AAChD,IAAA,MAAM,QAAA,EAAU,QAAA,CAAS,CAAC,CAAA;AAC1B,IAAA,MAAM,UAAA,EAAY,OAAA,CAAQ,IAAA,CAAK,OAAO,CAAA;AAEtC,IAAA,GAAA,CAAI,SAAA,EAAW;AAEb,MAAA,OAAA,EAAS,yBAAA,MAAY,CAAA,CAAE,QAAA,CAAS,CAAA;AAAA,IAClC,EAAA,KAAO;AAGL,MAAA,MAAM,IAAA,EAAM,+BAAA;AAAY,QACtB,CAAC,OAAO,CAAA,EAAG;AAAA,MACb,CAAC,CAAA;AACD,MAAA,OAAA,EAAS,EAAA,EAAI,EAAA,EAAI,GAAA,CAAI,QAAA,CAAS,EAAA,EAAI,GAAA;AAAA,IACpC;AAAA,EACF;AAEA,EAAA,OAAO,MAAA;AACT,CAAA;ADrGA;AACA;AACE;AACA;AACF,uHAAC","file":"/home/runner/work/pixels/pixels/packages/uniform/dist/chunk-TEIXBRF5.cjs","sourcesContent":[null,"import type { VetoTypeAny } from '@fuf-stack/veto';\n\nimport { useEffect, useId } from 'react';\n\nimport { array, objectLoose } from '@fuf-stack/veto';\n\nimport { useFormContext } from '../useFormContext/useFormContext';\n\n/**\n * Hook for adding dynamic client-side validation to forms.\n *\n * Automatically manages validation schema lifecycle: sets schema when data is provided,\n * clears schema when data is null/undefined, and cleans up on unmount.\n *\n * When the client validation schema changes, automatically re-validates all touched\n * fields to ensure they are validated against the new schema.\n *\n * @param data - Data to create validation schema from (or null/undefined to clear validation)\n * @param schemaFactory - Function that creates a validation schema from the data.\n *\n * @example\n * ```tsx\n * const { data: teamData } = useTeamQuery(teamId);\n *\n * useClientValidation(teamData, (data) =>\n * vt.object({\n * username: vt.string().refine(\n * (value) => !data.existingUsers.includes(value),\n * { message: 'Username already exists' }\n * )\n * })\n * );\n * ```\n */\nexport const useClientValidation = <TData = unknown>(\n data: TData | null | undefined,\n schemaFactory: (data: TData) => VetoTypeAny,\n): void => {\n const {\n formState: { touchedFields },\n validation: { setClientValidationSchema },\n trigger,\n } = useFormContext();\n\n // Auto-generate unique key\n const key = useId();\n\n // Use data hash instead of object reference to avoid unnecessary re-runs\n // when data object reference changes but data remains the same\n const dataHash = JSON.stringify(data);\n\n // Single effect to manage validation schema lifecycle\n useEffect(() => {\n // Set validation schema when data is available\n if (data != null) {\n const validationSchema = schemaFactory(data);\n setClientValidationSchema(key, validationSchema);\n } else {\n // Clear validation schema when no data\n setClientValidationSchema(key, null);\n }\n\n // Re-validate all touched fields when client validation schema changes\n const touchedFieldNames = Object.keys(touchedFields);\n if (touchedFieldNames.length > 0) {\n // Use setTimeout to ensure the client validation schema update has propagated\n // before triggering re-validation (fixes race condition)\n setTimeout(async () => {\n return trigger(touchedFieldNames);\n }, 1);\n }\n\n // Cleanup on unmount or when dependencies change\n return () => {\n setClientValidationSchema(key, null);\n };\n // Only re-run when data (dataHash) or key changes\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [dataHash, key]);\n};\n\n/**\n * Helper function to create a veto looseObject schema for a single field with nested path support.\n *\n * Parses field paths (dot-notation) and creates properly nested loose object schemas with array support.\n * All intermediate objects and arrays are marked as optional to allow partial data structures.\n * Numeric segments in the path are treated as array indices, and the parent field is wrapped in an array schema.\n *\n * @param name - The dot-separated path to the field (e.g., 'username', 'user.profile.email', 'items.0.name')\n * @param fieldSchema - The veto validation schema for the field\n * @returns A nested loose object schema matching the field path structure with proper type inference\n *\n * @example\n * ```tsx\n * // Simple field\n * clientValidationSchemaByName('username', vt.string())\n * // => objectLoose({ username: vt.string() })\n * ```\n *\n * @example\n * ```tsx\n * // Nested field (with optional intermediate objects)\n * clientValidationSchemaByName('user.profile.email', vt.string().email())\n * // => objectLoose({ user: objectLoose({ profile: objectLoose({ email: vt.string().email() }).optional() }).optional() })\n * ```\n *\n * @example\n * ```tsx\n * // Array field (with optional array)\n * clientValidationSchemaByName('items.0.name', vt.string())\n * // => objectLoose({ items: array(objectLoose({ name: vt.string() })).optional() })\n * ```\n *\n * @example\n * ```tsx\n * // Usage with useClientValidation\n * const { data: userData } = useUserQuery(userId);\n *\n * useClientValidation(userData, (data) =>\n * clientValidationSchemaByName('username', vt.string().refine(\n * (value) => !data.existingUsernames.includes(value),\n * { message: 'Username already taken' }\n * ))\n * );\n * ```\n */\nexport const clientValidationSchemaByName = <T extends VetoTypeAny>(\n name: string,\n fieldSchema: T,\n) => {\n const segments = name.split('.');\n\n // Build schema from innermost to outermost\n let schema: VetoTypeAny = fieldSchema;\n\n // Process segments in reverse order to build nested structure\n for (let i = segments.length - 1; i >= 0; i -= 1) {\n const segment = segments[i];\n const isNumeric = /^\\d+$/.test(segment);\n\n if (isNumeric) {\n // Current segment is an array index - wrap current schema in optional array\n schema = array(schema).optional();\n } else {\n // Current segment is a field name - wrap in objectLoose\n // Make it optional if it's an intermediate object (not the root)\n const obj = objectLoose({\n [segment]: schema,\n });\n schema = i > 0 ? obj.optional() : obj;\n }\n }\n\n return schema;\n};\n"]}