@cedarjs/forms 6.0.2 → 6.0.3-next.275

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.
@@ -1,9 +1,35 @@
1
1
  import React from 'react';
2
+ export interface FieldErrorRenderProps {
3
+ /**
4
+ * The error message. When the failed validation rule has no message of its
5
+ * own this is the default message for the error's `type`.
6
+ */
7
+ message: string;
8
+ /**
9
+ * The message of every failed validation rule, keyed by rule. Only set when
10
+ * the form collects all errors: `<Form config={{ criteriaMode: 'all' }}>`.
11
+ */
12
+ messages?: Record<string, string | string[]>;
13
+ /**
14
+ * What caused the error: the name of the validation rule (`required`,
15
+ * `minLength`, ...), the key of the failing function in a `validate`
16
+ * object, `server` for errors from the API side, or whatever `type`
17
+ * `setError` was called with.
18
+ */
19
+ type?: string | number;
20
+ }
2
21
  export interface FieldErrorProps extends React.ComponentPropsWithoutRef<'span'> {
3
22
  /**
4
23
  * The name of the field the `<FieldError>`'s associated with.
24
+ * Use `root.<key>` for a form-level error set with
25
+ * `setError('root.<key>', ...)`.
5
26
  */
6
27
  name: string;
28
+ /**
29
+ * Render something other than a `<span>` with the message in it. Only
30
+ * called when there's an error.
31
+ */
32
+ render?: (props: FieldErrorRenderProps) => React.ReactNode;
7
33
  }
8
34
  /**
9
35
  * Renders a `<span>` with an error message if there's a validation error on the corresponding field.
@@ -25,6 +51,22 @@ export interface FieldErrorProps extends React.ComponentPropsWithoutRef<'span'>
25
51
  * <FieldError name="name" className="error" />
26
52
  * ```
27
53
  *
54
+ * @example Rendering every failed rule's message with `render`
55
+ *
56
+ * ```jsx
57
+ * <Form config={{ criteriaMode: 'all' }}>
58
+ * <FieldError
59
+ * name="password"
60
+ * render={({ messages }) => (
61
+ * <ul>
62
+ * {Object.entries(messages).map(([type, message]) => (
63
+ * <li key={type}>{message}</li>
64
+ * ))}
65
+ * </ul>
66
+ * )}
67
+ * />
68
+ * ```
69
+ *
28
70
  * @see {@link https://cedarjs.com/docs/tutorial/chapter3/forms#fielderror}
29
71
  *
30
72
  * @privateRemarks
@@ -39,5 +81,5 @@ export interface FieldErrorProps extends React.ComponentPropsWithoutRef<'span'>
39
81
  *
40
82
  * @see {@link https://react-hook-form.com/get-started#Handleerrors}
41
83
  */
42
- export declare const FieldError: ({ name, ...rest }: FieldErrorProps) => React.JSX.Element | null;
84
+ export declare const FieldError: ({ name, render, ...rest }: FieldErrorProps) => string | number | bigint | boolean | Iterable<React.ReactNode> | Promise<string | number | bigint | boolean | React.ReactPortal | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | null | undefined> | React.JSX.Element | null | undefined;
43
85
  //# sourceMappingURL=FieldError.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"FieldError.d.ts","sourceRoot":"","sources":["../src/FieldError.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,wBAAwB,CAAC,MAAM,CAAC;IAC7E;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;CACb;AAYD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,eAAO,MAAM,UAAU,GAAI,mBAAmB,eAAe,6BAe5D,CAAA"}
