@hookform/resolvers 3.4.1 → 3.5.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.
@@ -1,6 +1,6 @@
1
1
  import { FieldValues, ResolverResult, ResolverOptions } from 'react-hook-form';
2
2
  import { Type } from 'arktype';
3
- export type Resolver = <T extends Type<any>>(schema: T, schemaOptions?: never, factoryOptions?: {
3
+ export type Resolver = <T extends Type<any>>(schema: T, schemaOptions?: undefined, factoryOptions?: {
4
4
  /**
5
5
  * Return the raw input values rather than the parsed values.
6
6
  * @default false
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hookform/resolvers/arktype",
3
3
  "amdName": "hookformResolversArktype",
4
- "version": "1.0.0",
4
+ "version": "2.0.0",
5
5
  "private": true,
6
6
  "description": "React Hook Form validation resolver: arktype",
7
7
  "main": "dist/arktype.js",
@@ -57,14 +57,14 @@ test("form's native validation with Zod", async () => {
57
57
  usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
58
58
  expect(usernameField.validity.valid).toBe(false);
59
59
  expect(usernameField.validationMessage).toBe(
60
- 'username must be more than 1 characters (was 0)',
60
+ 'username must be more than length 1 (was 0)',
61
61
  );
62
62
 
63
63
  // password
64
64
  passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
65
65
  expect(passwordField.validity.valid).toBe(false);
66
66
  expect(passwordField.validationMessage).toBe(
67
- 'password must be more than 1 characters (was 0)',
67
+ 'password must be more than length 1 (was 0)',
68
68
  );
69
69
 
70
70
  await user.type(screen.getByPlaceholderText(/username/i), 'joe');
@@ -47,10 +47,10 @@ test("form's validation with arkType and TypeScript's integration", async () =>
47
47
  await user.click(screen.getByText(/submit/i));
48
48
 
49
49
  expect(
50
- screen.getByText('username must be more than 1 characters (was 0)'),
50
+ screen.getByText('username must be more than length 1 (was 0)'),
51
51
  ).toBeInTheDocument();
52
52
  expect(
53
- screen.getByText('password must be more than 1 characters (was 0)'),
53
+ screen.getByText('password must be more than length 1 (was 0)'),
54
54
  ).toBeInTheDocument();
55
55
  expect(handleSubmit).not.toHaveBeenCalled();
56
56
  });
@@ -1,22 +1,20 @@
1
1
  import { Field, InternalFieldName } from 'react-hook-form';
2
- import { type, arrayOf, union } from 'arktype';
2
+ import { type } from 'arktype';
3
3
 
4
4
  export const schema = type({
5
5
  username: 'string>2',
6
- password: union(['string>8', '&', '/.*[A-Za-z].*/'], ['/.*\\d.*/']),
6
+ password: '/.*[A-Za-z].*/>8|/.*\\d.*/',
7
7
  repeatPassword: 'string>1',
8
- accessToken: union('string', 'number'),
8
+ accessToken: 'string|number',
9
9
  birthYear: '1900<number<2013',
10
10
  email: 'email',
11
- tags: arrayOf('string'),
11
+ tags: 'string[]',
12
12
  enabled: 'boolean',
13
13
  url: 'string>1',
14
- 'like?': arrayOf(
15
- type({
16
- id: 'number',
17
- name: 'string>3',
18
- }),
19
- ),
14
+ 'like?': type({
15
+ id: 'number',
16
+ name: 'string>3',
17
+ }).array(),
20
18
  dateStr: 'Date',
21
19
  });
22
20
 
@@ -3,72 +3,463 @@
3
3
  exports[`arktypeResolver > should return a single error from arktypeResolver when validation fails 1`] = `
4
4
  {
5
5
  "errors": {
6
- "accessToken": {
7
- "message": "accessToken must be defined",
6
+ "accessToken": ArkError {
7
+ "code": "required",
8
+ "data": {
9
+ "birthYear": "birthYear",
10
+ "email": "",
11
+ "like": [
12
+ {
13
+ "id": "z",
14
+ },
15
+ ],
16
+ "password": "___",
17
+ "url": "abc",
18
+ },
19
+ "input": {
20
+ "code": "required",
21
+ "missingValueDescription": "a number or a string",
22
+ "relativePath": [
23
+ "accessToken",
24
+ ],
25
+ },
26
+ "missingValueDescription": "a number or a string",
27
+ "nodeConfig": {
28
+ "actual": [Function],
29
+ "description": [Function],
30
+ "expected": [Function],
31
+ "message": [Function],
32
+ "problem": [Function],
33
+ },
34
+ "path": [
35
+ "accessToken",
36
+ ],
8
37
  "ref": undefined,
9
- "type": "missing",
38
+ "relativePath": [
39
+ "accessToken",
40
+ ],
41
+ "type": "required",
42
+ Symbol(ArkTypeInternalKind): "error",
10
43
  },
11
- "birthYear": {
12
- "message": "birthYear must be a number (was string)",
44
+ "birthYear": ArkError {
45
+ "code": "domain",
46
+ "data": "birthYear",
47
+ "description": "a number",
48
+ "domain": "number",
49
+ "input": {
50
+ "code": "domain",
51
+ "description": "a number",
52
+ "domain": "number",
53
+ },
54
+ "nodeConfig": {
55
+ "actual": [Function],
56
+ "description": [Function],
57
+ "expected": [Function],
58
+ "message": [Function],
59
+ "problem": [Function],
60
+ },
61
+ "path": [
62
+ "birthYear",
63
+ ],
13
64
  "ref": undefined,
14
65
  "type": "domain",
66
+ Symbol(ArkTypeInternalKind): "error",
15
67
  },
16
- "dateStr": {
17
- "message": "dateStr must be defined",
68
+ "dateStr": ArkError {
69
+ "code": "required",
70
+ "data": {
71
+ "birthYear": "birthYear",
72
+ "email": "",
73
+ "like": [
74
+ {
75
+ "id": "z",
76
+ },
77
+ ],
78
+ "password": "___",
79
+ "url": "abc",
80
+ },
81
+ "input": {
82
+ "code": "required",
83
+ "missingValueDescription": "a Date",
84
+ "relativePath": [
85
+ "dateStr",
86
+ ],
87
+ },
88
+ "missingValueDescription": "a Date",
89
+ "nodeConfig": {
90
+ "actual": [Function],
91
+ "description": [Function],
92
+ "expected": [Function],
93
+ "message": [Function],
94
+ "problem": [Function],
95
+ },
96
+ "path": [
97
+ "dateStr",
98
+ ],
18
99
  "ref": undefined,
19
- "type": "missing",
100
+ "relativePath": [
101
+ "dateStr",
102
+ ],
103
+ "type": "required",
104
+ Symbol(ArkTypeInternalKind): "error",
20
105
  },
21
- "email": {
22
- "message": "email must be a valid email (was '')",
106
+ "email": ArkError {
107
+ "code": "regex",
108
+ "data": "",
109
+ "description": "a valid email",
110
+ "flags": "",
111
+ "input": {
112
+ "code": "regex",
113
+ "description": "a valid email",
114
+ "flags": "",
115
+ "rule": "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\\\.[A-Za-z]{2,}$",
116
+ },
117
+ "nodeConfig": {
118
+ "actual": [Function],
119
+ "description": [Function],
120
+ "expected": [Function],
121
+ "message": [Function],
122
+ "problem": [Function],
123
+ },
124
+ "path": [
125
+ "email",
126
+ ],
23
127
  "ref": {
24
128
  "name": "email",
25
129
  },
130
+ "rule": "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\\\.[A-Za-z]{2,}$",
26
131
  "type": "regex",
132
+ Symbol(ArkTypeInternalKind): "error",
27
133
  },
28
- "enabled": {
29
- "message": "enabled must be defined",
134
+ "enabled": ArkError {
135
+ "code": "required",
136
+ "data": {
137
+ "birthYear": "birthYear",
138
+ "email": "",
139
+ "like": [
140
+ {
141
+ "id": "z",
142
+ },
143
+ ],
144
+ "password": "___",
145
+ "url": "abc",
146
+ },
147
+ "input": {
148
+ "code": "required",
149
+ "missingValueDescription": "boolean",
150
+ "relativePath": [
151
+ "enabled",
152
+ ],
153
+ },
154
+ "missingValueDescription": "boolean",
155
+ "nodeConfig": {
156
+ "actual": [Function],
157
+ "description": [Function],
158
+ "expected": [Function],
159
+ "message": [Function],
160
+ "problem": [Function],
161
+ },
162
+ "path": [
163
+ "enabled",
164
+ ],
30
165
  "ref": undefined,
31
- "type": "missing",
166
+ "relativePath": [
167
+ "enabled",
168
+ ],
169
+ "type": "required",
170
+ Symbol(ArkTypeInternalKind): "error",
32
171
  },
33
172
  "like": [
34
173
  {
35
- "id": {
36
- "message": "like/0/id must be a number (was string)",
174
+ "id": ArkError {
175
+ "code": "domain",
176
+ "data": "z",
177
+ "description": "a number",
178
+ "domain": "number",
179
+ "input": {
180
+ "code": "domain",
181
+ "description": "a number",
182
+ "domain": "number",
183
+ },
184
+ "nodeConfig": {
185
+ "actual": [Function],
186
+ "description": [Function],
187
+ "expected": [Function],
188
+ "message": [Function],
189
+ "problem": [Function],
190
+ },
191
+ "path": [
192
+ "like",
193
+ 0,
194
+ "id",
195
+ ],
37
196
  "ref": undefined,
38
197
  "type": "domain",
198
+ Symbol(ArkTypeInternalKind): "error",
39
199
  },
40
- "name": {
41
- "message": "like/0/name must be defined",
200
+ "name": ArkError {
201
+ "code": "required",
202
+ "data": {
203
+ "id": "z",
204
+ },
205
+ "input": {
206
+ "code": "required",
207
+ "missingValueDescription": "a string and more than length 3",
208
+ "relativePath": [
209
+ "name",
210
+ ],
211
+ },
212
+ "missingValueDescription": "a string and more than length 3",
213
+ "nodeConfig": {
214
+ "actual": [Function],
215
+ "description": [Function],
216
+ "expected": [Function],
217
+ "message": [Function],
218
+ "problem": [Function],
219
+ },
220
+ "path": [
221
+ "like",
222
+ 0,
223
+ "name",
224
+ ],
42
225
  "ref": undefined,
43
- "type": "missing",
226
+ "relativePath": [
227
+ "name",
228
+ ],
229
+ "type": "required",
230
+ Symbol(ArkTypeInternalKind): "error",
44
231
  },
45
232
  },
46
233
  ],
47
- "password": {
48
- "message": "At password, '___' must be...
49
- more than 8 characters
50
- a string matching /.*[A-Za-z].*/",
234
+ "password": ArkError {
235
+ "code": "union",
236
+ "data": "___",
237
+ "errors": [
238
+ ArkError {
239
+ "code": "regex",
240
+ "data": "___",
241
+ "description": "matched by .*[A-Za-z].*",
242
+ "input": {
243
+ "code": "regex",
244
+ "description": "matched by .*[A-Za-z].*",
245
+ "rule": ".*[A-Za-z].*",
246
+ },
247
+ "nodeConfig": {
248
+ "actual": [Function],
249
+ "description": [Function],
250
+ "expected": [Function],
251
+ "message": [Function],
252
+ "problem": [Function],
253
+ },
254
+ "path": [
255
+ "password",
256
+ ],
257
+ "rule": ".*[A-Za-z].*",
258
+ Symbol(ArkTypeInternalKind): "error",
259
+ },
260
+ ArkError {
261
+ "code": "regex",
262
+ "data": "___",
263
+ "description": "matched by .*\\\\d.*",
264
+ "input": {
265
+ "code": "regex",
266
+ "description": "matched by .*\\\\d.*",
267
+ "rule": ".*\\\\d.*",
268
+ },
269
+ "nodeConfig": {
270
+ "actual": [Function],
271
+ "description": [Function],
272
+ "expected": [Function],
273
+ "message": [Function],
274
+ "problem": [Function],
275
+ },
276
+ "path": [
277
+ "password",
278
+ ],
279
+ "rule": ".*\\\\d.*",
280
+ Symbol(ArkTypeInternalKind): "error",
281
+ },
282
+ ],
283
+ "input": {
284
+ "code": "union",
285
+ "errors": [
286
+ ArkError {
287
+ "code": "regex",
288
+ "data": "___",
289
+ "description": "matched by .*[A-Za-z].*",
290
+ "input": {
291
+ "code": "regex",
292
+ "description": "matched by .*[A-Za-z].*",
293
+ "rule": ".*[A-Za-z].*",
294
+ },
295
+ "nodeConfig": {
296
+ "actual": [Function],
297
+ "description": [Function],
298
+ "expected": [Function],
299
+ "message": [Function],
300
+ "problem": [Function],
301
+ },
302
+ "path": [
303
+ "password",
304
+ ],
305
+ "rule": ".*[A-Za-z].*",
306
+ Symbol(ArkTypeInternalKind): "error",
307
+ },
308
+ ArkError {
309
+ "code": "regex",
310
+ "data": "___",
311
+ "description": "matched by .*\\\\d.*",
312
+ "input": {
313
+ "code": "regex",
314
+ "description": "matched by .*\\\\d.*",
315
+ "rule": ".*\\\\d.*",
316
+ },
317
+ "nodeConfig": {
318
+ "actual": [Function],
319
+ "description": [Function],
320
+ "expected": [Function],
321
+ "message": [Function],
322
+ "problem": [Function],
323
+ },
324
+ "path": [
325
+ "password",
326
+ ],
327
+ "rule": ".*\\\\d.*",
328
+ Symbol(ArkTypeInternalKind): "error",
329
+ },
330
+ ],
331
+ },
332
+ "nodeConfig": {
333
+ "actual": [Function],
334
+ "description": [Function],
335
+ "expected": [Function],
336
+ "message": [Function],
337
+ "problem": [Function],
338
+ },
339
+ "path": [
340
+ "password",
341
+ ],
51
342
  "ref": {
52
343
  "name": "password",
53
344
  },
54
- "type": "multi",
345
+ "type": "union",
346
+ Symbol(ArkTypeInternalKind): "error",
55
347
  },
56
- "repeatPassword": {
57
- "message": "repeatPassword must be defined",
348
+ "repeatPassword": ArkError {
349
+ "code": "required",
350
+ "data": {
351
+ "birthYear": "birthYear",
352
+ "email": "",
353
+ "like": [
354
+ {
355
+ "id": "z",
356
+ },
357
+ ],
358
+ "password": "___",
359
+ "url": "abc",
360
+ },
361
+ "input": {
362
+ "code": "required",
363
+ "missingValueDescription": "a string and more than length 1",
364
+ "relativePath": [
365
+ "repeatPassword",
366
+ ],
367
+ },
368
+ "missingValueDescription": "a string and more than length 1",
369
+ "nodeConfig": {
370
+ "actual": [Function],
371
+ "description": [Function],
372
+ "expected": [Function],
373
+ "message": [Function],
374
+ "problem": [Function],
375
+ },
376
+ "path": [
377
+ "repeatPassword",
378
+ ],
58
379
  "ref": undefined,
59
- "type": "missing",
380
+ "relativePath": [
381
+ "repeatPassword",
382
+ ],
383
+ "type": "required",
384
+ Symbol(ArkTypeInternalKind): "error",
60
385
  },
61
- "tags": {
62
- "message": "tags must be defined",
386
+ "tags": ArkError {
387
+ "code": "required",
388
+ "data": {
389
+ "birthYear": "birthYear",
390
+ "email": "",
391
+ "like": [
392
+ {
393
+ "id": "z",
394
+ },
395
+ ],
396
+ "password": "___",
397
+ "url": "abc",
398
+ },
399
+ "input": {
400
+ "code": "required",
401
+ "missingValueDescription": "string[]",
402
+ "relativePath": [
403
+ "tags",
404
+ ],
405
+ },
406
+ "missingValueDescription": "string[]",
407
+ "nodeConfig": {
408
+ "actual": [Function],
409
+ "description": [Function],
410
+ "expected": [Function],
411
+ "message": [Function],
412
+ "problem": [Function],
413
+ },
414
+ "path": [
415
+ "tags",
416
+ ],
63
417
  "ref": undefined,
64
- "type": "missing",
418
+ "relativePath": [
419
+ "tags",
420
+ ],
421
+ "type": "required",
422
+ Symbol(ArkTypeInternalKind): "error",
65
423
  },
66
- "username": {
67
- "message": "username must be defined",
424
+ "username": ArkError {
425
+ "code": "required",
426
+ "data": {
427
+ "birthYear": "birthYear",
428
+ "email": "",
429
+ "like": [
430
+ {
431
+ "id": "z",
432
+ },
433
+ ],
434
+ "password": "___",
435
+ "url": "abc",
436
+ },
437
+ "input": {
438
+ "code": "required",
439
+ "missingValueDescription": "a string and more than length 2",
440
+ "relativePath": [
441
+ "username",
442
+ ],
443
+ },
444
+ "missingValueDescription": "a string and more than length 2",
445
+ "nodeConfig": {
446
+ "actual": [Function],
447
+ "description": [Function],
448
+ "expected": [Function],
449
+ "message": [Function],
450
+ "problem": [Function],
451
+ },
452
+ "path": [
453
+ "username",
454
+ ],
68
455
  "ref": {
69
456
  "name": "username",
70
457
  },
71
- "type": "missing",
458
+ "relativePath": [
459
+ "username",
460
+ ],
461
+ "type": "required",
462
+ Symbol(ArkTypeInternalKind): "error",
72
463
  },
73
464
  },
74
465
  "values": {},
@@ -1,34 +1,24 @@
1
1
  import { FieldError, FieldErrors } from 'react-hook-form';
2
2
  import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
3
3
  import type { Resolver } from './types';
4
- import { Problems } from 'arktype';
4
+ import { ArkErrors } from 'arktype';
5
5
 
6
- const parseErrorSchema = (e: Problems) => {
7
- const errors: Record<string, FieldError> = {};
8
- for (; e.length; ) {
9
- const error = e[0];
10
- const _path = error.path.join('.');
11
-
12
- if (!errors[_path]) {
13
- errors[_path] = { message: error.message, type: error.code };
14
- }
15
-
16
- // @ts-expect-error - false positive Property 'shift' does not exist on type 'Problems'.
17
- e.shift();
18
- }
19
-
20
- return errors;
6
+ const parseErrorSchema = (e: ArkErrors): Record<string, FieldError> => {
7
+ // copy code to type to match FieldError shape
8
+ e.forEach((e) => Object.assign(e, { type: e.code }));
9
+ // need to cast here because TS doesn't understand we added the type field
10
+ return e.byPath as never;
21
11
  };
22
12
 
23
13
  export const arktypeResolver: Resolver =
24
14
  (schema, _schemaOptions, resolverOptions = {}) =>
25
15
  (values, _, options) => {
26
- const result = schema(values);
16
+ const out = schema(values);
27
17
 
28
- if (result.problems) {
18
+ if (out instanceof ArkErrors) {
29
19
  return {
30
20
  values: {},
31
- errors: toNestErrors(parseErrorSchema(result.problems), options),
21
+ errors: toNestErrors(parseErrorSchema(out), options),
32
22
  };
33
23
  }
34
24
 
@@ -36,6 +26,6 @@ export const arktypeResolver: Resolver =
36
26
 
37
27
  return {
38
28
  errors: {} as FieldErrors,
39
- values: resolverOptions.raw ? values : result.data,
29
+ values: resolverOptions.raw ? values : out,
40
30
  };
41
31
  };
@@ -3,7 +3,7 @@ import { Type } from 'arktype';
3
3
 
4
4
  export type Resolver = <T extends Type<any>>(
5
5
  schema: T,
6
- schemaOptions?: never,
6
+ schemaOptions?: undefined,
7
7
  factoryOptions?: {
8
8
  /**
9
9
  * Return the raw input values rather than the parsed values.
package/dist/resolvers.js CHANGED
@@ -1,2 +1,2 @@
1
- var e=require("react-hook-form");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=/*#__PURE__*/r(require("lodash.set")),i=function(r,t,i){if(r&&"reportValidity"in r){var a=e.get(i,t);r.setCustomValidity(a&&a.message||""),r.reportValidity()}},a=function(e,r){var t=function(t){var a=r.fields[t];a&&a.ref&&"reportValidity"in a.ref?i(a.ref,t,e):a.refs&&a.refs.forEach(function(r){return i(r,t,e)})};for(var a in r.fields)t(a)},f=function(e,r){return e.some(function(e){return e.startsWith(r+".")})};exports.toNestErrors=function(r,i){i.shouldUseNativeValidation&&a(r,i);var n={};for(var o in r){var s=e.get(i.fields,o),u=Object.assign(r[o]||{},{ref:s&&s.ref});if(f(i.names||Object.keys(r),o)){var l=Object.assign({},e.get(n,o));t.default(l,"root",u),t.default(n,o,l)}else t.default(n,o,u)}return n},exports.validateFieldsNatively=a;
1
+ var r=require("react-hook-form"),e=function(e,t,i){if(e&&"reportValidity"in e){var n=r.get(i,t);e.setCustomValidity(n&&n.message||""),e.reportValidity()}},t=function(r,t){var i=function(i){var n=t.fields[i];n&&n.ref&&"reportValidity"in n.ref?e(n.ref,i,r):n.refs&&n.refs.forEach(function(t){return e(t,i,r)})};for(var n in t.fields)i(n)},i=function(r){return r instanceof Date},n=function(r){return null==r},o=function(r){return"object"==typeof r},s=function(r){return!n(r)&&!Array.isArray(r)&&o(r)&&!i(r)},a=function(r){return/^\w*$/.test(r)},f=function(r,e,t){for(var i=-1,n=a(e)?[e]:function(r){return e=r.replace(/["|']|\]/g,"").split(/\.|\[/),Array.isArray(e)?e.filter(Boolean):[];var e}(e),o=n.length,f=o-1;++i<o;){var u=n[i],c=t;if(i!==f){var l=r[u];c=s(l)||Array.isArray(l)?l:isNaN(+n[i+1])?{}:[]}r[u]=c,r=r[u]}return r},u=function(r,e){return r.some(function(r){return r.startsWith(e+".")})};exports.isDateObject=i,exports.isKey=a,exports.isNullOrUndefined=n,exports.isObject=s,exports.isObjectType=o,exports.toNestErrors=function(e,i){i.shouldUseNativeValidation&&t(e,i);var n={};for(var o in e){var s=r.get(i.fields,o),a=Object.assign(e[o]||{},{ref:s&&s.ref});if(u(i.names||Object.keys(e),o)){var c=Object.assign({},r.get(n,o));f(c,"root",a),f(n,o,c)}else f(n,o,a)}return n},exports.validateFieldsNatively=t;
2
2
  //# sourceMappingURL=resolvers.js.map