@conform-to/yup 0.3.1 → 0.4.0-pre.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/index.js +73 -154
- package/module/index.js +72 -134
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -2,186 +2,105 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
var yup = require('yup');
|
|
7
|
-
|
|
8
|
-
function _interopNamespace(e) {
|
|
9
|
-
if (e && e.__esModule) return e;
|
|
10
|
-
var n = Object.create(null);
|
|
11
|
-
if (e) {
|
|
12
|
-
Object.keys(e).forEach(function (k) {
|
|
13
|
-
if (k !== 'default') {
|
|
14
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
get: function () { return e[k]; }
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
n["default"] = e;
|
|
23
|
-
return Object.freeze(n);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
var yup__namespace = /*#__PURE__*/_interopNamespace(yup);
|
|
27
|
-
|
|
28
|
-
function formatError(error) {
|
|
29
|
-
var result = {};
|
|
30
|
-
|
|
31
|
-
var _loop = function _loop(validationError) {
|
|
32
|
-
dom.setValue(result, dom.getPaths(validationError.path).flatMap(path => ['details', path]).concat('message'), prev => prev ? prev : validationError.message);
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
for (var validationError of error.inner) {
|
|
36
|
-
_loop(validationError);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function resolve(source) {
|
|
5
|
+
function getFieldsetConstraint(source) {
|
|
43
6
|
var description = source.describe();
|
|
44
|
-
return {
|
|
45
|
-
|
|
46
|
-
constraint: Object.fromEntries(Object.entries(description.fields).map(_ref => {
|
|
47
|
-
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8;
|
|
48
|
-
|
|
49
|
-
var [key, def] = _ref;
|
|
50
|
-
var constraint = {};
|
|
51
|
-
|
|
52
|
-
switch (def.type) {
|
|
53
|
-
case 'string':
|
|
54
|
-
{
|
|
55
|
-
for (var test of def.tests) {
|
|
56
|
-
switch (test.name) {
|
|
57
|
-
case 'required':
|
|
58
|
-
constraint.required = true;
|
|
59
|
-
break;
|
|
60
|
-
|
|
61
|
-
case 'min':
|
|
62
|
-
if (!constraint.minLength || constraint.minLength < Number((_test$params = test.params) === null || _test$params === void 0 ? void 0 : _test$params.min)) {
|
|
63
|
-
var _test$params2;
|
|
64
|
-
|
|
65
|
-
constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min);
|
|
66
|
-
}
|
|
7
|
+
return Object.fromEntries(Object.entries(description.fields).map(_ref => {
|
|
8
|
+
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8;
|
|
67
9
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
case 'max':
|
|
71
|
-
if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) {
|
|
72
|
-
var _test$params4;
|
|
73
|
-
|
|
74
|
-
constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
break;
|
|
78
|
-
|
|
79
|
-
case 'matches':
|
|
80
|
-
if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) {
|
|
81
|
-
constraint.pattern = test.params.regex.source;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (!constraint.pattern && def.oneOf.length > 0) {
|
|
89
|
-
constraint.pattern = def.oneOf.join('|');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
10
|
+
var [key, def] = _ref;
|
|
11
|
+
var constraint = {};
|
|
94
12
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
13
|
+
switch (def.type) {
|
|
14
|
+
case 'string':
|
|
15
|
+
{
|
|
16
|
+
for (var test of def.tests) {
|
|
17
|
+
switch (test.name) {
|
|
98
18
|
case 'required':
|
|
99
19
|
constraint.required = true;
|
|
100
20
|
break;
|
|
101
21
|
|
|
102
22
|
case 'min':
|
|
103
|
-
if (!constraint.
|
|
104
|
-
var _test$
|
|
23
|
+
if (!constraint.minLength || constraint.minLength < Number((_test$params = test.params) === null || _test$params === void 0 ? void 0 : _test$params.min)) {
|
|
24
|
+
var _test$params2;
|
|
105
25
|
|
|
106
|
-
constraint.
|
|
26
|
+
constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min);
|
|
107
27
|
}
|
|
108
28
|
|
|
109
29
|
break;
|
|
110
30
|
|
|
111
31
|
case 'max':
|
|
112
|
-
if (!constraint.
|
|
113
|
-
var _test$
|
|
32
|
+
if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) {
|
|
33
|
+
var _test$params4;
|
|
114
34
|
|
|
115
|
-
constraint.
|
|
35
|
+
constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
break;
|
|
39
|
+
|
|
40
|
+
case 'matches':
|
|
41
|
+
if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) {
|
|
42
|
+
constraint.pattern = test.params.regex.source;
|
|
116
43
|
}
|
|
117
44
|
|
|
118
45
|
break;
|
|
119
46
|
}
|
|
120
47
|
}
|
|
121
48
|
|
|
49
|
+
if (!constraint.pattern && def.oneOf.length > 0) {
|
|
50
|
+
constraint.pattern = def.oneOf.join('|');
|
|
51
|
+
}
|
|
52
|
+
|
|
122
53
|
break;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return [key, constraint];
|
|
126
|
-
})),
|
|
127
|
-
|
|
128
|
-
validate(form, submitter) {
|
|
129
|
-
var payload = dom.getFormData(form, submitter);
|
|
130
|
-
var submission = dom.createSubmission(payload);
|
|
131
|
-
var errors = [];
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
source.validateSync(submission.form.value, {
|
|
135
|
-
abortEarly: false
|
|
136
|
-
});
|
|
137
|
-
} catch (e) {
|
|
138
|
-
if (e instanceof yup__namespace.ValidationError) {
|
|
139
|
-
errors.push(...e.inner.map(error => {
|
|
140
|
-
var _error$path;
|
|
141
|
-
|
|
142
|
-
return [(_error$path = error.path) !== null && _error$path !== void 0 ? _error$path : '', error.message];
|
|
143
|
-
}));
|
|
144
|
-
} else {
|
|
145
|
-
throw e;
|
|
146
54
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
// @ts-expect-error
|
|
174
|
-
value: submission.form.value,
|
|
175
|
-
error: formatError(error)
|
|
176
|
-
}
|
|
177
|
-
};
|
|
55
|
+
|
|
56
|
+
case 'number':
|
|
57
|
+
for (var _test of def.tests) {
|
|
58
|
+
switch (_test.name) {
|
|
59
|
+
case 'required':
|
|
60
|
+
constraint.required = true;
|
|
61
|
+
break;
|
|
62
|
+
|
|
63
|
+
case 'min':
|
|
64
|
+
if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) {
|
|
65
|
+
var _test$params7;
|
|
66
|
+
|
|
67
|
+
constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
break;
|
|
71
|
+
|
|
72
|
+
case 'max':
|
|
73
|
+
if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) {
|
|
74
|
+
var _test$params9;
|
|
75
|
+
|
|
76
|
+
constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
178
81
|
}
|
|
179
82
|
|
|
180
|
-
|
|
181
|
-
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return [key, constraint];
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
function getError(error, scope) {
|
|
90
|
+
var _error$inner$reduce;
|
|
91
|
+
|
|
92
|
+
return (_error$inner$reduce = error === null || error === void 0 ? void 0 : error.inner.reduce((result, e) => {
|
|
93
|
+
var _e$path;
|
|
94
|
+
|
|
95
|
+
var name = (_e$path = e.path) !== null && _e$path !== void 0 ? _e$path : '';
|
|
96
|
+
|
|
97
|
+
if (!scope || scope.includes(name)) {
|
|
98
|
+
result.push([name, e.message]);
|
|
182
99
|
}
|
|
183
100
|
|
|
184
|
-
|
|
101
|
+
return result;
|
|
102
|
+
}, [])) !== null && _error$inner$reduce !== void 0 ? _error$inner$reduce : [];
|
|
185
103
|
}
|
|
186
104
|
|
|
187
|
-
exports.
|
|
105
|
+
exports.getError = getError;
|
|
106
|
+
exports.getFieldsetConstraint = getFieldsetConstraint;
|
package/module/index.js
CHANGED
|
@@ -1,163 +1,101 @@
|
|
|
1
|
-
|
|
2
|
-
import * as yup from 'yup';
|
|
3
|
-
|
|
4
|
-
function formatError(error) {
|
|
5
|
-
var result = {};
|
|
6
|
-
|
|
7
|
-
var _loop = function _loop(validationError) {
|
|
8
|
-
setValue(result, getPaths(validationError.path).flatMap(path => ['details', path]).concat('message'), prev => prev ? prev : validationError.message);
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
for (var validationError of error.inner) {
|
|
12
|
-
_loop(validationError);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return result;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function resolve(source) {
|
|
1
|
+
function getFieldsetConstraint(source) {
|
|
19
2
|
var description = source.describe();
|
|
20
|
-
return {
|
|
21
|
-
|
|
22
|
-
constraint: Object.fromEntries(Object.entries(description.fields).map(_ref => {
|
|
23
|
-
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8;
|
|
24
|
-
|
|
25
|
-
var [key, def] = _ref;
|
|
26
|
-
var constraint = {};
|
|
27
|
-
|
|
28
|
-
switch (def.type) {
|
|
29
|
-
case 'string':
|
|
30
|
-
{
|
|
31
|
-
for (var test of def.tests) {
|
|
32
|
-
switch (test.name) {
|
|
33
|
-
case 'required':
|
|
34
|
-
constraint.required = true;
|
|
35
|
-
break;
|
|
36
|
-
|
|
37
|
-
case 'min':
|
|
38
|
-
if (!constraint.minLength || constraint.minLength < Number((_test$params = test.params) === null || _test$params === void 0 ? void 0 : _test$params.min)) {
|
|
39
|
-
var _test$params2;
|
|
40
|
-
|
|
41
|
-
constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
break;
|
|
3
|
+
return Object.fromEntries(Object.entries(description.fields).map(_ref => {
|
|
4
|
+
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8;
|
|
45
5
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
var _test$params4;
|
|
6
|
+
var [key, def] = _ref;
|
|
7
|
+
var constraint = {};
|
|
49
8
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
case 'matches':
|
|
56
|
-
if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) {
|
|
57
|
-
constraint.pattern = test.params.regex.source;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (!constraint.pattern && def.oneOf.length > 0) {
|
|
65
|
-
constraint.pattern = def.oneOf.join('|');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
case 'number':
|
|
72
|
-
for (var _test of def.tests) {
|
|
73
|
-
switch (_test.name) {
|
|
9
|
+
switch (def.type) {
|
|
10
|
+
case 'string':
|
|
11
|
+
{
|
|
12
|
+
for (var test of def.tests) {
|
|
13
|
+
switch (test.name) {
|
|
74
14
|
case 'required':
|
|
75
15
|
constraint.required = true;
|
|
76
16
|
break;
|
|
77
17
|
|
|
78
18
|
case 'min':
|
|
79
|
-
if (!constraint.
|
|
80
|
-
var _test$
|
|
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;
|
|
81
21
|
|
|
82
|
-
constraint.
|
|
22
|
+
constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min);
|
|
83
23
|
}
|
|
84
24
|
|
|
85
25
|
break;
|
|
86
26
|
|
|
87
27
|
case 'max':
|
|
88
|
-
if (!constraint.
|
|
89
|
-
var _test$
|
|
28
|
+
if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) {
|
|
29
|
+
var _test$params4;
|
|
30
|
+
|
|
31
|
+
constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
break;
|
|
90
35
|
|
|
91
|
-
|
|
36
|
+
case 'matches':
|
|
37
|
+
if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) {
|
|
38
|
+
constraint.pattern = test.params.regex.source;
|
|
92
39
|
}
|
|
93
40
|
|
|
94
41
|
break;
|
|
95
42
|
}
|
|
96
43
|
}
|
|
97
44
|
|
|
45
|
+
if (!constraint.pattern && def.oneOf.length > 0) {
|
|
46
|
+
constraint.pattern = def.oneOf.join('|');
|
|
47
|
+
}
|
|
48
|
+
|
|
98
49
|
break;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return [key, constraint];
|
|
102
|
-
})),
|
|
103
|
-
|
|
104
|
-
validate(form, submitter) {
|
|
105
|
-
var payload = getFormData(form, submitter);
|
|
106
|
-
var submission = createSubmission(payload);
|
|
107
|
-
var errors = [];
|
|
108
|
-
|
|
109
|
-
try {
|
|
110
|
-
source.validateSync(submission.form.value, {
|
|
111
|
-
abortEarly: false
|
|
112
|
-
});
|
|
113
|
-
} catch (e) {
|
|
114
|
-
if (e instanceof yup.ValidationError) {
|
|
115
|
-
errors.push(...e.inner.map(error => {
|
|
116
|
-
var _error$path;
|
|
117
|
-
|
|
118
|
-
return [(_error$path = error.path) !== null && _error$path !== void 0 ? _error$path : '', error.message];
|
|
119
|
-
}));
|
|
120
|
-
} else {
|
|
121
|
-
throw e;
|
|
122
50
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
// @ts-expect-error
|
|
150
|
-
value: submission.form.value,
|
|
151
|
-
error: formatError(error)
|
|
152
|
-
}
|
|
153
|
-
};
|
|
51
|
+
|
|
52
|
+
case 'number':
|
|
53
|
+
for (var _test of def.tests) {
|
|
54
|
+
switch (_test.name) {
|
|
55
|
+
case 'required':
|
|
56
|
+
constraint.required = true;
|
|
57
|
+
break;
|
|
58
|
+
|
|
59
|
+
case 'min':
|
|
60
|
+
if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) {
|
|
61
|
+
var _test$params7;
|
|
62
|
+
|
|
63
|
+
constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
break;
|
|
67
|
+
|
|
68
|
+
case 'max':
|
|
69
|
+
if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) {
|
|
70
|
+
var _test$params9;
|
|
71
|
+
|
|
72
|
+
constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
154
77
|
}
|
|
155
78
|
|
|
156
|
-
|
|
157
|
-
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return [key, constraint];
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
function getError(error, scope) {
|
|
86
|
+
var _error$inner$reduce;
|
|
87
|
+
|
|
88
|
+
return (_error$inner$reduce = error === null || error === void 0 ? void 0 : error.inner.reduce((result, e) => {
|
|
89
|
+
var _e$path;
|
|
90
|
+
|
|
91
|
+
var name = (_e$path = e.path) !== null && _e$path !== void 0 ? _e$path : '';
|
|
92
|
+
|
|
93
|
+
if (!scope || scope.includes(name)) {
|
|
94
|
+
result.push([name, e.message]);
|
|
158
95
|
}
|
|
159
96
|
|
|
160
|
-
|
|
97
|
+
return result;
|
|
98
|
+
}, [])) !== null && _error$inner$reduce !== void 0 ? _error$inner$reduce : [];
|
|
161
99
|
}
|
|
162
100
|
|
|
163
|
-
export {
|
|
101
|
+
export { getError, getFieldsetConstraint };
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@conform-to/yup",
|
|
3
3
|
"description": "Conform schema resolver for yup",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.4.0-pre.0",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"module": "module/index.js",
|
|
8
8
|
"repository": {
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"url": "https://github.com/edmundhung/conform/issues"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@conform-to/dom": "0.
|
|
17
|
+
"@conform-to/dom": "0.4.0-pre.0",
|
|
18
18
|
"yup": ">=0.32.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|