1
+ {"version":3,"file":"FieldError.d.ts","sourceRoot":"","sources":["../src/FieldError.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAQzB,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAA;IAC5C;;;;;OAKG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CACvB;AAED,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,wBAAwB,CAAC,MAAM,CAAC;IAC7E;;;;OAIG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,qBAAqB,KAAK,KAAK,CAAC,SAAS,CAAA;CAC3D;AAYD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,eAAO,MAAM,UAAU,GAAI,2BAA2B,eAAe,oSAsDpE,CAAA"}
@@ -9,13 +9,41 @@ const DEFAULT_MESSAGES = {
9
9
  max: "is too high",
10
10
  validate: "is not valid"
11
11
  };
12
- const FieldError = ({ name, ...rest }) => {
12
+ const FieldError = ({ name, render, ...rest }) => {
13
13
  const {
14
14
  formState: { errors }
15
15
  } = useFormContext();
16
16
  const validationError = get(errors, name);
17
- const errorMessage = validationError && (validationError.message || `${name} ${DEFAULT_MESSAGES[validationError.type]}`);
18
- return validationError ? /* @__PURE__ */ React.createElement("span", { ...rest }, errorMessage) : null;
17
+ if (!validationError) {
18
+ return null;
19
+ }
20
+ const isRootError = name === "root" || name.startsWith("root.");
21
+ const defaultMessages = DEFAULT_MESSAGES;
22
+ const getDefaultMessage = (type) => {
23
+ if (isRootError) {
24
+ return "";
25
+ }
26
+ return `${name} ${defaultMessages[String(type)] ?? DEFAULT_MESSAGES.validate}`;
27
+ };
28
+ const message = validationError.message || getDefaultMessage(validationError.type);
29
+ if (render) {
30
+ const toMessage = (type, result) => {
31
+ if (Array.isArray(result) || typeof result === "string" && result) {
32
+ return result;
33
+ }
34
+ return getDefaultMessage(type);
35
+ };
36
+ const messages = validationError.types && Object.fromEntries(
37
+ Object.entries(validationError.types).map(
38
+ ([type, result]) => [
39
+ type,
40
+ toMessage(type, result)
41
+ ]
42
+ )
43
+ );
44
+ return render({ message, messages, type: validationError.type });
45
+ }
46
+ return message ? /* @__PURE__ */ React.createElement("span", { ...rest }, message) : null;
19
47
  };
20
48
  export {
21
49
  FieldError
@@ -1 +1 @@
1
- {"version":3,"file":"Form.d.ts","sourceRoot":"","sources":["../src/Form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WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,CACjC,SAAQ,IAAI,CAAC,KAAK,CAAC,qBAAqB,CAAC,MAAM,CAAC,EAAE,UAAU,CAAC;IAC7D,KAAK,CAAC,EAAE,GAAG,CAAA;IACX;;;;;;;;;;;;;;;;;;;OAmBG;IACH,WAAW,CAAC,EAAE,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,CAAA;IACvE;;;;;;;;;;OAUG;IACH,MAAM,CAAC,EAAE,YAAY,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,CAAA;IACjE,QAAQ,CAAC,EAAE,CACT,KAAK,EAAE,kBAAkB,EACzB,KAAK,CAAC,EAAE,KAAK,CAAC,kBAAkB,KAC7B,IAAI,CAAA;CACV;AA2DD,eAAO,MAAM,IAAI,EAA4B,CAC3C,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EAEjC,KAAK,EAAE,SAAS,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,GAC1D,KAAK,CAAC,aAAa,CAAC,eAAe,CAAC,KACnC,KAAK,CAAC,YAAY,GAAG,IAAI,CAAA"}
package/dist/Form.js CHANGED
@@ -25,7 +25,12 @@ function FormInner({
25
25
  /* @__PURE__ */ React.createElement(
26
26
  ServerErrorsContext.Provider,
27
27
  {
28
- value: errorProps?.graphQLErrors?.[0]?.extensions?.properties?.messages || {}
28
+ value: (
29
+ // Apollo Client 4 stores GraphQL errors in `errors`
30
+ // (`CombinedGraphQLErrors`); `graphQLErrors` is the Apollo Client 3
31
+ // shape, kept for tests, Storybook mocks and other GraphQL clients
32
+ errorProps?.graphQLErrors?.[0]?.extensions?.properties?.messages || errorProps?.errors?.[0]?.extensions?.properties?.messages || {}
33
+ )
29
34
  },
30
35
  /* @__PURE__ */ React.createElement(FormProvider, { ...formMethods }, children)
31
36
  )
@@ -12,8 +12,16 @@ export interface ServerError extends Error {
12
12
  }
13
13
  export interface RWGqlError {
14
14
  message: string;
15
- graphQLErrors: readonly GraphQLFormattedError[];
16
- networkError: Error | ServerParseError | ServerError | null;
15
+ /**
16
+ * GraphQL errors as exposed by Apollo Client 4's `CombinedGraphQLErrors`
17
+ */
18
+ errors?: readonly GraphQLFormattedError[];
19
+ /**
20
+ * GraphQL errors in the Apollo Client 3 shape. Kept so tests, Storybook
21
+ * mocks and other GraphQL clients that use this shape keep working
22
+ */
23
+ graphQLErrors?: readonly GraphQLFormattedError[];
24
+ networkError?: Error | ServerParseError | ServerError | null;
17
25
  }
18
26
  export type RwGqlErrorProperties = Record<string, Record<string, string[]>>;
19
27
  interface FormErrorProps {
@@ -1 +1 @@
1
- {"version":3,"file":"FormError.d.ts","sourceRoot":"","sources":["../src/FormError.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAEpD,MAAM,WAAW,gBAAiB,SAAQ,KAAK;IAC7C,QAAQ,EAAE,QAAQ,CAAA;IAClB,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,WAAY,SAAQ,KAAK;IACxC,QAAQ,EAAE,QAAQ,CAAA;IAClB,UAAU,EAAE,MAAM,CAAA;IAClB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC5B;AAED,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,aAAa,EAAE,SAAS,qBAAqB,EAAE,CAAA;IAC/C,YAAY,EAAE,KAAK,GAAG,gBAAgB,GAAG,WAAW,GAAG,IAAI,CAAA;CAC5D;AAED,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAA;AA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;AAED,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAA;AAE3E,UAAU,cAAc;IACtB,KAAK,CAAC,EAAE,UAAU,CAAA;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,YAAY,CAAC,EAAE,KAAK,CAAC,aAAa,CAAA;IAClC,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,UAAU,CAAC,EAAE,KAAK,CAAC,aAAa,CAAA;IAChC,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,SAAS,CAAC,EAAE,KAAK,CAAC,aAAa,CAAA;IAC/B,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,aAAa,CAAC,EAAE,KAAK,CAAC,aAAa,CAAA;CACpC;AAED;;;GAGG;AACH,QAAA,MAAM,SAAS,GAAI,oIAUhB,cAAc,6BAmFhB,CAAA;AAED,eAAe,SAAS,CAAA"}
package/dist/FormError.js CHANGED
@@ -15,14 +15,21 @@ const FormError = ({
15
15
  }
16
16
  let rootMessage = error.message;
17
17
  const messages = [];
18
- const hasGraphQLError = !!error.graphQLErrors?.[0];
19
- const hasNetworkError = !!error.networkError && Object.keys(error.networkError).length > 0;
18
+ const graphQLErrors = error.graphQLErrors?.length ? error.graphQLErrors : error.errors ?? [];
19
+ const hasGraphQLError = !!graphQLErrors[0];
20
+ const networkError = error.networkError ?? ("bodyText" in error || "result" in error ? (
21
+ // `RWGqlError` is a loose interface over what GraphQL clients throw;
22
+ // when the object has server-error fields the error itself is the
23
+ // network error
24
+ error
25
+ ) : null);
26
+ const hasNetworkError = !!networkError && Object.keys(networkError).length > 0;
20
27
  if (hasGraphQLError) {
21
- rootMessage = error.graphQLErrors[0].message ?? "Something went wrong";
22
- if (error.graphQLErrors[0]?.extensions?.code === "BAD_USER_INPUT") {
28
+ rootMessage = graphQLErrors[0].message ?? "Something went wrong";
29
+ if (graphQLErrors[0]?.extensions?.code === "BAD_USER_INPUT") {
23
30
  rootMessage = "Errors prevented this form from being saved";
24
31
  }
25
- const properties = error.graphQLErrors[0].extensions?.["properties"];
32
+ const properties = graphQLErrors[0].extensions?.["properties"];
26
33
  const propertyMessages = properties?.["messages"];
27
34
  if (propertyMessages) {
28
35
  for (const e in propertyMessages) {
@@ -33,11 +40,11 @@ const FormError = ({
33
40
  }
34
41
  } else if (hasNetworkError) {
35
42
  rootMessage = rootMessage ?? "An error has occurred";
36
- if (Object.prototype.hasOwnProperty.call(error.networkError, "bodyText")) {
37
- const netErr = error.networkError;
43
+ if ("bodyText" in networkError) {
44
+ const netErr = networkError;
38
45
  messages.push(`${netErr.name}: ${netErr.bodyText}`);
39
- } else if (Object.prototype.hasOwnProperty.call(error.networkError, "result")) {
40
- const netErr = error.networkError;
46
+ } else if ("result" in networkError) {
47
+ const netErr = networkError;
41
48
  netErr.result.errors?.forEach((error2) => {
42
49
  if (typeof error2.message === "string") {
43
50
  if (error2.message.indexOf(";") >= 0) {
package/dist/Submit.d.ts CHANGED
@@ -10,7 +10,5 @@ import React from 'react';
10
10
  * </Form>
11
11
  * ```
12
12
  */
13
- export declare const Submit: React.ForwardRefExoticComponent<Omit<Omit<Omit<React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "ref"> & {
14
- ref?: ((instance: HTMLButtonElement | null) => void) | React.RefObject<HTMLButtonElement> | null | undefined;
15
- }, "type">, "ref"> & React.RefAttributes<HTMLButtonElement>>;
13
+ export declare const Submit: React.ForwardRefExoticComponent<Omit<Omit<React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "type">, "ref"> & React.RefAttributes<HTMLButtonElement>>;
16
14
  //# sourceMappingURL=Submit.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Submit.d.ts","sourceRoot":"","sources":["../src/Submit.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAqB,MAAM,OAAO,CAAA;AAEzC;;;;;;;;;;GAUG;AACH,eAAO,MAAM,MAAM;;4DAG6C,CAAA"}
1
+ {"version":3,"file":"Submit.d.ts","sourceRoot":"","sources":["../src/Submit.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAqB,MAAM,OAAO,CAAA;AAEzC;;;;;;;;;;GAUG;AACH,eAAO,MAAM,MAAM,gMAG6C,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"coercion.d.ts","sourceRoot":"","sources":["../src/coercion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD;;;;;;GAMG;AACH,MAAM,MAAM,sBAAsB,GAAG,eAAe,GAAG;IACrD,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAKD;;;;;;GAMG;AAEH,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,WAAW,GAAG,CAAC,GAAG,EAAE,CAAA;AA8JtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,UAAU,gBAAgB;IACxB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,CAAC,EAAE,YAAY,CAAA;CACvB;AAED,eAAO,MAAM,WAAW,GACtB,YAAY,sBAAsB,EAClC,yBAAyB,gBAAgB,SAyC1C,CAAA"}
1
+ {"version":3,"file":"coercion.d.ts","sourceRoot":"","sources":["../src/coercion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,eAAe,EAAY,MAAM,iBAAiB,CAAA;AAE7E;;;;;;GAMG;AACH,MAAM,MAAM,sBAAsB,GAAG,eAAe,GAAG;IACrD,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAKD;;;;;;GAMG;AAEH,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,WAAW,GAAG,CAAC,GAAG,EAAE,CAAA;AA2KtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,UAAU,gBAAgB;IACxB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,CAAC,EAAE,YAAY,CAAA;CACvB;AAED,eAAO,MAAM,WAAW,GACtB,YAAY,sBAAsB,EAClC,yBAAyB,gBAAgB,SAyE1C,CAAA"}
package/dist/coercion.js CHANGED
@@ -117,6 +117,7 @@ const getSetValueAsFn = (type, emptyAs, required, isId) => {
117
117
  return fn;
118
118
  };
119
119
  const JSONValidation = (val) => typeof val === "number" ? !isNaN(val) : true;
120
+ const JSON_VALIDATION_KEY = "validJSON";
120
121
  const setCoercion = (validation, { type, name, emptyAs }) => {
121
122
  if (validation.setValueAs) {
122
123
  return;
@@ -125,7 +126,24 @@ const setCoercion = (validation, { type, name, emptyAs }) => {
125
126
  if (validation.valueAsBoolean || type === "checkbox") {
126
127
  return;
127
128
  } else if (validation.valueAsJSON) {
128
- validation.validate = JSONValidation;
129
+ const userValidate = validation.validate;
130
+ const userValidators = typeof userValidate === "function" ? { validate: userValidate } : { ...userValidate };
131
+ let jsonValidationKey = JSON_VALIDATION_KEY;
132
+ while (jsonValidationKey in userValidators) {
133
+ jsonValidationKey = `_${jsonValidationKey}`;
134
+ }
135
+ const guardedValidators = Object.fromEntries(
136
+ Object.entries(userValidators).map(
137
+ ([key, validator]) => [
138
+ key,
139
+ (value, formValues) => JSONValidation(value) ? validator(value, formValues) : true
140
+ ]
141
+ )
142
+ );
143
+ validation.validate = {
144
+ [jsonValidationKey]: JSONValidation,
145
+ ...guardedValidators
146
+ };
129
147
  delete validation.valueAsJSON;
130
148
  valueAs = "valueAsJSON";
131
149
  } else if (type === "date" || type === "datetime-local" || validation.valueAsDate) {
@@ -19,7 +19,7 @@ export declare const useRegister: <T, Element extends HTMLTextAreaElement | HTML
19
19
  }, ref?: React.ForwardedRef<T>, emptyAs?: EmptyAsValue) => {
20
20
  ref: (element: T) => void;
21
21
  onBlur: React.FocusEventHandler<Element>;
22
- onChange: React.ChangeEventHandler<Element>;
22
+ onChange: React.ChangeEventHandler<Element, globalThis.Element>;
23
23
  name: string;
24
24
  min?: string | number;
25
25
  max?: string | number;
@@ -1 +1 @@
1
- {"version":3,"file":"useRegister.d.ts","sourceRoot":"","sources":["../src/useRegister.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAI9B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAEjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,MAAM,MAAM,gBAAgB,CAC1B,OAAO,SAAS,mBAAmB,GAAG,iBAAiB,GAAG,gBAAgB,GACxE,gBAAgB,IAChB,IAAI,CACN,UAAU,CAAC,OAAO,CAAC,EACnB,MAAM,GAAG,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,UAAU,CACvD,CAAA;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,WAAW,GACtB,CAAC,EACD,OAAO,SAAS,mBAAmB,GAAG,iBAAiB,GAAG,gBAAgB,GACxE,gBAAgB,EAElB,OAAO,gBAAgB,CAAC,OAAO,CAAC,GAAG;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,EACvD,MAAM,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,EAC3B,UAAU,YAAY;mBAmCL,CAAC;;;;;;;;;;;CAYnB,CAAA"}
1
+ {"version":3,"file":"useRegister.d.ts","sourceRoot":"","sources":["../src/useRegister.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAI9B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAEjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,MAAM,MAAM,gBAAgB,CAC1B,OAAO,SAAS,mBAAmB,GAAG,iBAAiB,GAAG,gBAAgB,GACxE,gBAAgB,IAChB,IAAI,CACN,UAAU,CAAC,OAAO,CAAC,EACnB,MAAM,GAAG,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,UAAU,CACvD,CAAA;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,WAAW,GACtB,CAAC,EACD,OAAO,SAAS,mBAAmB,GAAG,iBAAiB,GAAG,gBAAgB,GACxE,gBAAgB,EAElB,OAAO,gBAAgB,CAAC,OAAO,CAAC,GAAG;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,EACvD,MAAM,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,EAC3B,UAAU,YAAY;mBAqCL,CAAC;;;;;;;;;;;CAYnB,CAAA"}
@@ -6,7 +6,7 @@ const useRegister = (props, ref, emptyAs) => {
6
6
  if (!name) {
7
7
  throw Error("`name` prop must be provided");
8
8
  }
9
- const validation = props.validation || { required: false };
9
+ const validation = { ...props.validation || { required: false } };
10
10
  setCoercion(validation, {
11
11
  type: props.type,
12
12
  name,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/forms",
3
- "version": "6.0.2",
3
+ "version": "6.0.3-next.275",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -33,27 +33,27 @@
33
33
  "dependencies": {
34
34
  "graphql": "16.14.2",
35
35
  "pascalcase": "1.0.0",
36
- "react-hook-form": "7.87.0"
36
+ "react-hook-form": "7.88.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@cedarjs/framework-tools": "6.0.2",
40
- "@testing-library/dom": "10.4.1",
39
+ "@cedarjs/framework-tools": "6.0.3-next.275",
40
+ "@testing-library/dom": "10.4.2",
41
41
  "@testing-library/jest-dom": "6.9.1",
42
42
  "@testing-library/react": "16.3.3",
43
43
  "@testing-library/user-event": "14.6.7",
44
44
  "@types/pascalcase": "1.0.3",
45
- "@types/react": "^18.2.55",
46
- "@types/react-dom": "^18.2.19",
45
+ "@types/react": "^19.2.0",
46
+ "@types/react-dom": "^19.2.0",
47
47
  "concurrently": "9.2.4",
48
48
  "nodemon": "3.1.14",
49
49
  "publint": "0.3.24",
50
- "react": "18.3.1",
51
- "react-dom": "18.3.1",
50
+ "react": "19.3.0",
51
+ "react-dom": "19.3.0",
52
52
  "typescript": "5.9.3",
53
53
  "vitest": "4.1.11"
54
54
  },
55
55
  "peerDependencies": {
56
- "react": "18.3.1"
56
+ "react": "19.3.0"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">=24"