@conform-to/yup 1.0.0-pre.5 → 1.0.0-pre.7
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 +32 -0
- package/index.d.ts +4 -4
- package/index.js +10 -6
- package/index.mjs +11 -7
- package/package.json +2 -2
- package/README.md +0 -92
package/README
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
███████╗ ██████╗ ███╗ ██╗ ████████╗ ██████╗ ███████╗ ███╗ ███╗
|
|
4
|
+
██╔═════╝ ██╔═══██╗ ████╗ ██║ ██╔═════╝ ██╔═══██╗ ██╔═══██╗ ████████║
|
|
5
|
+
██║ ██║ ██║ ██╔██╗██║ ███████╗ ██║ ██║ ███████╔╝ ██╔██╔██║
|
|
6
|
+
██║ ██║ ██║ ██║╚████║ ██╔════╝ ██║ ██║ ██╔═══██╗ ██║╚═╝██║
|
|
7
|
+
╚███████╗ ╚██████╔╝ ██║ ╚███║ ██║ ╚██████╔╝ ██║ ██║ ██║ ██║
|
|
8
|
+
╚══════╝ ╚═════╝ ╚═╝ ╚══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
Version 1.0.0-pre.7 / License MIT / Copyright (c) 2024 Edmund Hung
|
|
12
|
+
|
|
13
|
+
A type-safe form validation library utilizing web fundamentals to progressively enhance HTML Forms with full support for server frameworks like Remix route action and Next.js server actions.
|
|
14
|
+
|
|
15
|
+
> Getting Started
|
|
16
|
+
|
|
17
|
+
Check out the overview and tutorial at our website https://conform.guide
|
|
18
|
+
|
|
19
|
+
> Documentation
|
|
20
|
+
|
|
21
|
+
The documentation is divided into several sections:
|
|
22
|
+
|
|
23
|
+
* Overview: https://conform.guide/overview
|
|
24
|
+
* Example Usages: https://conform.guide/examples
|
|
25
|
+
* Complex structures: https://conform.guide/complex-structures
|
|
26
|
+
* UI Integrations: https://conform.guide/integrations
|
|
27
|
+
* Accessibility Guide: https://conform.guide/accessibility
|
|
28
|
+
* API Reference: https://conform.guide/references
|
|
29
|
+
|
|
30
|
+
> Support
|
|
31
|
+
|
|
32
|
+
To report a bug, please open an issue on the repository at https://github.com/edmundhung/conform. For feature requests and questions, you can post them in the Discussions section.
|
package/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { type Constraint, type
|
|
1
|
+
import { type Constraint, type Intent, type Submission } from '@conform-to/dom';
|
|
2
2
|
import * as yup from 'yup';
|
|
3
|
-
export declare function
|
|
3
|
+
export declare function getYupConstraint<Source extends yup.AnyObjectSchema>(source: Source): Record<string, Constraint>;
|
|
4
4
|
export declare function parseWithYup<Schema extends yup.AnyObjectSchema>(payload: FormData | URLSearchParams, config: {
|
|
5
|
-
schema: Schema | ((
|
|
5
|
+
schema: Schema | ((intent: Intent | null) => Schema);
|
|
6
6
|
async?: false;
|
|
7
7
|
}): Submission<yup.InferType<Schema>, string[]>;
|
|
8
8
|
export declare function parseWithYup<Schema extends yup.AnyObjectSchema>(payload: FormData | URLSearchParams, config: {
|
|
9
|
-
schema: Schema | ((
|
|
9
|
+
schema: Schema | ((intent: Intent | null) => Schema);
|
|
10
10
|
async: true;
|
|
11
11
|
}): Promise<Submission<yup.InferType<Schema>, string[]>>;
|
package/index.js
CHANGED
|
@@ -25,7 +25,7 @@ function _interopNamespace(e) {
|
|
|
25
25
|
|
|
26
26
|
var yup__namespace = /*#__PURE__*/_interopNamespace(yup);
|
|
27
27
|
|
|
28
|
-
function
|
|
28
|
+
function getYupConstraint(source) {
|
|
29
29
|
var description = source.describe();
|
|
30
30
|
return Object.fromEntries(Object.entries(description.fields).map(_ref => {
|
|
31
31
|
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8;
|
|
@@ -70,14 +70,18 @@ function getFieldsetConstraint(source) {
|
|
|
70
70
|
constraint.required = true;
|
|
71
71
|
break;
|
|
72
72
|
case 'min':
|
|
73
|
-
|
|
73
|
+
if (typeof constraint.min === 'string') {
|
|
74
|
+
throw new Error('min should not be a string');
|
|
75
|
+
}
|
|
74
76
|
if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) {
|
|
75
77
|
var _test$params7;
|
|
76
78
|
constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min);
|
|
77
79
|
}
|
|
78
80
|
break;
|
|
79
81
|
case 'max':
|
|
80
|
-
|
|
82
|
+
if (typeof constraint.max === 'string') {
|
|
83
|
+
throw new Error('max should not be a number');
|
|
84
|
+
}
|
|
81
85
|
if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) {
|
|
82
86
|
var _test$params9;
|
|
83
87
|
constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max);
|
|
@@ -92,8 +96,8 @@ function getFieldsetConstraint(source) {
|
|
|
92
96
|
}
|
|
93
97
|
function parseWithYup(payload, config) {
|
|
94
98
|
return dom.parse(payload, {
|
|
95
|
-
resolve(payload,
|
|
96
|
-
var schema = typeof config.schema === 'function' ? config.schema(
|
|
99
|
+
resolve(payload, intent) {
|
|
100
|
+
var schema = typeof config.schema === 'function' ? config.schema(intent) : config.schema;
|
|
97
101
|
var resolveData = value => ({
|
|
98
102
|
value
|
|
99
103
|
});
|
|
@@ -127,5 +131,5 @@ function parseWithYup(payload, config) {
|
|
|
127
131
|
});
|
|
128
132
|
}
|
|
129
133
|
|
|
130
|
-
exports.
|
|
134
|
+
exports.getYupConstraint = getYupConstraint;
|
|
131
135
|
exports.parseWithYup = parseWithYup;
|
package/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parse } from '@conform-to/dom';
|
|
2
2
|
import * as yup from 'yup';
|
|
3
3
|
|
|
4
|
-
function
|
|
4
|
+
function getYupConstraint(source) {
|
|
5
5
|
var description = source.describe();
|
|
6
6
|
return Object.fromEntries(Object.entries(description.fields).map(_ref => {
|
|
7
7
|
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8;
|
|
@@ -46,14 +46,18 @@ function getFieldsetConstraint(source) {
|
|
|
46
46
|
constraint.required = true;
|
|
47
47
|
break;
|
|
48
48
|
case 'min':
|
|
49
|
-
|
|
49
|
+
if (typeof constraint.min === 'string') {
|
|
50
|
+
throw new Error('min should not be a string');
|
|
51
|
+
}
|
|
50
52
|
if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) {
|
|
51
53
|
var _test$params7;
|
|
52
54
|
constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min);
|
|
53
55
|
}
|
|
54
56
|
break;
|
|
55
57
|
case 'max':
|
|
56
|
-
|
|
58
|
+
if (typeof constraint.max === 'string') {
|
|
59
|
+
throw new Error('max should not be a number');
|
|
60
|
+
}
|
|
57
61
|
if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) {
|
|
58
62
|
var _test$params9;
|
|
59
63
|
constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max);
|
|
@@ -68,8 +72,8 @@ function getFieldsetConstraint(source) {
|
|
|
68
72
|
}
|
|
69
73
|
function parseWithYup(payload, config) {
|
|
70
74
|
return parse(payload, {
|
|
71
|
-
resolve(payload,
|
|
72
|
-
var schema = typeof config.schema === 'function' ? config.schema(
|
|
75
|
+
resolve(payload, intent) {
|
|
76
|
+
var schema = typeof config.schema === 'function' ? config.schema(intent) : config.schema;
|
|
73
77
|
var resolveData = value => ({
|
|
74
78
|
value
|
|
75
79
|
});
|
|
@@ -103,4 +107,4 @@ function parseWithYup(payload, config) {
|
|
|
103
107
|
});
|
|
104
108
|
}
|
|
105
109
|
|
|
106
|
-
export {
|
|
110
|
+
export { getYupConstraint, 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.0-pre.
|
|
6
|
+
"version": "1.0.0-pre.7",
|
|
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.0-pre.
|
|
28
|
+
"@conform-to/dom": "1.0.0-pre.7",
|
|
29
29
|
"yup": ">=0.32.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
package/README.md
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
# @conform-to/yup
|
|
2
|
-
|
|
3
|
-
> [Conform](https://github.com/edmundhung/conform) helpers for integrating with [Yup](https://github.com/jquense/yup)
|
|
4
|
-
|
|
5
|
-
<!-- aside -->
|
|
6
|
-
|
|
7
|
-
## API Reference
|
|
8
|
-
|
|
9
|
-
- [getFieldsetConstraint](#getfieldsetconstraint)
|
|
10
|
-
- [parse](#parse)
|
|
11
|
-
|
|
12
|
-
<!-- /aside -->
|
|
13
|
-
|
|
14
|
-
### getFieldsetConstraint
|
|
15
|
-
|
|
16
|
-
This tries to infer constraint of each field based on the yup schema. This is useful for:
|
|
17
|
-
|
|
18
|
-
1. Making it easy to style input using CSS, e.g. `:required`
|
|
19
|
-
2. Having some basic validation working before/without JS.
|
|
20
|
-
|
|
21
|
-
```tsx
|
|
22
|
-
import { useForm } from '@conform-to/react';
|
|
23
|
-
import { getFieldsetConstraint } 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 Example() {
|
|
32
|
-
const [form, { email, password }] = useForm({
|
|
33
|
-
constraint: getFieldsetConstraint(schema),
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// ...
|
|
37
|
-
}
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
### parse
|
|
41
|
-
|
|
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`.
|
|
43
|
-
|
|
44
|
-
```tsx
|
|
45
|
-
import { useForm } from '@conform-to/react';
|
|
46
|
-
import { parse } from '@conform-to/yup';
|
|
47
|
-
import * as yup from 'yup';
|
|
48
|
-
|
|
49
|
-
const schema = yup.object({
|
|
50
|
-
email: yup.string().required(),
|
|
51
|
-
password: yup.string().required(),
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
function ExampleForm() {
|
|
55
|
-
const [form] = useForm({
|
|
56
|
-
onValidate({ formData }) {
|
|
57
|
-
return parse(formData, { schema });
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// ...
|
|
62
|
-
}
|
|
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 async function action({ 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.intent !== 'submit' || !submission.value) {
|
|
87
|
-
return submission;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// ...
|
|
91
|
-
}
|
|
92
|
-
```
|