@conform-to/yup 0.5.1 → 0.6.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/README.md +34 -86
- package/_virtual/_rollupPluginBabelHelpers.js +0 -6
- package/index.js +44 -24
- package/module/_virtual/_rollupPluginBabelHelpers.js +0 -6
- package/module/index.js +45 -24
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,92 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
## API Reference
|
|
8
8
|
|
|
9
|
-
- [formatError](#formatError)
|
|
10
9
|
- [getFieldsetConstraint](#getfieldsetconstraint)
|
|
11
|
-
- [
|
|
10
|
+
- [parse](#parse)
|
|
12
11
|
|
|
13
12
|
<!-- /aside -->
|
|
14
13
|
|
|
15
|
-
### formatError
|
|
16
|
-
|
|
17
|
-
This formats Yup **ValidationError** to conform's error structure (i.e. A set of key/value pairs).
|
|
18
|
-
|
|
19
|
-
If an error is received instead of the Yup **ValidationError**, it will be treated as a form level error with message set to **error.messages**.
|
|
20
|
-
|
|
21
|
-
```tsx
|
|
22
|
-
import { useForm, parse } from '@conform-to/react';
|
|
23
|
-
import { formatError } from '@conform-to/yup';
|
|
24
|
-
import * as yup from 'yup';
|
|
25
|
-
|
|
26
|
-
const schema = yup.object({
|
|
27
|
-
email: yup.string().required(),
|
|
28
|
-
password: yup.string().required(),
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
function ExampleForm() {
|
|
32
|
-
const [form] = useForm<yup.InferType<typeof schema>>({
|
|
33
|
-
onValidate({ formData }) {
|
|
34
|
-
const submission = parse(formData);
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
// Only sync validation is allowed on the client side
|
|
38
|
-
schema.validateSync(submission.value, {
|
|
39
|
-
abortEarly: false,
|
|
40
|
-
});
|
|
41
|
-
} catch (error) {
|
|
42
|
-
submission.error.push(...formatError(error));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return submission;
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// ...
|
|
50
|
-
}
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
Or when validating the formData on server side (e.g. Remix):
|
|
54
|
-
|
|
55
|
-
```tsx
|
|
56
|
-
import { useForm, parse } from '@conform-to/react';
|
|
57
|
-
import { formatError } from '@conform-to/yup';
|
|
58
|
-
import * as yup from 'yup';
|
|
59
|
-
|
|
60
|
-
const schema = yup.object({
|
|
61
|
-
// Define the schema with yup
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
export let action = async ({ request }) => {
|
|
65
|
-
const formData = await request.formData();
|
|
66
|
-
const submission = parse(formData);
|
|
67
|
-
|
|
68
|
-
try {
|
|
69
|
-
// You can extends the schema with async validation as well
|
|
70
|
-
const data = await schema.validate(submission.value, {
|
|
71
|
-
abortEarly: false,
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
if (submission.type !== 'validate') {
|
|
75
|
-
return await handleFormData(data);
|
|
76
|
-
}
|
|
77
|
-
} catch (error) {
|
|
78
|
-
submission.error.push(...formatError(error));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return submission;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export default function ExampleRoute() {
|
|
85
|
-
const state = useActionData();
|
|
86
|
-
const [form] = useForm({
|
|
87
|
-
mode: 'server-validation',
|
|
88
|
-
state,
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// ...
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
14
|
### getFieldsetConstraint
|
|
96
15
|
|
|
97
16
|
This tries to infer constraint of each field based on the yup schema. This is useful for:
|
|
@@ -118,13 +37,13 @@ function Example() {
|
|
|
118
37
|
}
|
|
119
38
|
```
|
|
120
39
|
|
|
121
|
-
###
|
|
40
|
+
### parse
|
|
122
41
|
|
|
123
|
-
It parses the formData and returns a
|
|
42
|
+
It parses the formData and returns a submission result with the validation error. If no error is found, the parsed data will also be populated as `submission.value`.
|
|
124
43
|
|
|
125
44
|
```tsx
|
|
126
45
|
import { useForm } from '@conform-to/react';
|
|
127
|
-
import {
|
|
46
|
+
import { parse } from '@conform-to/yup';
|
|
128
47
|
import * as yup from 'yup';
|
|
129
48
|
|
|
130
49
|
const schema = yup.object({
|
|
@@ -135,10 +54,39 @@ const schema = yup.object({
|
|
|
135
54
|
function ExampleForm() {
|
|
136
55
|
const [form] = useForm({
|
|
137
56
|
onValidate({ formData }) {
|
|
138
|
-
return
|
|
57
|
+
return parse(formData, { schema });
|
|
139
58
|
},
|
|
140
59
|
});
|
|
141
60
|
|
|
142
61
|
// ...
|
|
143
62
|
}
|
|
144
63
|
```
|
|
64
|
+
|
|
65
|
+
Or when parsing the formData on server side (e.g. Remix):
|
|
66
|
+
|
|
67
|
+
```tsx
|
|
68
|
+
import { useForm } from '@conform-to/react';
|
|
69
|
+
import { parse } from '@conform-to/yup';
|
|
70
|
+
import * as yup from 'yup';
|
|
71
|
+
|
|
72
|
+
const schema = yup.object({
|
|
73
|
+
// Define the schema with yup
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
export let action = async ({ request }) => {
|
|
77
|
+
const formData = await request.formData();
|
|
78
|
+
const submission = parse(formData, {
|
|
79
|
+
// If you need extra validation on server side
|
|
80
|
+
schema: schema.test(/* ... */),
|
|
81
|
+
|
|
82
|
+
// If the schema definition includes async validation
|
|
83
|
+
async: true,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (!submission.value || submission.intent !== 'submit') {
|
|
87
|
+
return submission;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ...
|
|
91
|
+
};
|
|
92
|
+
```
|
|
@@ -4,17 +4,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
function ownKeys(object, enumerableOnly) {
|
|
6
6
|
var keys = Object.keys(object);
|
|
7
|
-
|
|
8
7
|
if (Object.getOwnPropertySymbols) {
|
|
9
8
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
10
9
|
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
11
10
|
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
12
11
|
})), keys.push.apply(keys, symbols);
|
|
13
12
|
}
|
|
14
|
-
|
|
15
13
|
return keys;
|
|
16
14
|
}
|
|
17
|
-
|
|
18
15
|
function _objectSpread2(target) {
|
|
19
16
|
for (var i = 1; i < arguments.length; i++) {
|
|
20
17
|
var source = null != arguments[i] ? arguments[i] : {};
|
|
@@ -24,10 +21,8 @@ function _objectSpread2(target) {
|
|
|
24
21
|
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
25
22
|
});
|
|
26
23
|
}
|
|
27
|
-
|
|
28
24
|
return target;
|
|
29
25
|
}
|
|
30
|
-
|
|
31
26
|
function _defineProperty(obj, key, value) {
|
|
32
27
|
if (key in obj) {
|
|
33
28
|
Object.defineProperty(obj, key, {
|
|
@@ -39,7 +34,6 @@ function _defineProperty(obj, key, value) {
|
|
|
39
34
|
} else {
|
|
40
35
|
obj[key] = value;
|
|
41
36
|
}
|
|
42
|
-
|
|
43
37
|
return obj;
|
|
44
38
|
}
|
|
45
39
|
|
package/index.js
CHANGED
|
@@ -88,30 +88,50 @@ function getFieldsetConstraint(source) {
|
|
|
88
88
|
return [key, constraint];
|
|
89
89
|
}));
|
|
90
90
|
}
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
var
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
91
|
+
function parse(payload, config) {
|
|
92
|
+
return dom.parse(payload, {
|
|
93
|
+
resolve(payload, intent) {
|
|
94
|
+
var schema = typeof config.schema === 'function' ? config.schema(intent) : config.schema;
|
|
95
|
+
var resolveData = value => ({
|
|
96
|
+
value
|
|
97
|
+
});
|
|
98
|
+
var resolveError = error => {
|
|
99
|
+
if (error instanceof yup__namespace.ValidationError) {
|
|
100
|
+
return {
|
|
101
|
+
error: error.inner.reduce((result, e) => {
|
|
102
|
+
var _e$path, _config$acceptMultipl;
|
|
103
|
+
var name = (_e$path = e.path) !== null && _e$path !== void 0 ? _e$path : '';
|
|
104
|
+
if (typeof result[name] === 'undefined') {
|
|
105
|
+
result[name] = e.message;
|
|
106
|
+
} else if ((_config$acceptMultipl = config.acceptMultipleErrors) !== null && _config$acceptMultipl !== void 0 && _config$acceptMultipl.call(config, {
|
|
107
|
+
name,
|
|
108
|
+
intent,
|
|
109
|
+
payload
|
|
110
|
+
})) {
|
|
111
|
+
result[name] = [].concat(result[name], e.message);
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}, {})
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
throw error;
|
|
118
|
+
};
|
|
119
|
+
if (!config.async) {
|
|
120
|
+
try {
|
|
121
|
+
var data = schema.validateSync(payload, {
|
|
122
|
+
abortEarly: false
|
|
123
|
+
});
|
|
124
|
+
return resolveData(data);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
return resolveError(error);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return schema.validate(payload, {
|
|
130
|
+
abortEarly: false
|
|
131
|
+
}).then(resolveData).catch(resolveError);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
113
134
|
}
|
|
114
135
|
|
|
115
|
-
exports.formatError = formatError;
|
|
116
136
|
exports.getFieldsetConstraint = getFieldsetConstraint;
|
|
117
|
-
exports.
|
|
137
|
+
exports.parse = parse;
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
function ownKeys(object, enumerableOnly) {
|
|
2
2
|
var keys = Object.keys(object);
|
|
3
|
-
|
|
4
3
|
if (Object.getOwnPropertySymbols) {
|
|
5
4
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
6
5
|
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
7
6
|
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
8
7
|
})), keys.push.apply(keys, symbols);
|
|
9
8
|
}
|
|
10
|
-
|
|
11
9
|
return keys;
|
|
12
10
|
}
|
|
13
|
-
|
|
14
11
|
function _objectSpread2(target) {
|
|
15
12
|
for (var i = 1; i < arguments.length; i++) {
|
|
16
13
|
var source = null != arguments[i] ? arguments[i] : {};
|
|
@@ -20,10 +17,8 @@ function _objectSpread2(target) {
|
|
|
20
17
|
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
21
18
|
});
|
|
22
19
|
}
|
|
23
|
-
|
|
24
20
|
return target;
|
|
25
21
|
}
|
|
26
|
-
|
|
27
22
|
function _defineProperty(obj, key, value) {
|
|
28
23
|
if (key in obj) {
|
|
29
24
|
Object.defineProperty(obj, key, {
|
|
@@ -35,7 +30,6 @@ function _defineProperty(obj, key, value) {
|
|
|
35
30
|
} else {
|
|
36
31
|
obj[key] = value;
|
|
37
32
|
}
|
|
38
|
-
|
|
39
33
|
return obj;
|
|
40
34
|
}
|
|
41
35
|
|
package/module/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { parse } from '@conform-to/dom';
|
|
1
|
+
import { parse as parse$1 } from '@conform-to/dom';
|
|
2
2
|
import * as yup from 'yup';
|
|
3
3
|
|
|
4
4
|
function getFieldsetConstraint(source) {
|
|
@@ -64,28 +64,49 @@ function getFieldsetConstraint(source) {
|
|
|
64
64
|
return [key, constraint];
|
|
65
65
|
}));
|
|
66
66
|
}
|
|
67
|
-
function
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
var
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
67
|
+
function parse(payload, config) {
|
|
68
|
+
return parse$1(payload, {
|
|
69
|
+
resolve(payload, intent) {
|
|
70
|
+
var schema = typeof config.schema === 'function' ? config.schema(intent) : config.schema;
|
|
71
|
+
var resolveData = value => ({
|
|
72
|
+
value
|
|
73
|
+
});
|
|
74
|
+
var resolveError = error => {
|
|
75
|
+
if (error instanceof yup.ValidationError) {
|
|
76
|
+
return {
|
|
77
|
+
error: error.inner.reduce((result, e) => {
|
|
78
|
+
var _e$path, _config$acceptMultipl;
|
|
79
|
+
var name = (_e$path = e.path) !== null && _e$path !== void 0 ? _e$path : '';
|
|
80
|
+
if (typeof result[name] === 'undefined') {
|
|
81
|
+
result[name] = e.message;
|
|
82
|
+
} else if ((_config$acceptMultipl = config.acceptMultipleErrors) !== null && _config$acceptMultipl !== void 0 && _config$acceptMultipl.call(config, {
|
|
83
|
+
name,
|
|
84
|
+
intent,
|
|
85
|
+
payload
|
|
86
|
+
})) {
|
|
87
|
+
result[name] = [].concat(result[name], e.message);
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}, {})
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
throw error;
|
|
94
|
+
};
|
|
95
|
+
if (!config.async) {
|
|
96
|
+
try {
|
|
97
|
+
var data = schema.validateSync(payload, {
|
|
98
|
+
abortEarly: false
|
|
99
|
+
});
|
|
100
|
+
return resolveData(data);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
return resolveError(error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return schema.validate(payload, {
|
|
106
|
+
abortEarly: false
|
|
107
|
+
}).then(resolveData).catch(resolveError);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
89
110
|
}
|
|
90
111
|
|
|
91
|
-
export {
|
|
112
|
+
export { getFieldsetConstraint, parse };
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@conform-to/yup",
|
|
3
3
|
"description": "Conform helpers for integrating with yup",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.6.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.6.0-pre.0",
|
|
18
18
|
"yup": ">=0.32.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|