remix-validated-form 0.0.2 → 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/.eslintcache +1 -0
- package/README.md +180 -1
- package/browser/ValidatedForm.d.ts +11 -0
- package/browser/ValidatedForm.js +69 -0
- package/browser/hooks.d.ts +8 -0
- package/browser/hooks.js +17 -0
- package/{src/index.ts → browser/index.d.ts} +0 -0
- package/browser/index.js +5 -0
- package/browser/internal/formContext.d.ts +13 -0
- package/browser/internal/formContext.js +7 -0
- package/browser/internal/util.d.ts +3 -0
- package/browser/internal/util.js +19 -0
- package/browser/server.d.ts +2 -0
- package/browser/server.js +2 -0
- package/browser/validation/types.d.ts +15 -0
- package/browser/validation/types.js +1 -0
- package/browser/validation/validation.test.d.ts +1 -0
- package/browser/validation/validation.test.js +120 -0
- package/browser/validation/withYup.d.ts +3 -0
- package/browser/validation/withYup.js +34 -0
- package/browser/validation/withZod.d.ts +3 -0
- package/browser/validation/withZod.js +35 -0
- package/build/ValidatedForm.d.ts +11 -0
- package/build/ValidatedForm.js +76 -0
- package/build/hooks.d.ts +8 -0
- package/build/hooks.js +23 -0
- package/build/index.d.ts +5 -0
- package/build/index.js +17 -0
- package/build/internal/formContext.d.ts +13 -0
- package/build/internal/formContext.js +10 -0
- package/build/internal/util.d.ts +3 -0
- package/build/internal/util.js +24 -0
- package/build/server.d.ts +2 -0
- package/build/server.js +6 -0
- package/build/validation/types.d.ts +15 -0
- package/build/validation/types.js +2 -0
- package/build/validation/validation.test.d.ts +1 -0
- package/build/validation/validation.test.js +141 -0
- package/build/validation/withYup.d.ts +3 -0
- package/build/validation/withYup.js +38 -0
- package/build/validation/withZod.d.ts +3 -0
- package/build/validation/withZod.js +39 -0
- package/package.json +7 -2
- package/.eslintrc.js +0 -46
- package/.github/workflows/test.yml +0 -35
- package/.husky/pre-commit +0 -4
- package/src/ValidatedForm.tsx +0 -130
- package/src/hooks.ts +0 -27
- package/src/internal/formContext.ts +0 -18
- package/src/internal/util.ts +0 -23
- package/src/server.ts +0 -5
- package/src/validation/types.ts +0 -12
- package/src/validation/validation.test.ts +0 -76
- package/src/validation/withYup.ts +0 -37
- package/test-app/README.md +0 -53
- package/test-app/app/components/Input.tsx +0 -24
- package/test-app/app/components/SubmitButton.tsx +0 -18
- package/test-app/app/entry.client.tsx +0 -4
- package/test-app/app/entry.server.tsx +0 -21
- package/test-app/app/root.tsx +0 -246
- package/test-app/app/routes/default-values.tsx +0 -34
- package/test-app/app/routes/index.tsx +0 -100
- package/test-app/app/routes/noscript.tsx +0 -10
- package/test-app/app/routes/submission.alt.tsx +0 -6
- package/test-app/app/routes/submission.fetcher.tsx +0 -6
- package/test-app/app/routes/submission.tsx +0 -47
- package/test-app/app/routes/validation.tsx +0 -40
- package/test-app/app/styles/dark.css +0 -7
- package/test-app/app/styles/demos/about.css +0 -26
- package/test-app/app/styles/demos/remix.css +0 -120
- package/test-app/app/styles/global.css +0 -98
- package/test-app/cypress/fixtures/example.json +0 -5
- package/test-app/cypress/integration/default-values.ts +0 -15
- package/test-app/cypress/integration/sanity.ts +0 -19
- package/test-app/cypress/integration/submission.ts +0 -26
- package/test-app/cypress/integration/validation.ts +0 -70
- package/test-app/cypress/plugins/config.ts +0 -38
- package/test-app/cypress/plugins/index.ts +0 -9
- package/test-app/cypress/support/commands/index.ts +0 -13
- package/test-app/cypress/support/commands/types.d.ts +0 -11
- package/test-app/cypress/support/index.ts +0 -20
- package/test-app/cypress/tsconfig.json +0 -11
- package/test-app/cypress.json +0 -3
- package/test-app/package-lock.json +0 -11675
- package/test-app/package.json +0 -40
- package/test-app/public/favicon.ico +0 -0
- package/test-app/remix.config.js +0 -10
- package/test-app/remix.env.d.ts +0 -2
- package/test-app/tsconfig.json +0 -18
- package/tsconfig.json +0 -15
@@ -0,0 +1,76 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.ValidatedForm = void 0;
|
7
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
8
|
+
const react_1 = require("@remix-run/react");
|
9
|
+
const react_2 = require("react");
|
10
|
+
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
11
|
+
const formContext_1 = require("./internal/formContext");
|
12
|
+
const util_1 = require("./internal/util");
|
13
|
+
function useFieldErrors(fetcher) {
|
14
|
+
const actionData = (0, react_1.useActionData)();
|
15
|
+
const dataToUse = fetcher ? fetcher.data : actionData;
|
16
|
+
const fieldErrorsFromAction = dataToUse === null || dataToUse === void 0 ? void 0 : dataToUse.fieldErrors;
|
17
|
+
const [fieldErrors, setFieldErrors] = (0, react_2.useState)(fieldErrorsFromAction !== null && fieldErrorsFromAction !== void 0 ? fieldErrorsFromAction : {});
|
18
|
+
(0, react_2.useEffect)(() => {
|
19
|
+
if (fieldErrorsFromAction)
|
20
|
+
setFieldErrors(fieldErrorsFromAction);
|
21
|
+
}, [fieldErrorsFromAction]);
|
22
|
+
return [fieldErrors, setFieldErrors];
|
23
|
+
}
|
24
|
+
const useIsSubmitting = (action, fetcher) => {
|
25
|
+
const actionForCurrentPage = (0, react_1.useFormAction)();
|
26
|
+
const pendingFormSubmit = (0, react_1.useTransition)().submission;
|
27
|
+
return fetcher
|
28
|
+
? fetcher.state === "submitting"
|
29
|
+
: pendingFormSubmit &&
|
30
|
+
pendingFormSubmit.action.endsWith(action !== null && action !== void 0 ? action : actionForCurrentPage);
|
31
|
+
};
|
32
|
+
const getDataFromForm = (el) => Object.fromEntries(new FormData(el));
|
33
|
+
function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }) {
|
34
|
+
var _a;
|
35
|
+
const [fieldErrors, setFieldErrors] = useFieldErrors(fetcher);
|
36
|
+
const isSubmitting = useIsSubmitting(action, fetcher);
|
37
|
+
const formRef = (0, react_2.useRef)(null);
|
38
|
+
const contextValue = (0, react_2.useMemo)(() => ({
|
39
|
+
fieldErrors,
|
40
|
+
action,
|
41
|
+
defaultValues,
|
42
|
+
isSubmitting: isSubmitting !== null && isSubmitting !== void 0 ? isSubmitting : false,
|
43
|
+
clearError: (fieldName) => {
|
44
|
+
setFieldErrors((prev) => (0, util_1.omit)(prev, fieldName));
|
45
|
+
},
|
46
|
+
validateField: (fieldName) => {
|
47
|
+
(0, tiny_invariant_1.default)(formRef.current, "Cannot find reference to form");
|
48
|
+
const { error } = validator.validateField(getDataFromForm(formRef.current), fieldName);
|
49
|
+
if (error) {
|
50
|
+
setFieldErrors((prev) => ({
|
51
|
+
...prev,
|
52
|
+
[fieldName]: error,
|
53
|
+
}));
|
54
|
+
}
|
55
|
+
},
|
56
|
+
}), [
|
57
|
+
fieldErrors,
|
58
|
+
action,
|
59
|
+
defaultValues,
|
60
|
+
isSubmitting,
|
61
|
+
setFieldErrors,
|
62
|
+
validator,
|
63
|
+
]);
|
64
|
+
const Form = (_a = fetcher === null || fetcher === void 0 ? void 0 : fetcher.Form) !== null && _a !== void 0 ? _a : react_1.Form;
|
65
|
+
return ((0, jsx_runtime_1.jsx)(Form, { ref: (0, util_1.mergeRefs)([formRef, formRefProp]), ...rest, action: action, onSubmit: (event) => {
|
66
|
+
const result = validator.validate(getDataFromForm(event.currentTarget));
|
67
|
+
if (result.error) {
|
68
|
+
event.preventDefault();
|
69
|
+
setFieldErrors(result.error);
|
70
|
+
}
|
71
|
+
else {
|
72
|
+
onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit(result.data, event);
|
73
|
+
}
|
74
|
+
}, children: (0, jsx_runtime_1.jsx)(formContext_1.FormContext.Provider, { value: contextValue, children: children }, void 0) }, void 0));
|
75
|
+
}
|
76
|
+
exports.ValidatedForm = ValidatedForm;
|
package/build/hooks.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
export declare const useField: (name: string) => {
|
2
|
+
error: string;
|
3
|
+
clearError: () => void;
|
4
|
+
validate: () => void;
|
5
|
+
defaultValue: any;
|
6
|
+
};
|
7
|
+
export declare const useFormContext: () => import("./internal/formContext").FormContextValue;
|
8
|
+
export declare const useIsSubmitting: () => boolean;
|
package/build/hooks.js
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useIsSubmitting = exports.useFormContext = exports.useField = void 0;
|
4
|
+
const react_1 = require("react");
|
5
|
+
const formContext_1 = require("./internal/formContext");
|
6
|
+
const useField = (name) => {
|
7
|
+
const { fieldErrors, clearError, validateField, defaultValues } = (0, react_1.useContext)(formContext_1.FormContext);
|
8
|
+
const field = (0, react_1.useMemo)(() => ({
|
9
|
+
error: fieldErrors[name],
|
10
|
+
clearError: () => {
|
11
|
+
clearError(name);
|
12
|
+
},
|
13
|
+
validate: () => validateField(name),
|
14
|
+
defaultValue: defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues[name],
|
15
|
+
}), [clearError, defaultValues, fieldErrors, name, validateField]);
|
16
|
+
return field;
|
17
|
+
};
|
18
|
+
exports.useField = useField;
|
19
|
+
// test commit
|
20
|
+
const useFormContext = () => (0, react_1.useContext)(formContext_1.FormContext);
|
21
|
+
exports.useFormContext = useFormContext;
|
22
|
+
const useIsSubmitting = () => (0, exports.useFormContext)().isSubmitting;
|
23
|
+
exports.useIsSubmitting = useIsSubmitting;
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5
|
+
}) : (function(o, m, k, k2) {
|
6
|
+
if (k2 === undefined) k2 = k;
|
7
|
+
o[k2] = m[k];
|
8
|
+
}));
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
11
|
+
};
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
13
|
+
__exportStar(require("./hooks"), exports);
|
14
|
+
__exportStar(require("./server"), exports);
|
15
|
+
__exportStar(require("./ValidatedForm"), exports);
|
16
|
+
__exportStar(require("./validation/types"), exports);
|
17
|
+
__exportStar(require("./validation/withYup"), exports);
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/// <reference types="react" />
|
2
|
+
import { FieldErrors } from "../validation/types";
|
3
|
+
export declare type FormContextValue = {
|
4
|
+
fieldErrors: FieldErrors;
|
5
|
+
clearError: (...names: string[]) => void;
|
6
|
+
validateField: (fieldName: string) => void;
|
7
|
+
action?: string;
|
8
|
+
isSubmitting: boolean;
|
9
|
+
defaultValues?: {
|
10
|
+
[fieldName: string]: any;
|
11
|
+
};
|
12
|
+
};
|
13
|
+
export declare const FormContext: import("react").Context<FormContextValue>;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.FormContext = void 0;
|
4
|
+
const react_1 = require("react");
|
5
|
+
exports.FormContext = (0, react_1.createContext)({
|
6
|
+
fieldErrors: {},
|
7
|
+
clearError: () => { },
|
8
|
+
validateField: () => { },
|
9
|
+
isSubmitting: false,
|
10
|
+
});
|
@@ -0,0 +1,24 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.mergeRefs = exports.omit = void 0;
|
4
|
+
const omit = (obj, ...keys) => {
|
5
|
+
const result = { ...obj };
|
6
|
+
for (const key of keys) {
|
7
|
+
delete result[key];
|
8
|
+
}
|
9
|
+
return result;
|
10
|
+
};
|
11
|
+
exports.omit = omit;
|
12
|
+
const mergeRefs = (refs) => {
|
13
|
+
return (value) => {
|
14
|
+
refs.filter(Boolean).forEach((ref) => {
|
15
|
+
if (typeof ref === "function") {
|
16
|
+
ref(value);
|
17
|
+
}
|
18
|
+
else if (ref != null) {
|
19
|
+
ref.current = value;
|
20
|
+
}
|
21
|
+
});
|
22
|
+
};
|
23
|
+
};
|
24
|
+
exports.mergeRefs = mergeRefs;
|
package/build/server.js
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.validationError = void 0;
|
4
|
+
const server_runtime_1 = require("@remix-run/server-runtime");
|
5
|
+
const validationError = (errors) => (0, server_runtime_1.json)({ fieldErrors: errors }, { status: 422 });
|
6
|
+
exports.validationError = validationError;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
export declare type FieldErrors = Record<string, string>;
|
2
|
+
export declare type ValidationResult<DataType> = {
|
3
|
+
data: DataType;
|
4
|
+
error: undefined;
|
5
|
+
} | {
|
6
|
+
error: FieldErrors;
|
7
|
+
data: undefined;
|
8
|
+
};
|
9
|
+
export declare type ValidateFieldResult = {
|
10
|
+
error?: string;
|
11
|
+
};
|
12
|
+
export declare type Validator<DataType> = {
|
13
|
+
validate: (unvalidatedData: unknown) => ValidationResult<DataType>;
|
14
|
+
validateField: (unvalidatedData: unknown, field: string) => ValidateFieldResult;
|
15
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,141 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5
|
+
}) : (function(o, m, k, k2) {
|
6
|
+
if (k2 === undefined) k2 = k;
|
7
|
+
o[k2] = m[k];
|
8
|
+
}));
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11
|
+
}) : function(o, v) {
|
12
|
+
o["default"] = v;
|
13
|
+
});
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
15
|
+
if (mod && mod.__esModule) return mod;
|
16
|
+
var result = {};
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18
|
+
__setModuleDefault(result, mod);
|
19
|
+
return result;
|
20
|
+
};
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
22
|
+
const yup = __importStar(require("yup"));
|
23
|
+
const zod_1 = require("zod");
|
24
|
+
const __1 = require("..");
|
25
|
+
const withZod_1 = require("./withZod");
|
26
|
+
const validationTestCases = [
|
27
|
+
{
|
28
|
+
name: "yup",
|
29
|
+
validator: (0, __1.withYup)(yup.object({
|
30
|
+
firstName: yup.string().required(),
|
31
|
+
lastName: yup.string().required(),
|
32
|
+
age: yup.number(),
|
33
|
+
})),
|
34
|
+
},
|
35
|
+
{
|
36
|
+
name: "zod",
|
37
|
+
validator: (0, withZod_1.withZod)(zod_1.z.object({
|
38
|
+
firstName: zod_1.z.string().nonempty(),
|
39
|
+
lastName: zod_1.z.string().nonempty(),
|
40
|
+
age: zod_1.z.optional(zod_1.z.number()),
|
41
|
+
})),
|
42
|
+
},
|
43
|
+
];
|
44
|
+
// Not going to enforce exact error strings here
|
45
|
+
const anyString = expect.any(String);
|
46
|
+
describe("Validation", () => {
|
47
|
+
describe.each(validationTestCases)("Adapter for $name", ({ validator }) => {
|
48
|
+
describe("validate", () => {
|
49
|
+
it("should return the data when valid", () => {
|
50
|
+
const obj = {
|
51
|
+
firstName: "John",
|
52
|
+
lastName: "Doe",
|
53
|
+
age: 30,
|
54
|
+
};
|
55
|
+
expect(validator.validate(obj)).toEqual({
|
56
|
+
data: obj,
|
57
|
+
error: undefined,
|
58
|
+
});
|
59
|
+
});
|
60
|
+
it("should return field errors when invalid", () => {
|
61
|
+
const obj = { age: "hi!" };
|
62
|
+
expect(validator.validate(obj)).toEqual({
|
63
|
+
data: undefined,
|
64
|
+
error: {
|
65
|
+
firstName: anyString,
|
66
|
+
lastName: anyString,
|
67
|
+
age: anyString,
|
68
|
+
},
|
69
|
+
});
|
70
|
+
});
|
71
|
+
});
|
72
|
+
describe("validateField", () => {
|
73
|
+
it("should not return an error if field is valid", () => {
|
74
|
+
const obj = {
|
75
|
+
firstName: "John",
|
76
|
+
lastName: {}, // invalid, but we should only be validating firstName
|
77
|
+
};
|
78
|
+
expect(validator.validateField(obj, "firstName")).toEqual({
|
79
|
+
error: undefined,
|
80
|
+
});
|
81
|
+
});
|
82
|
+
it("should return an error if field is invalid", () => {
|
83
|
+
const obj = { firstName: "John", lastName: {} };
|
84
|
+
expect(validator.validateField(obj, "lastName")).toEqual({
|
85
|
+
error: anyString,
|
86
|
+
});
|
87
|
+
});
|
88
|
+
});
|
89
|
+
});
|
90
|
+
});
|
91
|
+
describe("withZod", () => {
|
92
|
+
it("returns coherent errors for complex schemas", () => {
|
93
|
+
const schema = zod_1.z.union([
|
94
|
+
zod_1.z.object({
|
95
|
+
type: zod_1.z.literal("foo"),
|
96
|
+
foo: zod_1.z.string(),
|
97
|
+
}),
|
98
|
+
zod_1.z.object({
|
99
|
+
type: zod_1.z.literal("bar"),
|
100
|
+
bar: zod_1.z.string(),
|
101
|
+
}),
|
102
|
+
]);
|
103
|
+
const obj = {
|
104
|
+
type: "foo",
|
105
|
+
bar: 123,
|
106
|
+
foo: 123,
|
107
|
+
};
|
108
|
+
expect((0, withZod_1.withZod)(schema).validate(obj)).toEqual({
|
109
|
+
data: undefined,
|
110
|
+
error: {
|
111
|
+
type: anyString,
|
112
|
+
bar: anyString,
|
113
|
+
foo: anyString,
|
114
|
+
},
|
115
|
+
});
|
116
|
+
});
|
117
|
+
it("returns errors for fields that are unions", () => {
|
118
|
+
const schema = zod_1.z.object({
|
119
|
+
field1: zod_1.z.union([zod_1.z.literal("foo"), zod_1.z.literal("bar")]),
|
120
|
+
field2: zod_1.z.union([zod_1.z.literal("foo"), zod_1.z.literal("bar")]),
|
121
|
+
});
|
122
|
+
const obj = {
|
123
|
+
field1: "a value",
|
124
|
+
// field2 missing
|
125
|
+
};
|
126
|
+
const validator = (0, withZod_1.withZod)(schema);
|
127
|
+
expect(validator.validate(obj)).toEqual({
|
128
|
+
data: undefined,
|
129
|
+
error: {
|
130
|
+
field1: anyString,
|
131
|
+
field2: anyString,
|
132
|
+
},
|
133
|
+
});
|
134
|
+
expect(validator.validateField(obj, "field1")).toEqual({
|
135
|
+
error: anyString,
|
136
|
+
});
|
137
|
+
expect(validator.validateField(obj, "field2")).toEqual({
|
138
|
+
error: anyString,
|
139
|
+
});
|
140
|
+
});
|
141
|
+
});
|
@@ -0,0 +1,38 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.withYup = void 0;
|
4
|
+
const validationErrorToFieldErrors = (error) => {
|
5
|
+
const fieldErrors = {};
|
6
|
+
error.inner.forEach((innerError) => {
|
7
|
+
if (!innerError.path)
|
8
|
+
return;
|
9
|
+
fieldErrors[innerError.path] = innerError.message;
|
10
|
+
});
|
11
|
+
return fieldErrors;
|
12
|
+
};
|
13
|
+
const withYup = (validationSchema) => ({
|
14
|
+
validate: (data) => {
|
15
|
+
try {
|
16
|
+
const validated = validationSchema.validateSync(data, {
|
17
|
+
abortEarly: false,
|
18
|
+
});
|
19
|
+
return { data: validated, error: undefined };
|
20
|
+
}
|
21
|
+
catch (err) {
|
22
|
+
return {
|
23
|
+
error: validationErrorToFieldErrors(err),
|
24
|
+
data: undefined,
|
25
|
+
};
|
26
|
+
}
|
27
|
+
},
|
28
|
+
validateField: (data, field) => {
|
29
|
+
try {
|
30
|
+
validationSchema.validateSyncAt(field, data);
|
31
|
+
return {};
|
32
|
+
}
|
33
|
+
catch (err) {
|
34
|
+
return { error: err.message };
|
35
|
+
}
|
36
|
+
},
|
37
|
+
});
|
38
|
+
exports.withYup = withYup;
|
@@ -0,0 +1,39 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.withZod = void 0;
|
4
|
+
const getIssuesForError = (err) => {
|
5
|
+
return err.issues.flatMap((issue) => {
|
6
|
+
if ("unionErrors" in issue) {
|
7
|
+
return issue.unionErrors.flatMap((err) => getIssuesForError(err));
|
8
|
+
}
|
9
|
+
else {
|
10
|
+
return [issue];
|
11
|
+
}
|
12
|
+
});
|
13
|
+
};
|
14
|
+
function withZod(zodSchema) {
|
15
|
+
return {
|
16
|
+
validate: (value) => {
|
17
|
+
const result = zodSchema.safeParse(value);
|
18
|
+
if (result.success)
|
19
|
+
return { data: result.data, error: undefined };
|
20
|
+
const fieldErrors = {};
|
21
|
+
getIssuesForError(result.error).forEach((issue) => {
|
22
|
+
const path = issue.path.join(".");
|
23
|
+
if (!fieldErrors[path])
|
24
|
+
fieldErrors[path] = issue.message;
|
25
|
+
});
|
26
|
+
return { error: fieldErrors, data: undefined };
|
27
|
+
},
|
28
|
+
validateField: (data, field) => {
|
29
|
+
var _a;
|
30
|
+
const result = zodSchema.safeParse(data);
|
31
|
+
if (result.success)
|
32
|
+
return { error: undefined };
|
33
|
+
return {
|
34
|
+
error: (_a = getIssuesForError(result.error).find((issue) => issue.path.join(".") === field)) === null || _a === void 0 ? void 0 : _a.message,
|
35
|
+
};
|
36
|
+
},
|
37
|
+
};
|
38
|
+
}
|
39
|
+
exports.withZod = withZod;
|
package/package.json
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
{
|
2
2
|
"name": "remix-validated-form",
|
3
|
-
"version": "
|
3
|
+
"version": "1.1.0",
|
4
4
|
"description": "Form component and utils for easy form validation in remix",
|
5
5
|
"browser": "./browser/index.js",
|
6
6
|
"main": "./build/index.js",
|
7
|
+
"repository": {
|
8
|
+
"type": "git",
|
9
|
+
"url": "https://github.com/airjp73/remix-validated-form"
|
10
|
+
},
|
7
11
|
"sideEffects": false,
|
8
12
|
"scripts": {
|
9
13
|
"build": "npm run build:browser && npm run build:main && npm run build:tests",
|
@@ -60,7 +64,8 @@
|
|
60
64
|
"react": "^17.0.2",
|
61
65
|
"ts-jest": "^27.0.7",
|
62
66
|
"typescript": "^4.5.2",
|
63
|
-
"yup": "^0.32.11"
|
67
|
+
"yup": "^0.32.11",
|
68
|
+
"zod": "^3.11.6"
|
64
69
|
},
|
65
70
|
"dependencies": {
|
66
71
|
"tiny-invariant": "^1.2.0"
|
package/.eslintrc.js
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @type {import('eslint').Linter.Config}
|
3
|
-
*/
|
4
|
-
module.exports = {
|
5
|
-
ignorePatterns: [
|
6
|
-
"node_modules/",
|
7
|
-
".cache/",
|
8
|
-
"browser/",
|
9
|
-
"build/",
|
10
|
-
"test-app/build",
|
11
|
-
"test-app/remix-validated-form",
|
12
|
-
],
|
13
|
-
extends: ["react-app"],
|
14
|
-
plugins: ["prettier"],
|
15
|
-
rules: {
|
16
|
-
"import/no-anonymous-default-export": "off",
|
17
|
-
"import/order": [
|
18
|
-
"warn",
|
19
|
-
{
|
20
|
-
groups: [
|
21
|
-
"builtin",
|
22
|
-
"external",
|
23
|
-
"internal",
|
24
|
-
"unknown",
|
25
|
-
"parent",
|
26
|
-
"sibling",
|
27
|
-
"index",
|
28
|
-
],
|
29
|
-
alphabetize: {
|
30
|
-
order: "asc",
|
31
|
-
caseInsensitive: true,
|
32
|
-
},
|
33
|
-
"newlines-between": "never",
|
34
|
-
},
|
35
|
-
],
|
36
|
-
"prettier/prettier": "error",
|
37
|
-
},
|
38
|
-
overrides: [
|
39
|
-
{
|
40
|
-
files: ["./cypress/**"],
|
41
|
-
rules: {
|
42
|
-
"@typescript-eslint/no-unused-expressions": "off",
|
43
|
-
},
|
44
|
-
},
|
45
|
-
],
|
46
|
-
};
|
@@ -1,35 +0,0 @@
|
|
1
|
-
name: Test and build
|
2
|
-
|
3
|
-
on:
|
4
|
-
push:
|
5
|
-
branches:
|
6
|
-
- "main"
|
7
|
-
pull_request:
|
8
|
-
branches:
|
9
|
-
- "main"
|
10
|
-
|
11
|
-
jobs:
|
12
|
-
build:
|
13
|
-
runs-on: ubuntu-latest
|
14
|
-
|
15
|
-
steps:
|
16
|
-
- uses: actions/checkout@v2
|
17
|
-
|
18
|
-
- name: Install Deps
|
19
|
-
run: npm install
|
20
|
-
|
21
|
-
- name: Lint
|
22
|
-
run: npm run lint
|
23
|
-
|
24
|
-
- name: Jest Tests
|
25
|
-
run: npm run test
|
26
|
-
|
27
|
-
- name: Build
|
28
|
-
run: npm run build
|
29
|
-
|
30
|
-
- name: Cypress Tests
|
31
|
-
uses: cypress-io/github-action@v2
|
32
|
-
with:
|
33
|
-
working-directory: ./test-app
|
34
|
-
start: npm run dev
|
35
|
-
wait-on: http://localhost:3000
|
package/.husky/pre-commit
DELETED