@cedarjs/forms 6.0.3-next.227 → 6.0.3-next.296

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":"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;AA2JtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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) {
@@ -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.3-next.227",
3
+ "version": "6.0.3-next.296",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -36,8 +36,8 @@
36
36
  "react-hook-form": "7.88.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@cedarjs/framework-tools": "6.0.3-next.227",
40
- "@testing-library/dom": "10.4.1",
39
+ "@cedarjs/framework-tools": "6.0.3-next.296",
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",