@conform-to/yup 1.0.6 → 1.1.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/README CHANGED
@@ -8,7 +8,7 @@
8
8
  ╚══════╝ ╚═════╝ ╚═╝ ╚══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
9
9
 
10
10
 
11
- Version 1.0.6 / License MIT / Copyright (c) 2024 Edmund Hung
11
+ Version 1.1.0 / License MIT / Copyright (c) 2024 Edmund Hung
12
12
 
13
13
  A type-safe form validation library utilizing web fundamentals to progressively enhance HTML Forms with full support for server frameworks like Remix and Next.js.
14
14
 
@@ -0,0 +1,3 @@
1
+ import { type Constraint } from '@conform-to/dom';
2
+ import type * as yup from 'yup';
3
+ export declare function getYupConstraint<Source extends yup.AnyObjectSchema>(source: Source): Record<string, Constraint>;
package/constraint.js ADDED
@@ -0,0 +1,193 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function _parseStringFieldTests(def) {
6
+ var constraint = {};
7
+ for (var test of def.tests) {
8
+ switch (test.name) {
9
+ case 'required':
10
+ {
11
+ constraint.required = true;
12
+ }
13
+ case 'min':
14
+ {
15
+ var _test$params;
16
+ if (!constraint.minLength && (_test$params = test.params) !== null && _test$params !== void 0 && _test$params.min) {
17
+ var _test$params2;
18
+ constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min);
19
+ }
20
+ }
21
+ case 'max':
22
+ {
23
+ var _test$params3;
24
+ if (!constraint.maxLength && (_test$params3 = test.params) !== null && _test$params3 !== void 0 && _test$params3.max) {
25
+ constraint.maxLength = Number(test.params.max);
26
+ }
27
+ }
28
+ case 'matches':
29
+ {
30
+ var _test$params4;
31
+ if (!constraint.pattern && ((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.regex) instanceof RegExp) {
32
+ constraint.pattern = test.params.regex.source;
33
+ }
34
+ }
35
+ }
36
+ }
37
+ if (!constraint.pattern && def.oneOf.length > 0) {
38
+ constraint.pattern = def.oneOf.join('|');
39
+ }
40
+ return constraint;
41
+ }
42
+ function _parseDateFieldTests(def) {
43
+ var constraint = {};
44
+ for (var test of def.tests) {
45
+ switch (test.name) {
46
+ case 'required':
47
+ constraint.required = true;
48
+ break;
49
+ case 'min':
50
+ if (!constraint.min && test.params) {
51
+ constraint.min = test.params.min;
52
+ }
53
+ break;
54
+ case 'max':
55
+ if (!constraint.max && test.params) {
56
+ constraint.max = test.params.max;
57
+ }
58
+ break;
59
+ }
60
+ }
61
+ return constraint;
62
+ }
63
+ function _parseNumberFieldTests(def) {
64
+ var _test$params5, _test$params7;
65
+ var constraint = {};
66
+ for (var test of def.tests) {
67
+ switch (test.name) {
68
+ case 'required':
69
+ constraint.required = true;
70
+ break;
71
+ case 'min':
72
+ if (typeof constraint.min === 'string') {
73
+ throw new Error('min should not be a string');
74
+ }
75
+ if (typeof constraint.min === 'object') {
76
+ throw new Error('min should be a number');
77
+ }
78
+ if (!constraint.min || constraint.min < Number((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.min)) {
79
+ var _test$params6;
80
+ constraint.min = Number((_test$params6 = test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min);
81
+ }
82
+ break;
83
+ case 'max':
84
+ if (typeof constraint.max === 'string') {
85
+ throw new Error('max should not be a number');
86
+ }
87
+ if (typeof constraint.max === 'object') {
88
+ throw new Error('min should be a number');
89
+ }
90
+ if (!constraint.max || constraint.max > Number((_test$params7 = test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.max)) {
91
+ var _test$params8;
92
+ constraint.max = Number((_test$params8 = test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max);
93
+ }
94
+ break;
95
+ }
96
+ }
97
+ return constraint;
98
+ }
99
+ function _parseObjectFieldTests(def) {
100
+ var constraint = {};
101
+ for (var test of def.tests) {
102
+ switch (test.name) {
103
+ case 'required':
104
+ constraint.required = true;
105
+ break;
106
+ }
107
+ }
108
+ return constraint;
109
+ }
110
+ function _parseYupField(_ref) {
111
+ var _test$params9, _test$params10;
112
+ var [key, def] = _ref;
113
+ var constraints = [];
114
+ switch (def.type) {
115
+ case 'object':
116
+ {
117
+ var {
118
+ fields
119
+ } = def;
120
+ Object.entries(fields).forEach(_ref2 => {
121
+ var [fieldKey, field] = _ref2;
122
+ constraints = [...constraints, ..._parseYupField(["".concat(key, ".").concat(fieldKey), field])];
123
+ });
124
+ constraints.push([key, _parseObjectFieldTests(def)]);
125
+ break;
126
+ }
127
+ case 'array':
128
+ {
129
+ var constraint = {};
130
+ constraint.multiple = true;
131
+ for (var test of def.tests) {
132
+ switch (test.name) {
133
+ case 'required':
134
+ constraint.required = true;
135
+ break;
136
+ case 'min':
137
+ if (Number((_test$params9 = test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.min) > 1) {
138
+ constraint.multiple = true;
139
+ }
140
+ break;
141
+ case 'max':
142
+ if (Number((_test$params10 = test.params) === null || _test$params10 === void 0 ? void 0 : _test$params10.max) > 1) {
143
+ constraint.multiple = true;
144
+ }
145
+ break;
146
+ }
147
+ }
148
+ var {
149
+ innerType
150
+ } = def;
151
+ if (innerType) {
152
+ switch (innerType.type) {
153
+ case 'string':
154
+ {
155
+ constraints.push(["".concat(key, "[]"), _parseStringFieldTests(innerType)]);
156
+ break;
157
+ }
158
+ case 'object':
159
+ {
160
+ constraints = constraints.concat(_parseYupField(["".concat(key, "[]"), innerType]));
161
+ break;
162
+ }
163
+ }
164
+ }
165
+ constraints.push([key, constraint]);
166
+ break;
167
+ }
168
+ case 'date':
169
+ {
170
+ constraints.push([key, _parseDateFieldTests(def)]);
171
+ break;
172
+ }
173
+ case 'string':
174
+ {
175
+ constraints = [[key, _parseStringFieldTests(def)]];
176
+ break;
177
+ }
178
+ case 'number':
179
+ {
180
+ constraints = [[key, _parseNumberFieldTests(def)]];
181
+ break;
182
+ }
183
+ }
184
+ return constraints;
185
+ }
186
+ function getYupConstraint(source) {
187
+ var description = source.describe();
188
+ return Object.fromEntries(Object.entries(description.fields).reduce((acc, fieldEntry) => {
189
+ return [...acc, ..._parseYupField(fieldEntry)];
190
+ }, []));
191
+ }
192
+
193
+ exports.getYupConstraint = getYupConstraint;
package/constraint.mjs ADDED
@@ -0,0 +1,189 @@
1
+ function _parseStringFieldTests(def) {
2
+ var constraint = {};
3
+ for (var test of def.tests) {
4
+ switch (test.name) {
5
+ case 'required':
6
+ {
7
+ constraint.required = true;
8
+ }
9
+ case 'min':
10
+ {
11
+ var _test$params;
12
+ if (!constraint.minLength && (_test$params = test.params) !== null && _test$params !== void 0 && _test$params.min) {
13
+ var _test$params2;
14
+ constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min);
15
+ }
16
+ }
17
+ case 'max':
18
+ {
19
+ var _test$params3;
20
+ if (!constraint.maxLength && (_test$params3 = test.params) !== null && _test$params3 !== void 0 && _test$params3.max) {
21
+ constraint.maxLength = Number(test.params.max);
22
+ }
23
+ }
24
+ case 'matches':
25
+ {
26
+ var _test$params4;
27
+ if (!constraint.pattern && ((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.regex) instanceof RegExp) {
28
+ constraint.pattern = test.params.regex.source;
29
+ }
30
+ }
31
+ }
32
+ }
33
+ if (!constraint.pattern && def.oneOf.length > 0) {
34
+ constraint.pattern = def.oneOf.join('|');
35
+ }
36
+ return constraint;
37
+ }
38
+ function _parseDateFieldTests(def) {
39
+ var constraint = {};
40
+ for (var test of def.tests) {
41
+ switch (test.name) {
42
+ case 'required':
43
+ constraint.required = true;
44
+ break;
45
+ case 'min':
46
+ if (!constraint.min && test.params) {
47
+ constraint.min = test.params.min;
48
+ }
49
+ break;
50
+ case 'max':
51
+ if (!constraint.max && test.params) {
52
+ constraint.max = test.params.max;
53
+ }
54
+ break;
55
+ }
56
+ }
57
+ return constraint;
58
+ }
59
+ function _parseNumberFieldTests(def) {
60
+ var _test$params5, _test$params7;
61
+ var constraint = {};
62
+ for (var test of def.tests) {
63
+ switch (test.name) {
64
+ case 'required':
65
+ constraint.required = true;
66
+ break;
67
+ case 'min':
68
+ if (typeof constraint.min === 'string') {
69
+ throw new Error('min should not be a string');
70
+ }
71
+ if (typeof constraint.min === 'object') {
72
+ throw new Error('min should be a number');
73
+ }
74
+ if (!constraint.min || constraint.min < Number((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.min)) {
75
+ var _test$params6;
76
+ constraint.min = Number((_test$params6 = test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min);
77
+ }
78
+ break;
79
+ case 'max':
80
+ if (typeof constraint.max === 'string') {
81
+ throw new Error('max should not be a number');
82
+ }
83
+ if (typeof constraint.max === 'object') {
84
+ throw new Error('min should be a number');
85
+ }
86
+ if (!constraint.max || constraint.max > Number((_test$params7 = test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.max)) {
87
+ var _test$params8;
88
+ constraint.max = Number((_test$params8 = test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max);
89
+ }
90
+ break;
91
+ }
92
+ }
93
+ return constraint;
94
+ }
95
+ function _parseObjectFieldTests(def) {
96
+ var constraint = {};
97
+ for (var test of def.tests) {
98
+ switch (test.name) {
99
+ case 'required':
100
+ constraint.required = true;
101
+ break;
102
+ }
103
+ }
104
+ return constraint;
105
+ }
106
+ function _parseYupField(_ref) {
107
+ var _test$params9, _test$params10;
108
+ var [key, def] = _ref;
109
+ var constraints = [];
110
+ switch (def.type) {
111
+ case 'object':
112
+ {
113
+ var {
114
+ fields
115
+ } = def;
116
+ Object.entries(fields).forEach(_ref2 => {
117
+ var [fieldKey, field] = _ref2;
118
+ constraints = [...constraints, ..._parseYupField(["".concat(key, ".").concat(fieldKey), field])];
119
+ });
120
+ constraints.push([key, _parseObjectFieldTests(def)]);
121
+ break;
122
+ }
123
+ case 'array':
124
+ {
125
+ var constraint = {};
126
+ constraint.multiple = true;
127
+ for (var test of def.tests) {
128
+ switch (test.name) {
129
+ case 'required':
130
+ constraint.required = true;
131
+ break;
132
+ case 'min':
133
+ if (Number((_test$params9 = test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.min) > 1) {
134
+ constraint.multiple = true;
135
+ }
136
+ break;
137
+ case 'max':
138
+ if (Number((_test$params10 = test.params) === null || _test$params10 === void 0 ? void 0 : _test$params10.max) > 1) {
139
+ constraint.multiple = true;
140
+ }
141
+ break;
142
+ }
143
+ }
144
+ var {
145
+ innerType
146
+ } = def;
147
+ if (innerType) {
148
+ switch (innerType.type) {
149
+ case 'string':
150
+ {
151
+ constraints.push(["".concat(key, "[]"), _parseStringFieldTests(innerType)]);
152
+ break;
153
+ }
154
+ case 'object':
155
+ {
156
+ constraints = constraints.concat(_parseYupField(["".concat(key, "[]"), innerType]));
157
+ break;
158
+ }
159
+ }
160
+ }
161
+ constraints.push([key, constraint]);
162
+ break;
163
+ }
164
+ case 'date':
165
+ {
166
+ constraints.push([key, _parseDateFieldTests(def)]);
167
+ break;
168
+ }
169
+ case 'string':
170
+ {
171
+ constraints = [[key, _parseStringFieldTests(def)]];
172
+ break;
173
+ }
174
+ case 'number':
175
+ {
176
+ constraints = [[key, _parseNumberFieldTests(def)]];
177
+ break;
178
+ }
179
+ }
180
+ return constraints;
181
+ }
182
+ function getYupConstraint(source) {
183
+ var description = source.describe();
184
+ return Object.fromEntries(Object.entries(description.fields).reduce((acc, fieldEntry) => {
185
+ return [...acc, ..._parseYupField(fieldEntry)];
186
+ }, []));
187
+ }
188
+
189
+ export { getYupConstraint };
package/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { type Constraint, type Intent, type Submission } from '@conform-to/dom';
1
+ import { type Intent, type Submission } from '@conform-to/dom';
2
2
  import * as yup from 'yup';
3
- export declare function getYupConstraint<Source extends yup.AnyObjectSchema>(source: Source): Record<string, Constraint>;
3
+ export { getYupConstraint } from './constraint';
4
4
  export declare function parseWithYup<Schema extends yup.AnyObjectSchema>(payload: FormData | URLSearchParams, config: {
5
5
  schema: Schema | ((intent: Intent | null) => Schema);
6
6
  async?: false;
package/index.js CHANGED
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var dom = require('@conform-to/dom');
6
6
  var yup = require('yup');
7
+ var constraint = require('./constraint.js');
7
8
 
8
9
  function _interopNamespace(e) {
9
10
  if (e && e.__esModule) return e;
@@ -25,75 +26,6 @@ function _interopNamespace(e) {
25
26
 
26
27
  var yup__namespace = /*#__PURE__*/_interopNamespace(yup);
27
28
 
28
- function getYupConstraint(source) {
29
- var description = source.describe();
30
- return Object.fromEntries(Object.entries(description.fields).map(_ref => {
31
- var _test$params, _test$params3, _test$params5, _test$params6, _test$params8;
32
- var [key, def] = _ref;
33
- var constraint = {};
34
- switch (def.type) {
35
- case 'string':
36
- {
37
- for (var test of def.tests) {
38
- switch (test.name) {
39
- case 'required':
40
- constraint.required = true;
41
- break;
42
- case 'min':
43
- if (!constraint.minLength || constraint.minLength < Number((_test$params = test.params) === null || _test$params === void 0 ? void 0 : _test$params.min)) {
44
- var _test$params2;
45
- constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min);
46
- }
47
- break;
48
- case 'max':
49
- if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) {
50
- var _test$params4;
51
- constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max);
52
- }
53
- break;
54
- case 'matches':
55
- if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) {
56
- constraint.pattern = test.params.regex.source;
57
- }
58
- break;
59
- }
60
- }
61
- if (!constraint.pattern && def.oneOf.length > 0) {
62
- constraint.pattern = def.oneOf.join('|');
63
- }
64
- break;
65
- }
66
- case 'number':
67
- for (var _test of def.tests) {
68
- switch (_test.name) {
69
- case 'required':
70
- constraint.required = true;
71
- break;
72
- case 'min':
73
- if (typeof constraint.min === 'string') {
74
- throw new Error('min should not be a string');
75
- }
76
- if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) {
77
- var _test$params7;
78
- constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min);
79
- }
80
- break;
81
- case 'max':
82
- if (typeof constraint.max === 'string') {
83
- throw new Error('max should not be a number');
84
- }
85
- if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) {
86
- var _test$params9;
87
- constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max);
88
- }
89
- break;
90
- }
91
- }
92
- break;
93
- }
94
- return [key, constraint];
95
- }));
96
- }
97
29
  function parseWithYup(payload, config) {
98
30
  return dom.parse(payload, {
99
31
  resolve(payload, intent) {
@@ -131,5 +63,5 @@ function parseWithYup(payload, config) {
131
63
  });
132
64
  }
133
65
 
134
- exports.getYupConstraint = getYupConstraint;
66
+ exports.getYupConstraint = constraint.getYupConstraint;
135
67
  exports.parseWithYup = parseWithYup;
package/index.mjs CHANGED
@@ -1,75 +1,7 @@
1
1
  import { parse } from '@conform-to/dom';
2
2
  import * as yup from 'yup';
3
+ export { getYupConstraint } from './constraint.mjs';
3
4
 
4
- function getYupConstraint(source) {
5
- var description = source.describe();
6
- return Object.fromEntries(Object.entries(description.fields).map(_ref => {
7
- var _test$params, _test$params3, _test$params5, _test$params6, _test$params8;
8
- var [key, def] = _ref;
9
- var constraint = {};
10
- switch (def.type) {
11
- case 'string':
12
- {
13
- for (var test of def.tests) {
14
- switch (test.name) {
15
- case 'required':
16
- constraint.required = true;
17
- break;
18
- case 'min':
19
- if (!constraint.minLength || constraint.minLength < Number((_test$params = test.params) === null || _test$params === void 0 ? void 0 : _test$params.min)) {
20
- var _test$params2;
21
- constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min);
22
- }
23
- break;
24
- case 'max':
25
- if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) {
26
- var _test$params4;
27
- constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max);
28
- }
29
- break;
30
- case 'matches':
31
- if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) {
32
- constraint.pattern = test.params.regex.source;
33
- }
34
- break;
35
- }
36
- }
37
- if (!constraint.pattern && def.oneOf.length > 0) {
38
- constraint.pattern = def.oneOf.join('|');
39
- }
40
- break;
41
- }
42
- case 'number':
43
- for (var _test of def.tests) {
44
- switch (_test.name) {
45
- case 'required':
46
- constraint.required = true;
47
- break;
48
- case 'min':
49
- if (typeof constraint.min === 'string') {
50
- throw new Error('min should not be a string');
51
- }
52
- if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) {
53
- var _test$params7;
54
- constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min);
55
- }
56
- break;
57
- case 'max':
58
- if (typeof constraint.max === 'string') {
59
- throw new Error('max should not be a number');
60
- }
61
- if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) {
62
- var _test$params9;
63
- constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max);
64
- }
65
- break;
66
- }
67
- }
68
- break;
69
- }
70
- return [key, constraint];
71
- }));
72
- }
73
5
  function parseWithYup(payload, config) {
74
6
  return parse(payload, {
75
7
  resolve(payload, intent) {
@@ -107,4 +39,4 @@ function parseWithYup(payload, config) {
107
39
  });
108
40
  }
109
41
 
110
- export { getYupConstraint, parseWithYup };
42
+ export { parseWithYup };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Conform helpers for integrating with yup",
4
4
  "homepage": "https://conform.guide",
5
5
  "license": "MIT",
6
- "version": "1.0.6",
6
+ "version": "1.1.0",
7
7
  "main": "index.js",
8
8
  "module": "index.mjs",
9
9
  "types": "index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "url": "https://github.com/edmundhung/conform/issues"
26
26
  },
27
27
  "peerDependencies": {
28
- "@conform-to/dom": "1.0.6",
28
+ "@conform-to/dom": "1.1.0",
29
29
  "yup": ">=0.32.0"
30
30
  },
31
31
  "devDependencies": {