@conform-to/yup 0.3.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Edmund Hung
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # @conform-to/yup
2
+
3
+ > [Yup](https://github.com/jquense/yup) schema resolver for [conform](https://github.com/edmundhung/conform)
4
+
5
+ ## API Reference
6
+
7
+ - [resolve](#resolve)
8
+
9
+ ### resolve
10
+
11
+ It resolves yup schema to a conform schema:
12
+
13
+ ```tsx
14
+ import { useForm, useFieldset } from '@conform-to/react';
15
+ import { resolve } from '@conform-to/yup';
16
+ import * as yup from 'yup';
17
+
18
+ // Define the schema with zod
19
+ const schema = resolve(
20
+ yup.object({
21
+ email: yup.string().required(),
22
+ password: yup.string().required(),
23
+ })
24
+ );
25
+
26
+ // When used with `@conform-to/react`:
27
+ function ExampleForm() {
28
+ const formProps = useForm({
29
+ // Validating the form with the schema
30
+ validate: schema.validate
31
+ onSubmit: event => {
32
+ // Read the FormData from the from
33
+ const payload = new FormData(e.target);
34
+
35
+ // Parse the data against the zod schema
36
+ const submission = schema.parse(payload);
37
+
38
+ // It could be accepted / rejected / modified
39
+ console.log(submission.state);
40
+
41
+ // Parsed value (Only if accepted)
42
+ console.log(submission.data);
43
+
44
+ // Structured form value
45
+ console.log(submission.form.value);
46
+
47
+ // Structured form error (only if rejected)
48
+ console.log(submission.form.error);
49
+ };
50
+ })
51
+ const [setupFieldset, { email, password }] = useFieldset({
52
+ // Inferring the constraint with the schema
53
+ constraint: schema.constraint
54
+ });
55
+
56
+ // ...
57
+ }
58
+ ```
59
+
60
+ Or parse the request payload on server side (e.g. Remix):
61
+
62
+ ```tsx
63
+ import { resolve } from '@conform-to/yup';
64
+ import * as yup from 'yup';
65
+
66
+ const schema = resolve(
67
+ yup.object({
68
+ // Define the schema with yup
69
+ }),
70
+ );
71
+
72
+ export let action = async ({ request }) => {
73
+ const formData = await request.formData();
74
+ const submission = schema.parse(formData);
75
+
76
+ // Return the current form state if not accepted
77
+ if (submission.state !== 'accepted') {
78
+ return json(submission.form);
79
+ }
80
+
81
+ // Do something else
82
+ };
83
+
84
+ export default function ExampleRoute() {
85
+ const formState = useActionData();
86
+
87
+ // You can then use formState.value / formState.error
88
+ // to populate inital value of each fields with
89
+ // the intital error
90
+ }
91
+ ```
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function ownKeys(object, enumerableOnly) {
6
+ var keys = Object.keys(object);
7
+
8
+ if (Object.getOwnPropertySymbols) {
9
+ var symbols = Object.getOwnPropertySymbols(object);
10
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
11
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
12
+ })), keys.push.apply(keys, symbols);
13
+ }
14
+
15
+ return keys;
16
+ }
17
+
18
+ function _objectSpread2(target) {
19
+ for (var i = 1; i < arguments.length; i++) {
20
+ var source = null != arguments[i] ? arguments[i] : {};
21
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
22
+ _defineProperty(target, key, source[key]);
23
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
24
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
25
+ });
26
+ }
27
+
28
+ return target;
29
+ }
30
+
31
+ function _defineProperty(obj, key, value) {
32
+ if (key in obj) {
33
+ Object.defineProperty(obj, key, {
34
+ value: value,
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true
38
+ });
39
+ } else {
40
+ obj[key] = value;
41
+ }
42
+
43
+ return obj;
44
+ }
45
+
46
+ exports.defineProperty = _defineProperty;
47
+ exports.objectSpread2 = _objectSpread2;
package/index.js ADDED
@@ -0,0 +1,187 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var dom = require('@conform-to/dom');
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) {
43
+ var description = source.describe();
44
+ return {
45
+ source,
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
+ }
67
+
68
+ break;
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
+ }
94
+
95
+ case 'number':
96
+ for (var _test of def.tests) {
97
+ switch (_test.name) {
98
+ case 'required':
99
+ constraint.required = true;
100
+ break;
101
+
102
+ case 'min':
103
+ if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) {
104
+ var _test$params7;
105
+
106
+ constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min);
107
+ }
108
+
109
+ break;
110
+
111
+ case 'max':
112
+ if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) {
113
+ var _test$params9;
114
+
115
+ constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max);
116
+ }
117
+
118
+ break;
119
+ }
120
+ }
121
+
122
+ 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
+ }
147
+ }
148
+
149
+ dom.setFormError(form, errors);
150
+ },
151
+
152
+ parse(payload) {
153
+ var submission = dom.createSubmission(payload);
154
+
155
+ if (submission.state !== 'accepted') {
156
+ return submission;
157
+ }
158
+
159
+ try {
160
+ var result = source.validateSync(submission.form.value, {
161
+ abortEarly: false
162
+ });
163
+ return {
164
+ state: 'accepted',
165
+ data: result,
166
+ form: submission.form
167
+ };
168
+ } catch (error) {
169
+ if (error instanceof yup__namespace.ValidationError) {
170
+ return {
171
+ state: 'rejected',
172
+ form: {
173
+ // @ts-expect-error
174
+ value: submission.form.value,
175
+ error: formatError(error)
176
+ }
177
+ };
178
+ }
179
+
180
+ throw error;
181
+ }
182
+ }
183
+
184
+ };
185
+ }
186
+
187
+ exports.resolve = resolve;
@@ -0,0 +1,42 @@
1
+ function ownKeys(object, enumerableOnly) {
2
+ var keys = Object.keys(object);
3
+
4
+ if (Object.getOwnPropertySymbols) {
5
+ var symbols = Object.getOwnPropertySymbols(object);
6
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
7
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
8
+ })), keys.push.apply(keys, symbols);
9
+ }
10
+
11
+ return keys;
12
+ }
13
+
14
+ function _objectSpread2(target) {
15
+ for (var i = 1; i < arguments.length; i++) {
16
+ var source = null != arguments[i] ? arguments[i] : {};
17
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
18
+ _defineProperty(target, key, source[key]);
19
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
20
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
21
+ });
22
+ }
23
+
24
+ return target;
25
+ }
26
+
27
+ function _defineProperty(obj, key, value) {
28
+ if (key in obj) {
29
+ Object.defineProperty(obj, key, {
30
+ value: value,
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true
34
+ });
35
+ } else {
36
+ obj[key] = value;
37
+ }
38
+
39
+ return obj;
40
+ }
41
+
42
+ export { _defineProperty as defineProperty, _objectSpread2 as objectSpread2 };
@@ -0,0 +1,163 @@
1
+ import { getFormData, createSubmission, setFormError, setValue, getPaths } from '@conform-to/dom';
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) {
19
+ var description = source.describe();
20
+ return {
21
+ source,
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;
45
+
46
+ case 'max':
47
+ if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) {
48
+ var _test$params4;
49
+
50
+ constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max);
51
+ }
52
+
53
+ break;
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) {
74
+ case 'required':
75
+ constraint.required = true;
76
+ break;
77
+
78
+ case 'min':
79
+ if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) {
80
+ var _test$params7;
81
+
82
+ constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min);
83
+ }
84
+
85
+ break;
86
+
87
+ case 'max':
88
+ if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) {
89
+ var _test$params9;
90
+
91
+ constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max);
92
+ }
93
+
94
+ break;
95
+ }
96
+ }
97
+
98
+ 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
+ }
123
+ }
124
+
125
+ setFormError(form, errors);
126
+ },
127
+
128
+ parse(payload) {
129
+ var submission = createSubmission(payload);
130
+
131
+ if (submission.state !== 'accepted') {
132
+ return submission;
133
+ }
134
+
135
+ try {
136
+ var result = source.validateSync(submission.form.value, {
137
+ abortEarly: false
138
+ });
139
+ return {
140
+ state: 'accepted',
141
+ data: result,
142
+ form: submission.form
143
+ };
144
+ } catch (error) {
145
+ if (error instanceof yup.ValidationError) {
146
+ return {
147
+ state: 'rejected',
148
+ form: {
149
+ // @ts-expect-error
150
+ value: submission.form.value,
151
+ error: formatError(error)
152
+ }
153
+ };
154
+ }
155
+
156
+ throw error;
157
+ }
158
+ }
159
+
160
+ };
161
+ }
162
+
163
+ export { resolve };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@conform-to/yup",
3
+ "description": "Conform schema resolver for yup",
4
+ "license": "MIT",
5
+ "version": "0.3.0-pre.0",
6
+ "main": "index.js",
7
+ "module": "module/index.js",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/edmundhung/conform",
11
+ "directory": "packages/conform-yup"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/edmundhung/conform/issues"
15
+ },
16
+ "peerDependencies": {
17
+ "@conform-to/dom": "0.3.0-pre.0",
18
+ "yup": ">=0.32.0"
19
+ },
20
+ "devDependencies": {
21
+ "yup": "^0.32.11"
22
+ },
23
+ "sideEffects": false
24
+ }