@aureus-core/hooks 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +55 -2
- package/dist/internal/utils/constants/index.d.ts +2 -0
- package/dist/internal/utils/constants/index.js +1 -0
- package/dist/internal/utils/constants/is-dev.d.ts +5 -0
- package/dist/internal/utils/constants/is-dev.js +7 -0
- package/dist/internal/utils/helpers/index.d.ts +2 -0
- package/dist/internal/utils/helpers/index.js +1 -0
- package/dist/internal/utils/helpers/warn-once.d.ts +7 -0
- package/dist/internal/utils/helpers/warn-once.js +18 -0
- package/dist/schema-form/factory/create-form-hook.d.ts +18 -0
- package/dist/schema-form/factory/create-form-hook.js +33 -0
- package/dist/schema-form/factory/index.d.ts +2 -0
- package/dist/schema-form/factory/index.js +1 -0
- package/dist/schema-form/hooks/index.d.ts +4 -0
- package/dist/schema-form/hooks/index.js +4 -0
- package/dist/schema-form/hooks/use-merged-values.d.ts +6 -0
- package/dist/schema-form/hooks/use-merged-values.js +12 -0
- package/dist/schema-form/hooks/use-schema-revalidation.d.ts +11 -0
- package/dist/schema-form/hooks/use-schema-revalidation.js +55 -0
- package/dist/schema-form/hooks/use-server-errors.d.ts +8 -0
- package/dist/schema-form/hooks/use-server-errors.js +15 -0
- package/dist/schema-form/index.d.ts +8 -0
- package/dist/schema-form/index.js +4 -0
- package/dist/schema-form/schema-form.d.ts +18 -0
- package/dist/schema-form/schema-form.js +40 -0
- package/dist/schema-form/types/index.d.ts +2 -0
- package/dist/schema-form/types/index.js +0 -0
- package/dist/schema-form/types/schema-form-types.d.ts +105 -0
- package/dist/schema-form/types/schema-form-types.js +0 -0
- package/dist/schema-form/utils/helpers/apply-server-errors.d.ts +18 -0
- package/dist/schema-form/utils/helpers/apply-server-errors.js +50 -0
- package/dist/schema-form/utils/helpers/index.d.ts +4 -0
- package/dist/schema-form/utils/helpers/index.js +4 -0
- package/dist/schema-form/utils/helpers/merge-values.d.ts +9 -0
- package/dist/schema-form/utils/helpers/merge-values.js +24 -0
- package/dist/schema-form/utils/helpers/normalize-server-errors.d.ts +13 -0
- package/dist/schema-form/utils/helpers/normalize-server-errors.js +27 -0
- package/package.json +81 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Eddy Javier Cabrera Rodriguez
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @aureus-core/hooks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Reusable React hooks shared by every Aureus project. ESM only, one entry point per module, no root import.
|
|
4
|
+
|
|
5
|
+
**Documentation:** <https://ing-eddy.github.io/aureus-frontend-core/> (section **hooks**), in English and Spanish, with live examples.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @aureus-core/hooks
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`react` 19 is a required peer. Each module lists the libraries it needs as optional peers, so you only install what you use.
|
|
14
|
+
|
|
15
|
+
| Module | Import path | Needs | What it does |
|
|
16
|
+
| ------------- | -------------------------------- | ----------------------------------------------------- | ------------------------------------------------------------------- |
|
|
17
|
+
| `schema-form` | `@aureus-core/hooks/schema-form` | `react-hook-form` 7, `zod` 4, `@hookform/resolvers` 7 | Creates react-hook-form form hooks from zod schemas or schema hooks |
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
pnpm add @aureus-core/hooks react-hook-form zod @hookform/resolvers
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## `schema-form`
|
|
24
|
+
|
|
25
|
+
Create the hook of a form once, with its schema, its initial values and its own options:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { createFormHook } from "@aureus-core/hooks/schema-form";
|
|
29
|
+
|
|
30
|
+
export const useCompanyForm = createFormHook({
|
|
31
|
+
schema: useCompanySchema, // a zod schema, or a schema hook (useTranslation + useMemo)
|
|
32
|
+
defaultValues: { name: "", city: "", taxId: "" },
|
|
33
|
+
mode: "onBlur", // any useForm option; react-hook-form's defaults otherwise
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Use it to create or edit:
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
const form = useCompanyForm({ schemaParams: { isEdit: false } }); // create: empty form
|
|
41
|
+
const form = useCompanyForm({ schemaParams: { isEdit: true }, values: company }); // edit: merged over the defaults, kept in sync
|
|
42
|
+
|
|
43
|
+
return <FormProvider {...form}>{/* *-control components from @aureus-core/ui */}</FormProvider>;
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- **`values`** is existing data, deep-merged over `defaultValues`. When it arrives later or its content changes, the form is reset to it.
|
|
47
|
+
- **`schemaParams`** is passed to a schema hook that takes a parameter. TypeScript requires it, accepts it or rejects it, following that parameter.
|
|
48
|
+
- **`serverErrors`** takes backend errors as they come: `{ email: ["Already registered", "Domain not allowed"], root: "…" }`. Messages are joined with `joinMessages` (default: a space), and the full array is kept in `error.types.server`. Use `parseServerErrors` for other shapes, or `applyServerErrors(form, errors)` in a callback.
|
|
49
|
+
- **Messages follow the language.** When the schema hook returns a new schema (its `t` changed), the fields that show a schema error are validated again. Server errors are kept.
|
|
50
|
+
- It returns react-hook-form's own `UseFormReturn` (input values, output data on submit).
|
|
51
|
+
|
|
52
|
+
`useSchemaForm(schema, { defaultValues })` is the same engine without the factory, for one-off forms.
|
|
53
|
+
|
|
54
|
+
## Development warnings
|
|
55
|
+
|
|
56
|
+
Misuse (an unmemoized schema, a server error for an unknown field) logs a `console.warn` once, outside production. The check reads `process.env.NODE_ENV`, which your bundler replaces at build time.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isDev } from "./is-dev.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// src/internal/utils/constants/is-dev.ts
|
|
2
|
+
/**
|
|
3
|
+
* True outside production builds. Bundlers replace `process.env.NODE_ENV` statically, as they do
|
|
4
|
+
* for React itself, so development-only warnings are removed from production bundles.
|
|
5
|
+
*/
|
|
6
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
7
|
+
export { isDev };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { resetWarnings, warnOnce } from "./warn-once.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logs `message` with `console.warn` once per `key`, and only outside production builds. Messages
|
|
3
|
+
* start with the hook name in brackets (`[useSchemaForm] …`).
|
|
4
|
+
*/
|
|
5
|
+
export declare function warnOnce(key: string, message: string): void;
|
|
6
|
+
/** Forgets every warning already logged. For tests only; not part of any public module. */
|
|
7
|
+
export declare function resetWarnings(): void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { isDev } from "../constants/is-dev.js";
|
|
2
|
+
// src/internal/utils/helpers/warn-once.ts
|
|
3
|
+
/** The keys already warned about, kept for the life of the page. */
|
|
4
|
+
const warned = /* @__PURE__ */ new Set();
|
|
5
|
+
/**
|
|
6
|
+
* Logs `message` with `console.warn` once per `key`, and only outside production builds. Messages
|
|
7
|
+
* start with the hook name in brackets (`[useSchemaForm] …`).
|
|
8
|
+
*/
|
|
9
|
+
function warnOnce(key, message) {
|
|
10
|
+
if (!isDev || warned.has(key)) return;
|
|
11
|
+
warned.add(key);
|
|
12
|
+
console.warn(message);
|
|
13
|
+
}
|
|
14
|
+
/** Forgets every warning already logged. For tests only; not part of any public module. */
|
|
15
|
+
function resetWarnings() {
|
|
16
|
+
warned.clear();
|
|
17
|
+
}
|
|
18
|
+
export { resetWarnings, warnOnce };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CreateFormHookConfig, FormHook, FormInput, SchemaOf, SchemaSource, ServerErrors } from "../types/schema-form-types.js";
|
|
2
|
+
import "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Creates the hook of one form from its schema, its initial values and its own options. No global
|
|
5
|
+
* configuration: each form hook uses only what it is given here.
|
|
6
|
+
*
|
|
7
|
+
* - `schema`: a zod schema, or a schema hook (`useLoginSchema`). A hook with a parameter receives
|
|
8
|
+
* `schemaParams`, which TypeScript then requires (or accepts, if the parameter is optional).
|
|
9
|
+
* - `defaultValues`: the initial values (usually empty), with the schema's input type.
|
|
10
|
+
* - Any `useForm` option (`mode`, `shouldUnregister`…), `joinMessages`, `parseServerErrors` and
|
|
11
|
+
* `resolverOptions`. Options passed when the hook is called override these for that call.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* export const useCompanyForm = createFormHook({ schema: useCompanySchema, defaultValues: COMPANY_DEFAULTS });
|
|
15
|
+
*
|
|
16
|
+
* const form = useCompanyForm({ schemaParams: { isEdit: true }, values: company, serverErrors });
|
|
17
|
+
*/
|
|
18
|
+
export declare function createFormHook<Src extends SchemaSource, B = ServerErrors<FormInput<SchemaOf<Src>>>>(config: CreateFormHookConfig<Src, B>): FormHook<Src, B>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useSchemaForm } from "../schema-form.js";
|
|
2
|
+
// src/schema-form/factory/create-form-hook.ts
|
|
3
|
+
/** `base` with the defined entries of `overrides` on top (an `undefined` override keeps the base). */
|
|
4
|
+
function withOverrides(base, overrides) {
|
|
5
|
+
const result = { ...base };
|
|
6
|
+
for (const [key, value] of Object.entries(overrides)) if (value !== void 0) result[key] = value;
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates the hook of one form from its schema, its initial values and its own options. No global
|
|
11
|
+
* configuration: each form hook uses only what it is given here.
|
|
12
|
+
*
|
|
13
|
+
* - `schema`: a zod schema, or a schema hook (`useLoginSchema`). A hook with a parameter receives
|
|
14
|
+
* `schemaParams`, which TypeScript then requires (or accepts, if the parameter is optional).
|
|
15
|
+
* - `defaultValues`: the initial values (usually empty), with the schema's input type.
|
|
16
|
+
* - Any `useForm` option (`mode`, `shouldUnregister`…), `joinMessages`, `parseServerErrors` and
|
|
17
|
+
* `resolverOptions`. Options passed when the hook is called override these for that call.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* export const useCompanyForm = createFormHook({ schema: useCompanySchema, defaultValues: COMPANY_DEFAULTS });
|
|
21
|
+
*
|
|
22
|
+
* const form = useCompanyForm({ schemaParams: { isEdit: true }, values: company, serverErrors });
|
|
23
|
+
*/
|
|
24
|
+
function createFormHook(config) {
|
|
25
|
+
const { schema, ...formConfig } = config;
|
|
26
|
+
function useConfiguredForm(options) {
|
|
27
|
+
const { schemaParams, ...callOptions } = options ?? {};
|
|
28
|
+
const resolvedSchema = typeof schema === "function" ? schema(schemaParams) : schema;
|
|
29
|
+
return useSchemaForm(resolvedSchema, withOverrides(formConfig, callOptions));
|
|
30
|
+
}
|
|
31
|
+
return useConfiguredForm;
|
|
32
|
+
}
|
|
33
|
+
export { createFormHook };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createFormHook } from "./create-form-hook.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `values` merged over `defaultValues`, for react-hook-form's `values` option. `undefined` (no
|
|
3
|
+
* existing data) keeps the form on its default values. react-hook-form compares the result deeply,
|
|
4
|
+
* so a new object with equal content does not reset the form.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useMergedValues<T>(defaultValues: T, values: unknown): T | undefined;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { mergeValues } from "../utils/helpers/merge-values.js";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
// src/schema-form/hooks/use-merged-values.ts
|
|
4
|
+
/**
|
|
5
|
+
* `values` merged over `defaultValues`, for react-hook-form's `values` option. `undefined` (no
|
|
6
|
+
* existing data) keeps the form on its default values. react-hook-form compares the result deeply,
|
|
7
|
+
* so a new object with equal content does not reset the form.
|
|
8
|
+
*/
|
|
9
|
+
function useMergedValues(defaultValues, values) {
|
|
10
|
+
return useMemo(() => values === void 0 || values === null ? void 0 : mergeValues(defaultValues, values), [defaultValues, values]);
|
|
11
|
+
}
|
|
12
|
+
export { useMergedValues };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FieldValues, UseFormReturn } from "react-hook-form";
|
|
2
|
+
/**
|
|
3
|
+
* When the schema changes identity (the language changed, or the schema hook's parameters did),
|
|
4
|
+
* validates again the fields that show a schema error, so their messages follow the new schema.
|
|
5
|
+
* Fields without an error, and server errors, are left alone. An error of the object as a whole (a
|
|
6
|
+
* refinement without a `path`) belongs to no field and is not validated again.
|
|
7
|
+
*
|
|
8
|
+
* A schema that changes on the renders caused by that re-validation (not memoized) is not chased:
|
|
9
|
+
* the re-validation stops and a development warning explains why.
|
|
10
|
+
*/
|
|
11
|
+
export declare function useSchemaRevalidation<In extends FieldValues>(form: UseFormReturn<In, any, any>, schema: unknown): void;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { warnOnce } from "../../internal/utils/helpers/warn-once.js";
|
|
2
|
+
import { isPlainObject } from "../utils/helpers/merge-values.js";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
// src/schema-form/hooks/use-schema-revalidation.ts
|
|
5
|
+
/** Every path of `values` (objects and arrays, nested), in dot notation. */
|
|
6
|
+
function valuePaths(values, prefix = "") {
|
|
7
|
+
return (Array.isArray(values) ? values.map((value, index) => [String(index), value]) : isPlainObject(values) ? Object.entries(values) : []).flatMap(([key, value]) => {
|
|
8
|
+
const path = prefix === "" ? key : `${prefix}.${key}`;
|
|
9
|
+
return [path, ...valuePaths(value, path)];
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
/** An error set by validation: server errors have the type `"server"` and are left alone. */
|
|
13
|
+
const isSchemaError = (error) => isPlainObject(error) && typeof error.type === "string" && error.type !== "server";
|
|
14
|
+
/**
|
|
15
|
+
* Paths of the fields that show an error that does not come from the server. Read through
|
|
16
|
+
* `getFieldState`, which sees the live errors even when the component owning the form does not read
|
|
17
|
+
* `formState` while rendering. An array-level error (`items.root`) is re-validated through its array.
|
|
18
|
+
*/
|
|
19
|
+
function schemaErrorPaths(form) {
|
|
20
|
+
const paths = [];
|
|
21
|
+
for (const path of valuePaths(form.getValues())) {
|
|
22
|
+
const { error } = form.getFieldState(path);
|
|
23
|
+
if (isSchemaError(error) || isSchemaError(form.getFieldState(`${path}.root`).error)) paths.push(path);
|
|
24
|
+
}
|
|
25
|
+
return paths;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* When the schema changes identity (the language changed, or the schema hook's parameters did),
|
|
29
|
+
* validates again the fields that show a schema error, so their messages follow the new schema.
|
|
30
|
+
* Fields without an error, and server errors, are left alone. An error of the object as a whole (a
|
|
31
|
+
* refinement without a `path`) belongs to no field and is not validated again.
|
|
32
|
+
*
|
|
33
|
+
* A schema that changes on the renders caused by that re-validation (not memoized) is not chased:
|
|
34
|
+
* the re-validation stops and a development warning explains why.
|
|
35
|
+
*/
|
|
36
|
+
function useSchemaRevalidation(form, schema) {
|
|
37
|
+
const previousSchema = useRef(schema);
|
|
38
|
+
const revalidating = useRef(false);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (previousSchema.current === schema) {
|
|
41
|
+
revalidating.current = false;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
previousSchema.current = schema;
|
|
45
|
+
if (revalidating.current) {
|
|
46
|
+
warnOnce("useSchemaForm:unstable-schema", "[useSchemaForm] The schema changed again while its errors were being re-validated, so it is probably created on every render. Memoize it (useMemo with [t, ...params]) and pass primitive or memoized schemaParams.");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const paths = schemaErrorPaths(form);
|
|
50
|
+
if (paths.length === 0) return;
|
|
51
|
+
revalidating.current = true;
|
|
52
|
+
form.trigger(paths);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
export { useSchemaRevalidation };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ApplyServerErrorsOptions } from "../types/schema-form-types.js";
|
|
2
|
+
import "../types/index.js";
|
|
3
|
+
import { FieldValues, UseFormReturn } from "react-hook-form";
|
|
4
|
+
/**
|
|
5
|
+
* Sets `serverErrors` on the form each time a new object is received. The same object passed again on
|
|
6
|
+
* later renders is not set again, so an error the user has fixed does not come back.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useServerErrors<In extends FieldValues, B>(form: UseFormReturn<In, any, any>, serverErrors: B | null | undefined, options: ApplyServerErrorsOptions<B, In>): void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { setServerErrors } from "../utils/helpers/apply-server-errors.js";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
// src/schema-form/hooks/use-server-errors.ts
|
|
4
|
+
/**
|
|
5
|
+
* Sets `serverErrors` on the form each time a new object is received. The same object passed again on
|
|
6
|
+
* later renders is not set again, so an error the user has fixed does not come back.
|
|
7
|
+
*/
|
|
8
|
+
function useServerErrors(form, serverErrors, options) {
|
|
9
|
+
const optionsRef = useRef(options);
|
|
10
|
+
optionsRef.current = options;
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
setServerErrors(form, serverErrors, optionsRef.current, "useSchemaForm");
|
|
13
|
+
}, [form, serverErrors]);
|
|
14
|
+
}
|
|
15
|
+
export { useServerErrors };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ApplyServerErrorsOptions, CreateFormHookConfig, DeepPartial, FormBehaviorOptions, FormDataOptions, FormHook, FormHookArgs, FormHookOptions, FormInput, FormOutput, FormSchema, JoinMessages, ResolverOptions, SchemaFormOptions, SchemaFormReturn, SchemaOf, SchemaSource, ServerErrorMessages, ServerErrorPath, ServerErrors, ServerErrorsParser } from "./types/schema-form-types.js";
|
|
2
|
+
import "./factory/index.js";
|
|
3
|
+
import { createFormHook } from "./factory/create-form-hook.js";
|
|
4
|
+
import { useSchemaForm } from "./schema-form.js";
|
|
5
|
+
import "./types/index.js";
|
|
6
|
+
import { applyServerErrors } from "./utils/helpers/apply-server-errors.js";
|
|
7
|
+
import "./utils/helpers/index.js";
|
|
8
|
+
export { type ApplyServerErrorsOptions, type CreateFormHookConfig, type DeepPartial, type FormBehaviorOptions, type FormDataOptions, type FormHook, type FormHookArgs, type FormHookOptions, type FormInput, type FormOutput, type FormSchema, type JoinMessages, type ResolverOptions, type SchemaFormOptions, type SchemaFormReturn, type SchemaOf, type SchemaSource, type ServerErrorMessages, type ServerErrorPath, type ServerErrors, type ServerErrorsParser, applyServerErrors, createFormHook, useSchemaForm };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FormInput, FormSchema, SchemaFormOptions, SchemaFormReturn, ServerErrors } from "./types/schema-form-types.js";
|
|
2
|
+
import "./types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* A react-hook-form form validated by a zod schema. Returns react-hook-form's own form object, ready
|
|
5
|
+
* for `<FormProvider {...form}>`.
|
|
6
|
+
*
|
|
7
|
+
* - `defaultValues`: the initial values (usually empty).
|
|
8
|
+
* - `values`: existing data (edit forms), deep-merged over `defaultValues` and kept in sync.
|
|
9
|
+
* - `serverErrors`: backend errors (`{ email: ["…", "…"] }`), set each time a new object arrives.
|
|
10
|
+
* - Any other `useForm` option is passed through unchanged; react-hook-form's defaults apply.
|
|
11
|
+
*
|
|
12
|
+
* When the schema changes identity (language or parameters), the fields with a schema error are
|
|
13
|
+
* validated again, so their messages follow it.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const form = useSchemaForm(profileSchema, { defaultValues: { name: "" } });
|
|
17
|
+
*/
|
|
18
|
+
export declare function useSchemaForm<S extends FormSchema, B = ServerErrors<FormInput<S>>>(schema: S, options: SchemaFormOptions<S, B>): SchemaFormReturn<S>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useMergedValues } from "./hooks/use-merged-values.js";
|
|
2
|
+
import { useSchemaRevalidation } from "./hooks/use-schema-revalidation.js";
|
|
3
|
+
import { useServerErrors } from "./hooks/use-server-errors.js";
|
|
4
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
5
|
+
import { useMemo } from "react";
|
|
6
|
+
import { useForm } from "react-hook-form";
|
|
7
|
+
// src/schema-form/schema-form.ts
|
|
8
|
+
/**
|
|
9
|
+
* A react-hook-form form validated by a zod schema. Returns react-hook-form's own form object, ready
|
|
10
|
+
* for `<FormProvider {...form}>`.
|
|
11
|
+
*
|
|
12
|
+
* - `defaultValues`: the initial values (usually empty).
|
|
13
|
+
* - `values`: existing data (edit forms), deep-merged over `defaultValues` and kept in sync.
|
|
14
|
+
* - `serverErrors`: backend errors (`{ email: ["…", "…"] }`), set each time a new object arrives.
|
|
15
|
+
* - Any other `useForm` option is passed through unchanged; react-hook-form's defaults apply.
|
|
16
|
+
*
|
|
17
|
+
* When the schema changes identity (language or parameters), the fields with a schema error are
|
|
18
|
+
* validated again, so their messages follow it.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const form = useSchemaForm(profileSchema, { defaultValues: { name: "" } });
|
|
22
|
+
*/
|
|
23
|
+
function useSchemaForm(schema, options) {
|
|
24
|
+
const { defaultValues, values, serverErrors, parseServerErrors, joinMessages, resolverOptions, ...formOptions } = options;
|
|
25
|
+
const resolver = useMemo(() => zodResolver(schema, resolverOptions), [schema, resolverOptions]);
|
|
26
|
+
const mergedValues = useMergedValues(defaultValues, values);
|
|
27
|
+
const form = useForm({
|
|
28
|
+
...formOptions,
|
|
29
|
+
resolver,
|
|
30
|
+
defaultValues,
|
|
31
|
+
values: mergedValues
|
|
32
|
+
});
|
|
33
|
+
useServerErrors(form, serverErrors, {
|
|
34
|
+
joinMessages,
|
|
35
|
+
parse: parseServerErrors
|
|
36
|
+
});
|
|
37
|
+
useSchemaRevalidation(form, schema);
|
|
38
|
+
return form;
|
|
39
|
+
}
|
|
40
|
+
export { useSchemaForm };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { ApplyServerErrorsOptions, CreateFormHookConfig, DeepPartial, FormBehaviorOptions, FormDataOptions, FormHook, FormHookArgs, FormHookOptions, FormInput, FormOutput, FormSchema, JoinMessages, ResolverOptions, SchemaFormOptions, SchemaFormReturn, SchemaOf, SchemaSource, ServerErrorMessages, ServerErrorPath, ServerErrors, ServerErrorsParser } from "./schema-form-types.js";
|
|
2
|
+
export type { ApplyServerErrorsOptions, CreateFormHookConfig, DeepPartial, FormBehaviorOptions, FormDataOptions, FormHook, FormHookArgs, FormHookOptions, FormInput, FormOutput, FormSchema, JoinMessages, ResolverOptions, SchemaFormOptions, SchemaFormReturn, SchemaOf, SchemaSource, ServerErrorMessages, ServerErrorPath, ServerErrors, ServerErrorsParser };
|
|
File without changes
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
2
|
+
import { FieldPath, FieldValues, UseFormProps, UseFormReturn } from "react-hook-form";
|
|
3
|
+
import * as z4 from "zod/v4/core";
|
|
4
|
+
/** Any zod 4 schema (zod or zod/mini) whose input is an object: the values of a form. */
|
|
5
|
+
export type FormSchema = z4.$ZodType<unknown, FieldValues>;
|
|
6
|
+
/** The form's values: the schema's input type. `defaultValues` and `values` use it. */
|
|
7
|
+
export type FormInput<S extends FormSchema> = z4.input<S> extends FieldValues ? z4.input<S> : never;
|
|
8
|
+
/** The data passed to `handleSubmit`: the schema's output type (after transforms and coercion). */
|
|
9
|
+
export type FormOutput<S extends FormSchema> = z4.output<S>;
|
|
10
|
+
/**
|
|
11
|
+
* Partial values for edit forms: every object property is optional, recursively. Arrays, dates and
|
|
12
|
+
* other non-plain values are replaced as a whole, so they are not made partial.
|
|
13
|
+
*/
|
|
14
|
+
export type DeepPartial<T> = T extends readonly unknown[] | Date | ((...args: never[]) => unknown) ? T : T extends object ? { [K in keyof T]?: DeepPartial<T[K]>; } : T;
|
|
15
|
+
/** A zod schema, or a hook (with or without one parameter) that returns one. */
|
|
16
|
+
export type SchemaSource = FormSchema | ((params: any) => FormSchema);
|
|
17
|
+
/** The schema produced by a `SchemaSource`. */
|
|
18
|
+
export type SchemaOf<Src> = Src extends ((...args: never[]) => infer S) ? S : Src;
|
|
19
|
+
/** A key of server errors: a field path (dot notation), or `root` / `root.<name>` for form-level errors. */
|
|
20
|
+
export type ServerErrorPath<In extends FieldValues> = FieldPath<In> | "root" | `root.${string}`;
|
|
21
|
+
/** One message or several for the same field. Empty messages are ignored. */
|
|
22
|
+
export type ServerErrorMessages = string | readonly string[] | null | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Validation errors returned by a backend, by field path:
|
|
25
|
+
* `{ email: ["Already registered", "Domain not allowed"], "address.city": "Required", root: "…" }`.
|
|
26
|
+
*/
|
|
27
|
+
export type ServerErrors<In extends FieldValues = FieldValues> = Partial<Record<ServerErrorPath<In>, ServerErrorMessages>>;
|
|
28
|
+
/** Joins the messages of one field into the text shown by the control. */
|
|
29
|
+
export type JoinMessages = (messages: readonly string[]) => string;
|
|
30
|
+
/** Turns a backend error body of any shape into `ServerErrors`. */
|
|
31
|
+
export type ServerErrorsParser<B, In extends FieldValues> = (body: B) => ServerErrors<In> | null | undefined;
|
|
32
|
+
/** zod's parse options, passed to the resolver (for example `{ reportInput: true }`). */
|
|
33
|
+
export type ResolverOptions = Parameters<typeof zodResolver>[1];
|
|
34
|
+
/** Options the module manages itself, so they cannot be passed to `useForm`. */
|
|
35
|
+
type ManagedFormProps = "resolver" | "context" | "defaultValues" | "values";
|
|
36
|
+
/**
|
|
37
|
+
* The behaviour of a form: every react-hook-form `useForm` option (except `resolver`, `context`,
|
|
38
|
+
* `defaultValues` and `values`), plus the module's own. All optional and independent; react-hook-form's
|
|
39
|
+
* defaults apply to anything left out.
|
|
40
|
+
*/
|
|
41
|
+
export type FormBehaviorOptions<S extends FormSchema, B = ServerErrors<FormInput<S>>> = Omit<UseFormProps<FormInput<S>, unknown, FormOutput<S>>, ManagedFormProps> & {
|
|
42
|
+
/** Joins several server messages of one field. Default: joined with a single space. */
|
|
43
|
+
joinMessages?: JoinMessages;
|
|
44
|
+
/** Converts `serverErrors` from your backend's shape. Without it, `serverErrors` is `ServerErrors`. */
|
|
45
|
+
parseServerErrors?: ServerErrorsParser<B, FormInput<S>>;
|
|
46
|
+
/** zod's parse options for validation. Keep the object stable (a constant). */
|
|
47
|
+
resolverOptions?: ResolverOptions;
|
|
48
|
+
};
|
|
49
|
+
/** What a component passes when it uses a form: existing values and server errors. */
|
|
50
|
+
export type FormDataOptions<S extends FormSchema, B = ServerErrors<FormInput<S>>> = {
|
|
51
|
+
/**
|
|
52
|
+
* Existing data (edit forms), deep-merged over `defaultValues`. When it arrives later or its content
|
|
53
|
+
* changes, the form is reset to it (`resetOptions` applies). A new object with equal content does
|
|
54
|
+
* not reset.
|
|
55
|
+
*/
|
|
56
|
+
values?: DeepPartial<FormInput<S>> | null;
|
|
57
|
+
/**
|
|
58
|
+
* Backend validation errors. They are set each time a new object is passed, so pass a stable object
|
|
59
|
+
* (for example the request's error), not a literal created on every render.
|
|
60
|
+
*/
|
|
61
|
+
serverErrors?: B | null;
|
|
62
|
+
};
|
|
63
|
+
/** Options of `useSchemaForm`. */
|
|
64
|
+
export type SchemaFormOptions<S extends FormSchema, B = ServerErrors<FormInput<S>>> = FormBehaviorOptions<S, B> & FormDataOptions<S, B> & {
|
|
65
|
+
/** The form's initial values (usually empty), with the schema's input type. */
|
|
66
|
+
defaultValues: FormInput<S>;
|
|
67
|
+
};
|
|
68
|
+
/** react-hook-form's form object for a schema: input values, output data on submit. */
|
|
69
|
+
export type SchemaFormReturn<S extends FormSchema> = UseFormReturn<FormInput<S>, unknown, FormOutput<S>>;
|
|
70
|
+
/** Configuration of `createFormHook`: the schema, the initial values and this form's behaviour. */
|
|
71
|
+
export type CreateFormHookConfig<Src extends SchemaSource, B = ServerErrors<FormInput<SchemaOf<Src>>>> = FormBehaviorOptions<SchemaOf<Src>, B> & {
|
|
72
|
+
/** A zod schema, or a schema hook (for example `useLoginSchema`), with or without one parameter. */
|
|
73
|
+
schema: Src;
|
|
74
|
+
/** The form's initial values (usually empty), with the schema's input type. */
|
|
75
|
+
defaultValues: FormInput<SchemaOf<Src>>;
|
|
76
|
+
};
|
|
77
|
+
/** Options of a form hook created with `createFormHook`. Behaviour options override the config for that call. */
|
|
78
|
+
export type FormHookOptions<Src extends SchemaSource, B = ServerErrors<FormInput<SchemaOf<Src>>>> = FormBehaviorOptions<SchemaOf<Src>, B> & FormDataOptions<SchemaOf<Src>, B>;
|
|
79
|
+
/** The parameter list of a schema hook, or `[]` for a static schema. */
|
|
80
|
+
type SchemaParamsOf<Src> = Src extends ((...args: infer A) => unknown) ? A : [];
|
|
81
|
+
/**
|
|
82
|
+
* The arguments of a form hook, from the schema hook's parameter:
|
|
83
|
+
* - static schema or hook without parameters → `schemaParams` is not accepted;
|
|
84
|
+
* - required parameter → `options` and `schemaParams` are required;
|
|
85
|
+
* - optional parameter → `schemaParams` is optional.
|
|
86
|
+
*/
|
|
87
|
+
export type FormHookArgs<Src extends SchemaSource, Opts> = SchemaParamsOf<Src> extends [] ? [options?: Opts & {
|
|
88
|
+
schemaParams?: never;
|
|
89
|
+
}] : SchemaParamsOf<Src> extends [infer P] ? [options: Opts & {
|
|
90
|
+
schemaParams: P;
|
|
91
|
+
}] : SchemaParamsOf<Src> extends [(infer P)?] ? [options?: Opts & {
|
|
92
|
+
schemaParams?: P;
|
|
93
|
+
}] : [options?: Opts & {
|
|
94
|
+
/** The parameter, when its shape cannot be inferred. */ schemaParams?: unknown;
|
|
95
|
+
}];
|
|
96
|
+
/** A form hook created with `createFormHook` (for example `useLoginForm`). */
|
|
97
|
+
export type FormHook<Src extends SchemaSource, B = ServerErrors<FormInput<SchemaOf<Src>>>> = (...args: FormHookArgs<Src, FormHookOptions<Src, B>>) => SchemaFormReturn<SchemaOf<Src>>;
|
|
98
|
+
/** Options of `applyServerErrors`. */
|
|
99
|
+
export type ApplyServerErrorsOptions<B, In extends FieldValues> = {
|
|
100
|
+
/** Joins several messages of one field. Default: joined with a single space. */
|
|
101
|
+
joinMessages?: JoinMessages;
|
|
102
|
+
/** Converts the errors from your backend's shape. */
|
|
103
|
+
parse?: ServerErrorsParser<B, In>;
|
|
104
|
+
};
|
|
105
|
+
export {};
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ApplyServerErrorsOptions, ServerErrors } from "../../types/schema-form-types.js";
|
|
2
|
+
import "../../types/index.js";
|
|
3
|
+
import { FieldValues, UseFormReturn } from "react-hook-form";
|
|
4
|
+
/**
|
|
5
|
+
* Sets backend errors on a form (shared by `applyServerErrors` and the `serverErrors` option).
|
|
6
|
+
* `source` names the caller in development warnings.
|
|
7
|
+
*/
|
|
8
|
+
export declare function setServerErrors<In extends FieldValues, B>(form: UseFormReturn<In, any, any>, errors: B | null | undefined, options: ApplyServerErrorsOptions<B, In>, source: string): void;
|
|
9
|
+
/**
|
|
10
|
+
* Sets backend validation errors on a form, for example in a request's error callback. Each field
|
|
11
|
+
* gets an error of type `"server"`: its message is the field's messages joined with `joinMessages`
|
|
12
|
+
* (default: a space), and `types.server` holds them all. Keys may be dot paths (`"items.0.name"`) or
|
|
13
|
+
* `root` / `root.<name>`. Empty messages are ignored.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* mutation.mutate(values, { onError: (error) => applyServerErrors(form, error.body.errors) });
|
|
17
|
+
*/
|
|
18
|
+
export declare function applyServerErrors<In extends FieldValues, B = ServerErrors<In>>(form: UseFormReturn<In, any, any>, errors: B | null | undefined, options?: ApplyServerErrorsOptions<B, In>): void;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { warnOnce } from "../../../internal/utils/helpers/warn-once.js";
|
|
2
|
+
import { isPlainObject } from "./merge-values.js";
|
|
3
|
+
import { normalizeServerErrors } from "./normalize-server-errors.js";
|
|
4
|
+
// src/schema-form/utils/helpers/apply-server-errors.ts
|
|
5
|
+
/** The default `joinMessages`. */
|
|
6
|
+
const joinWithSpace = (messages) => messages.join(" ");
|
|
7
|
+
/** True when `path` (dot notation) points to an existing key of `values`. */
|
|
8
|
+
function hasPath(values, path) {
|
|
9
|
+
let current = values;
|
|
10
|
+
for (const segment of path.split(".")) if (Array.isArray(current)) {
|
|
11
|
+
const index = Number(segment);
|
|
12
|
+
if (!Number.isInteger(index) || index < 0 || index >= current.length) return false;
|
|
13
|
+
current = current[index];
|
|
14
|
+
} else if (isPlainObject(current) && segment in current) current = current[segment];
|
|
15
|
+
else return false;
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
/** Form-level errors: they name no field, so they are not checked against the values. */
|
|
19
|
+
const isRootPath = (name) => name === "root" || name.startsWith("root.");
|
|
20
|
+
/**
|
|
21
|
+
* Sets backend errors on a form (shared by `applyServerErrors` and the `serverErrors` option).
|
|
22
|
+
* `source` names the caller in development warnings.
|
|
23
|
+
*/
|
|
24
|
+
function setServerErrors(form, errors, options, source) {
|
|
25
|
+
if (errors === null || errors === void 0) return;
|
|
26
|
+
const { joinMessages = joinWithSpace, parse } = options;
|
|
27
|
+
const raw = parse ? parse(errors) : errors;
|
|
28
|
+
const values = form.getValues();
|
|
29
|
+
for (const { name, messages } of normalizeServerErrors(raw, source)) {
|
|
30
|
+
if (!isRootPath(name) && !hasPath(values, name)) warnOnce(`${source}:unknown-server-field:${name}`, `[${source}] Server error for "${name}", which is not a field of the form's values. Check the key, or add the field to defaultValues.`);
|
|
31
|
+
form.setError(name, {
|
|
32
|
+
type: "server",
|
|
33
|
+
message: joinMessages(messages),
|
|
34
|
+
types: { server: messages }
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sets backend validation errors on a form, for example in a request's error callback. Each field
|
|
40
|
+
* gets an error of type `"server"`: its message is the field's messages joined with `joinMessages`
|
|
41
|
+
* (default: a space), and `types.server` holds them all. Keys may be dot paths (`"items.0.name"`) or
|
|
42
|
+
* `root` / `root.<name>`. Empty messages are ignored.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* mutation.mutate(values, { onError: (error) => applyServerErrors(form, error.body.errors) });
|
|
46
|
+
*/
|
|
47
|
+
function applyServerErrors(form, errors, options = {}) {
|
|
48
|
+
setServerErrors(form, errors, options, "applyServerErrors");
|
|
49
|
+
}
|
|
50
|
+
export { applyServerErrors, setServerErrors };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { applyServerErrors, setServerErrors } from "./apply-server-errors.js";
|
|
2
|
+
import { isPlainObject, mergeValues } from "./merge-values.js";
|
|
3
|
+
import { NormalizedServerError, normalizeServerErrors } from "./normalize-server-errors.js";
|
|
4
|
+
export { type NormalizedServerError, applyServerErrors, isPlainObject, mergeValues, normalizeServerErrors, setServerErrors };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { applyServerErrors, setServerErrors } from "./apply-server-errors.js";
|
|
2
|
+
import { isPlainObject, mergeValues } from "./merge-values.js";
|
|
3
|
+
import { normalizeServerErrors } from "./normalize-server-errors.js";
|
|
4
|
+
export { applyServerErrors, isPlainObject, mergeValues, normalizeServerErrors, setServerErrors };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** A plain object (`{}` or `Object.create(null)`), as opposed to arrays, dates, class instances or files. */
|
|
2
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
3
|
+
/**
|
|
4
|
+
* Deep-merges `values` over `defaults`, without mutating either:
|
|
5
|
+
* - plain objects are merged recursively;
|
|
6
|
+
* - arrays, dates and primitives replace the default;
|
|
7
|
+
* - `undefined` keeps the default, and `null` is kept.
|
|
8
|
+
*/
|
|
9
|
+
export declare function mergeValues<T>(defaults: T, values: unknown): T;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/schema-form/utils/helpers/merge-values.ts
|
|
2
|
+
/** A plain object (`{}` or `Object.create(null)`), as opposed to arrays, dates, class instances or files. */
|
|
3
|
+
function isPlainObject(value) {
|
|
4
|
+
if (value === null || typeof value !== "object") return false;
|
|
5
|
+
const prototype = Object.getPrototypeOf(value);
|
|
6
|
+
return prototype === Object.prototype || prototype === null;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Deep-merges `values` over `defaults`, without mutating either:
|
|
10
|
+
* - plain objects are merged recursively;
|
|
11
|
+
* - arrays, dates and primitives replace the default;
|
|
12
|
+
* - `undefined` keeps the default, and `null` is kept.
|
|
13
|
+
*/
|
|
14
|
+
function mergeValues(defaults, values) {
|
|
15
|
+
if (values === void 0) return defaults;
|
|
16
|
+
if (!isPlainObject(defaults) || !isPlainObject(values)) return values;
|
|
17
|
+
const result = { ...defaults };
|
|
18
|
+
for (const [key, value] of Object.entries(values)) {
|
|
19
|
+
if (value === void 0) continue;
|
|
20
|
+
result[key] = mergeValues(result[key], value);
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
export { isPlainObject, mergeValues };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** One field's server messages, ready for `setError`. */
|
|
2
|
+
export interface NormalizedServerError {
|
|
3
|
+
/** The field path, or `root` / `root.<name>`. */
|
|
4
|
+
name: string;
|
|
5
|
+
/** The field's non-blank messages, in the order received. Never empty. */
|
|
6
|
+
messages: string[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Turns `{ path: string | string[] }` into a list of fields with their non-blank messages. Keys
|
|
10
|
+
* with no message are dropped, silently, and so are values of any other type, such as nested
|
|
11
|
+
* objects. A body that is not an object is ignored with a development warning.
|
|
12
|
+
*/
|
|
13
|
+
export declare function normalizeServerErrors(errors: unknown, source: string): NormalizedServerError[];
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { warnOnce } from "../../../internal/utils/helpers/warn-once.js";
|
|
2
|
+
import { isPlainObject } from "./merge-values.js";
|
|
3
|
+
// src/schema-form/utils/helpers/normalize-server-errors.ts
|
|
4
|
+
/** A text worth showing: blank strings and non-strings are not messages. */
|
|
5
|
+
const isMessage = (value) => typeof value === "string" && value.trim() !== "";
|
|
6
|
+
/**
|
|
7
|
+
* Turns `{ path: string | string[] }` into a list of fields with their non-blank messages. Keys
|
|
8
|
+
* with no message are dropped, silently, and so are values of any other type, such as nested
|
|
9
|
+
* objects. A body that is not an object is ignored with a development warning.
|
|
10
|
+
*/
|
|
11
|
+
function normalizeServerErrors(errors, source) {
|
|
12
|
+
if (errors === null || errors === void 0) return [];
|
|
13
|
+
if (!isPlainObject(errors)) {
|
|
14
|
+
warnOnce(`${source}:server-errors-shape`, `[${source}] serverErrors must be an object of field paths to messages ({ email: ["…"] }). Use parseServerErrors to convert your backend's error body.`);
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
const normalized = [];
|
|
18
|
+
for (const [name, value] of Object.entries(errors)) {
|
|
19
|
+
const messages = (Array.isArray(value) ? value : [value]).filter(isMessage);
|
|
20
|
+
if (messages.length > 0) normalized.push({
|
|
21
|
+
name,
|
|
22
|
+
messages
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return normalized;
|
|
26
|
+
}
|
|
27
|
+
export { normalizeServerErrors };
|
package/package.json
CHANGED
|
@@ -1,6 +1,84 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aureus-core/hooks",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Reusable React hooks for Aureus projects: schema-driven react-hook-form forms",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Ing-Eddy/aureus-frontend-core.git",
|
|
10
|
+
"directory": "packages/hooks"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"exports": {
|
|
17
|
+
"./internal/*": null,
|
|
18
|
+
"./*": {
|
|
19
|
+
"types": "./dist/*/index.d.ts",
|
|
20
|
+
"default": "./dist/*/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"provenance": true
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@hookform/resolvers": "^7",
|
|
30
|
+
"react": "^19",
|
|
31
|
+
"react-hook-form": "^7",
|
|
32
|
+
"zod": "^4"
|
|
33
|
+
},
|
|
34
|
+
"peerDependenciesMeta": {
|
|
35
|
+
"@hookform/resolvers": {
|
|
36
|
+
"optional": true
|
|
37
|
+
},
|
|
38
|
+
"react-hook-form": {
|
|
39
|
+
"optional": true
|
|
40
|
+
},
|
|
41
|
+
"zod": {
|
|
42
|
+
"optional": true
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
47
|
+
"@aureus-core/doc-guard": "0.0.0",
|
|
48
|
+
"@aureus-core/tsconfig": "0.0.0",
|
|
49
|
+
"@hookform/resolvers": "^7.0.6",
|
|
50
|
+
"@testing-library/dom": "^10.4.2",
|
|
51
|
+
"@testing-library/react": "^16.3.3",
|
|
52
|
+
"@testing-library/user-event": "^14.6.10",
|
|
53
|
+
"@types/node": "^26.6.6",
|
|
54
|
+
"@types/react": "^19.3.0",
|
|
55
|
+
"@types/react-dom": "^19.3.0",
|
|
56
|
+
"jsdom": "^31.0.0",
|
|
57
|
+
"publint": "^0.3.25",
|
|
58
|
+
"react": "^19",
|
|
59
|
+
"react-dom": "^19",
|
|
60
|
+
"react-hook-form": "^7.92.0",
|
|
61
|
+
"tsdown": "^0.23.7",
|
|
62
|
+
"typescript": "^7.0.2",
|
|
63
|
+
"vitest": "^5.1.0",
|
|
64
|
+
"zod": "^4.6.7"
|
|
65
|
+
},
|
|
66
|
+
"homepage": "https://ing-eddy.github.io/aureus-frontend-core/",
|
|
67
|
+
"bugs": {
|
|
68
|
+
"url": "https://github.com/Ing-Eddy/aureus-frontend-core/issues"
|
|
69
|
+
},
|
|
70
|
+
"keywords": [
|
|
71
|
+
"react",
|
|
72
|
+
"hooks",
|
|
73
|
+
"react-hook-form",
|
|
74
|
+
"zod",
|
|
75
|
+
"forms"
|
|
76
|
+
],
|
|
77
|
+
"scripts": {
|
|
78
|
+
"build": "tsdown",
|
|
79
|
+
"dev": "tsdown --watch",
|
|
80
|
+
"typecheck": "tsc --noEmit",
|
|
81
|
+
"test": "vitest run",
|
|
82
|
+
"check:package": "node scripts/check-package.mjs"
|
|
83
|
+
}
|
|
6
84
|
}
|