@paragrav/rhf-utils 0.0.110 → 0.0.111

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/README.md CHANGED
@@ -34,7 +34,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
34
34
  FormComponent: Form.Root,
35
35
 
36
36
  // transform backend error
37
- // use `getOnSubmitTrpcClientErrorHandler` for TRPC backends
37
+ // use `getOnSubmitTrpcClientErrorHandler` HOF for TRPC backends
38
38
  onSubmitError: (
39
39
  error, // unknown
40
40
  ) => {
@@ -201,3 +201,34 @@ declare module '@paragrav/rhf-utils' {
201
201
  }
202
202
  }
203
203
  ```
204
+
205
+ ## Errors
206
+
207
+ You can configure via `RhfUtilsClientConfig` (example at the top) when form context errors are outputted -- i.e., via console and/or thrown error.
208
+
209
+ ### Orphans
210
+
211
+ The concept of orphan errors is any error that is not being shown to user. An example would be a stray field in a schema that is prohibiting users from successfully submitting a form.
212
+
213
+ The criteria for an orphan is any form context error that meets all of the following criteria:
214
+
215
+ - not a root error -- e.g., `root` or `root.${string}`
216
+ - has no `ref` -- RHF includes `ref` to the associated input on each error object (when applicable)
217
+ - has no marker in DOM (e.g., `FormNonFieldErrorMarker`)
218
+
219
+ To get accurate orphan analysis, you must either use `FormNonFieldErrorMarker` in any error you are displaying to user which is not "root" and doesn't have a `ref`. (There is no harm in using this consistently across all errors, even those expected to have a ref.)
220
+
221
+ Alternatively, you can use `FormErrorMessageByPath` to display error message to user:
222
+
223
+ ```tsx
224
+ <FormErrorMessageByPath path="phone" />
225
+ ```
226
+
227
+ Example use case: Field array with minimum items. (When there are no items, the error is not associated with a field and is displayed separately.)
228
+
229
+ Orphans are optionally exposed in a few places.
230
+
231
+ - In errors outputted via console.
232
+ - And `useFlatFieldErrorsContext()` hook, which returns an object with divided into `all`, `root`, and `orphans` errors.
233
+
234
+ `FlatFieldErrors` is a flattened, simplified version of RHF's `FieldErrors`. Keys represent field paths flattened to dot notation.
@@ -2,13 +2,13 @@ import { jsxs as n, Fragment as a, jsx as e } from "react/jsx-runtime";
2
2
  import { useFormState as g, get as i } from "react-hook-form";
3
3
  import F from "../nonfield/FormNonFieldErrorMarker.mjs";
4
4
  import c from "./FormErrorMessage.mjs";
5
- const p = ({ path: o, ...s }) => {
6
- const { errors: m } = g(), r = i(m, o, void 0), t = s.Component ?? c;
7
- return /* @__PURE__ */ n(a, { children: [
5
+ const f = ({ path: o, Component: s }) => {
6
+ const { errors: t } = g(), r = i(t, o, void 0), m = s ?? c;
7
+ return r != null && r.message ? /* @__PURE__ */ n(a, { children: [
8
8
  /* @__PURE__ */ e(F, { path: o }),
9
- (r == null ? void 0 : r.message) && /* @__PURE__ */ e(t, { message: r.message })
10
- ] });
11
- }, l = p;
9
+ /* @__PURE__ */ e(m, { message: r.message })
10
+ ] }) : null;
11
+ }, E = f;
12
12
  export {
13
- l as default
13
+ E as default
14
14
  };
@@ -8,9 +8,15 @@ import { FlatFieldErrors } from './types';
8
8
  * @param errors {@link FieldErrors} object.
9
9
  * @returns FlatFieldErrors object.
10
10
  *
11
- * e.g.,
11
+ * @example
12
12
  * ```
13
- * [ "address.street", { "address.street": { type: "too_short", ref: { value: "123" }, message: "Required." } } ]
13
+ * {
14
+ * "address.street": {
15
+ * type: "too_short",
16
+ * ref: { value: "123" },
17
+ * message: "Required." }
18
+ * }
19
+ * }
14
20
  * ```
15
21
  */
16
22
  declare const getFlatFieldErrors: (errors: FieldErrors) => FlatFieldErrors;
@@ -5,6 +5,10 @@ type Props = {
5
5
  };
6
6
  /**
7
7
  * Empty, hidden marker to indicate a non-field/non-root error is being displayed to user.
8
+ *
9
+ * Example use case:
10
+ * Field array with minimum items.
11
+ * (When there are no items, the error is not associated with a field and is displayed separately.)
8
12
  */
9
13
  declare const FormNonFieldErrorMarker: React.FC<Props>;
10
14
  export default FormNonFieldErrorMarker;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@paragrav/rhf-utils",
3
3
  "author": "paragrav.dev",
4
4
  "license": "MIT",
5
- "version": "0.0.110",
5
+ "version": "0.0.111",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
8
  "sideEffects": false,