remix-validated-form 1.1.1-beta.0 → 2.0.1-beta.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/.turbo/turbo-build.log +9 -0
- package/.turbo/turbo-dev.log +0 -0
- package/browser/ValidatedForm.d.ts +22 -0
- package/browser/ValidatedForm.js +4 -1
- package/browser/hooks.d.ts +28 -3
- package/browser/hooks.js +12 -1
- package/browser/index.d.ts +2 -0
- package/browser/index.js +1 -0
- package/browser/{flatten.d.ts → internal/flatten.d.ts} +2 -3
- package/browser/{flatten.js → internal/flatten.js} +2 -8
- package/browser/internal/formContext.d.ts +18 -0
- package/browser/internal/formContext.js +0 -0
- package/browser/internal/util.d.ts +0 -0
- package/browser/internal/util.js +0 -0
- package/browser/server.d.ts +5 -0
- package/browser/server.js +5 -0
- package/browser/test-data/testFormData.d.ts +15 -0
- package/browser/test-data/testFormData.js +46 -0
- package/browser/validation/createValidator.d.ts +5 -1
- package/browser/validation/createValidator.js +15 -4
- package/browser/validation/types.d.ts +9 -0
- package/browser/validation/types.js +0 -0
- package/browser/validation/validation.test.d.ts +0 -0
- package/browser/validation/validation.test.js +65 -0
- package/browser/validation/withYup.d.ts +3 -0
- package/browser/validation/withYup.js +3 -0
- package/browser/validation/withZod.d.ts +3 -0
- package/browser/validation/withZod.js +3 -0
- package/build/ValidatedForm.d.ts +22 -0
- package/build/ValidatedForm.js +4 -1
- package/build/hooks.d.ts +28 -3
- package/build/hooks.js +12 -1
- package/build/index.d.ts +2 -0
- package/build/index.js +1 -0
- package/build/{flatten.d.ts → internal/flatten.d.ts} +2 -3
- package/build/{flatten.js → internal/flatten.js} +4 -10
- package/build/internal/formContext.d.ts +18 -0
- package/build/internal/formContext.js +0 -0
- package/build/internal/util.d.ts +0 -0
- package/build/internal/util.js +0 -0
- package/build/server.d.ts +5 -0
- package/build/server.js +5 -0
- package/build/test-data/testFormData.d.ts +15 -0
- package/build/test-data/testFormData.js +50 -0
- package/build/validation/createValidator.d.ts +5 -1
- package/build/validation/createValidator.js +15 -4
- package/build/validation/types.d.ts +9 -0
- package/build/validation/types.js +0 -0
- package/build/validation/validation.test.d.ts +0 -0
- package/build/validation/validation.test.js +65 -0
- package/build/validation/withYup.d.ts +3 -0
- package/build/validation/withYup.js +3 -0
- package/build/validation/withZod.d.ts +3 -0
- package/build/validation/withZod.js +3 -0
- package/jest.config.js +5 -0
- package/package.json +7 -32
- package/src/ValidatedForm.tsx +151 -0
- package/src/hooks.ts +60 -0
- package/src/index.ts +8 -0
- package/src/internal/flatten.ts +48 -0
- package/src/internal/formContext.ts +36 -0
- package/src/internal/util.ts +23 -0
- package/src/server.ts +10 -0
- package/src/test-data/testFormData.ts +55 -0
- package/src/validation/createValidator.ts +24 -0
- package/src/validation/types.ts +26 -0
- package/src/validation/validation.test.ts +317 -0
- package/src/validation/withYup.ts +43 -0
- package/src/validation/withZod.ts +51 -0
- package/tsconfig.json +5 -0
- package/.eslintcache +0 -1
- package/.eslintignore +0 -1
- package/.prettierignore +0 -10
- package/LICENSE +0 -21
- package/README.md +0 -230
- package/sample-app/.env +0 -7
- package/sample-app/README.md +0 -53
- package/sample-app/app/components/ErrorBox.tsx +0 -34
- package/sample-app/app/components/FormInput.tsx +0 -40
- package/sample-app/app/components/FormSelect.tsx +0 -37
- package/sample-app/app/components/SubjectForm.tsx +0 -150
- package/sample-app/app/entry.client.tsx +0 -4
- package/sample-app/app/entry.server.tsx +0 -21
- package/sample-app/app/root.tsx +0 -92
- package/sample-app/app/routes/index.tsx +0 -5
- package/sample-app/app/routes/subjects/$id.edit.tsx +0 -100
- package/sample-app/app/routes/subjects/index.tsx +0 -112
- package/sample-app/app/routes/subjects/new.tsx +0 -48
- package/sample-app/app/services/db.server.ts +0 -23
- package/sample-app/app/types.ts +0 -6
- package/sample-app/package-lock.json +0 -4617
- package/sample-app/package.json +0 -36
- package/sample-app/prisma/dev.db +0 -0
- package/sample-app/prisma/schema.prisma +0 -34
- package/sample-app/public/favicon.ico +0 -0
- package/sample-app/remix.config.js +0 -10
- package/sample-app/remix.env.d.ts +0 -2
File without changes
|
@@ -2,10 +2,32 @@ import { Form as RemixForm, useFetcher } from "@remix-run/react";
|
|
2
2
|
import React, { ComponentProps } from "react";
|
3
3
|
import { Validator } from "./validation/types";
|
4
4
|
export declare type FormProps<DataType> = {
|
5
|
+
/**
|
6
|
+
* A `Validator` object that describes how to validate the form.
|
7
|
+
*/
|
5
8
|
validator: Validator<DataType>;
|
9
|
+
/**
|
10
|
+
* A submit callback that gets called when the form is submitted
|
11
|
+
* after all validations have been run.
|
12
|
+
*/
|
6
13
|
onSubmit?: (data: DataType, event: React.FormEvent<HTMLFormElement>) => void;
|
14
|
+
/**
|
15
|
+
* Allows you to provide a `fetcher` from remix's `useFetcher` hook.
|
16
|
+
* The form will use the fetcher for loading states, action data, etc
|
17
|
+
* instead of the default form action.
|
18
|
+
*/
|
7
19
|
fetcher?: ReturnType<typeof useFetcher>;
|
20
|
+
/**
|
21
|
+
* Accepts an object of default values for the form
|
22
|
+
* that will automatically be propagated to the form fields via `useField`.
|
23
|
+
*/
|
8
24
|
defaultValues?: Partial<DataType>;
|
25
|
+
/**
|
26
|
+
* A ref to the form element.
|
27
|
+
*/
|
9
28
|
formRef?: React.RefObject<HTMLFormElement>;
|
10
29
|
} & Omit<ComponentProps<typeof RemixForm>, "onSubmit">;
|
30
|
+
/**
|
31
|
+
* The primary form component of `remix-validated-form`.
|
32
|
+
*/
|
11
33
|
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }: FormProps<DataType>): JSX.Element;
|
package/browser/ValidatedForm.js
CHANGED
@@ -23,7 +23,10 @@ const useIsSubmitting = (action, fetcher) => {
|
|
23
23
|
: pendingFormSubmit &&
|
24
24
|
pendingFormSubmit.action === (action !== null && action !== void 0 ? action : actionForCurrentPage);
|
25
25
|
};
|
26
|
-
const getDataFromForm = (el) =>
|
26
|
+
const getDataFromForm = (el) => new FormData(el);
|
27
|
+
/**
|
28
|
+
* The primary form component of `remix-validated-form`.
|
29
|
+
*/
|
27
30
|
export function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }) {
|
28
31
|
var _a;
|
29
32
|
const [fieldErrors, setFieldErrors] = useFieldErrors(fetcher);
|
package/browser/hooks.d.ts
CHANGED
@@ -1,8 +1,33 @@
|
|
1
|
-
export declare
|
2
|
-
|
1
|
+
export declare type FieldProps = {
|
2
|
+
/**
|
3
|
+
* The validation error message if there is one.
|
4
|
+
*/
|
5
|
+
error?: string;
|
6
|
+
/**
|
7
|
+
* Clears the error message.
|
8
|
+
*/
|
3
9
|
clearError: () => void;
|
10
|
+
/**
|
11
|
+
* Validates the field.
|
12
|
+
*/
|
4
13
|
validate: () => void;
|
5
|
-
|
14
|
+
/**
|
15
|
+
* The default value of the field, if there is one.
|
16
|
+
*/
|
17
|
+
defaultValue?: any;
|
6
18
|
};
|
19
|
+
/**
|
20
|
+
* Provides the data and helpers necessary to set up a field.
|
21
|
+
*/
|
22
|
+
export declare const useField: (name: string) => FieldProps;
|
23
|
+
/**
|
24
|
+
* Provides access to the entire form context.
|
25
|
+
* This is not usually necessary, but can be useful for advanced use cases.
|
26
|
+
*/
|
7
27
|
export declare const useFormContext: () => import("./internal/formContext").FormContextValue;
|
28
|
+
/**
|
29
|
+
* Returns whether or not the parent form is currently being submitted.
|
30
|
+
* This is different from remix's `useTransition().submission` in that it
|
31
|
+
* is aware of what form it's in and when _that_ form is being submitted.
|
32
|
+
*/
|
8
33
|
export declare const useIsSubmitting: () => boolean;
|
package/browser/hooks.js
CHANGED
@@ -2,6 +2,9 @@ import get from "lodash/get";
|
|
2
2
|
import toPath from "lodash/toPath";
|
3
3
|
import { useContext, useMemo } from "react";
|
4
4
|
import { FormContext } from "./internal/formContext";
|
5
|
+
/**
|
6
|
+
* Provides the data and helpers necessary to set up a field.
|
7
|
+
*/
|
5
8
|
export const useField = (name) => {
|
6
9
|
const { fieldErrors, clearError, validateField, defaultValues } = useContext(FormContext);
|
7
10
|
const field = useMemo(() => ({
|
@@ -16,6 +19,14 @@ export const useField = (name) => {
|
|
16
19
|
}), [clearError, defaultValues, fieldErrors, name, validateField]);
|
17
20
|
return field;
|
18
21
|
};
|
19
|
-
|
22
|
+
/**
|
23
|
+
* Provides access to the entire form context.
|
24
|
+
* This is not usually necessary, but can be useful for advanced use cases.
|
25
|
+
*/
|
20
26
|
export const useFormContext = () => useContext(FormContext);
|
27
|
+
/**
|
28
|
+
* Returns whether or not the parent form is currently being submitted.
|
29
|
+
* This is different from remix's `useTransition().submission` in that it
|
30
|
+
* is aware of what form it's in and when _that_ form is being submitted.
|
31
|
+
*/
|
21
32
|
export const useIsSubmitting = () => useFormContext().isSubmitting;
|
package/browser/index.d.ts
CHANGED
package/browser/index.js
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
import { GenericObject } from "
|
2
|
-
|
3
|
-
export declare function unflatten(params: GenericObject): {};
|
1
|
+
import { GenericObject } from "..";
|
2
|
+
export declare const objectFromPathEntries: (entries: [string, any][]) => {};
|
4
3
|
/** Flatten an object so there are no nested objects or arrays */
|
5
4
|
export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
|
@@ -1,18 +1,12 @@
|
|
1
|
-
// `flatten`
|
1
|
+
// `flatten` is taken from https://github.com/richie5um/FlattenJS. Decided to implement them here instead of using that package because this is a core functionality of the library and this will add more flexibility in case we need to change the implementation.
|
2
2
|
import assign from "lodash/assign";
|
3
3
|
import isArray from "lodash/isArray";
|
4
4
|
import isObject from "lodash/isObject";
|
5
5
|
import keys from "lodash/keys";
|
6
6
|
import mapKeys from "lodash/mapKeys";
|
7
|
-
import reduce from "lodash/reduce";
|
8
7
|
import set from "lodash/set";
|
9
8
|
import transform from "lodash/transform";
|
10
|
-
|
11
|
-
export function unflatten(params) {
|
12
|
-
return reduce(params, function (result, value, key) {
|
13
|
-
return set(result, key, value);
|
14
|
-
}, {});
|
15
|
-
}
|
9
|
+
export const objectFromPathEntries = (entries) => entries.reduce((acc, [key, value]) => set(acc, key, value), {});
|
16
10
|
/** Flatten an object so there are no nested objects or arrays */
|
17
11
|
export function flatten(obj, preserveEmpty = false) {
|
18
12
|
return transform(obj, function (result, value, key) {
|
@@ -1,11 +1,29 @@
|
|
1
1
|
/// <reference types="react" />
|
2
2
|
import { FieldErrors } from "../validation/types";
|
3
3
|
export declare type FormContextValue = {
|
4
|
+
/**
|
5
|
+
* All the errors in all the fields in the form.
|
6
|
+
*/
|
4
7
|
fieldErrors: FieldErrors;
|
8
|
+
/**
|
9
|
+
* Clear the errors of the specified fields.
|
10
|
+
*/
|
5
11
|
clearError: (...names: string[]) => void;
|
12
|
+
/**
|
13
|
+
* Validate the specified field.
|
14
|
+
*/
|
6
15
|
validateField: (fieldName: string) => void;
|
16
|
+
/**
|
17
|
+
* The `action` prop of the form.
|
18
|
+
*/
|
7
19
|
action?: string;
|
20
|
+
/**
|
21
|
+
* Whether or not the form is submitting.
|
22
|
+
*/
|
8
23
|
isSubmitting: boolean;
|
24
|
+
/**
|
25
|
+
* The default values of the form.
|
26
|
+
*/
|
9
27
|
defaultValues?: {
|
10
28
|
[fieldName: string]: any;
|
11
29
|
};
|
File without changes
|
File without changes
|
package/browser/internal/util.js
CHANGED
File without changes
|
package/browser/server.d.ts
CHANGED
@@ -1,2 +1,7 @@
|
|
1
1
|
import { FieldErrors } from "./validation/types";
|
2
|
+
/**
|
3
|
+
* Takes the errors from a `Validator` and returns a `Response`.
|
4
|
+
* The `ValidatedForm` on the frontend will automatically display the errors
|
5
|
+
* if this is returned from the action.
|
6
|
+
*/
|
2
7
|
export declare const validationError: (errors: FieldErrors) => Response;
|
package/browser/server.js
CHANGED
@@ -1,2 +1,7 @@
|
|
1
1
|
import { json } from "@remix-run/server-runtime";
|
2
|
+
/**
|
3
|
+
* Takes the errors from a `Validator` and returns a `Response`.
|
4
|
+
* The `ValidatedForm` on the frontend will automatically display the errors
|
5
|
+
* if this is returned from the action.
|
6
|
+
*/
|
2
7
|
export const validationError = (errors) => json({ fieldErrors: errors }, { status: 422 });
|
@@ -0,0 +1,15 @@
|
|
1
|
+
export declare class TestFormData implements FormData {
|
2
|
+
private _params;
|
3
|
+
constructor(body?: string);
|
4
|
+
append(name: string, value: string | Blob, fileName?: string): void;
|
5
|
+
delete(name: string): void;
|
6
|
+
get(name: string): FormDataEntryValue | null;
|
7
|
+
getAll(name: string): FormDataEntryValue[];
|
8
|
+
has(name: string): boolean;
|
9
|
+
set(name: string, value: string | Blob, fileName?: string): void;
|
10
|
+
forEach(callbackfn: (value: FormDataEntryValue, key: string, parent: FormData) => void, thisArg?: any): void;
|
11
|
+
entries(): IterableIterator<[string, FormDataEntryValue]>;
|
12
|
+
keys(): IterableIterator<string>;
|
13
|
+
values(): IterableIterator<FormDataEntryValue>;
|
14
|
+
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
|
15
|
+
}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
// Copied from remix to use in tests
|
2
|
+
// https://github.com/remix-run/remix/blob/a69a631cb5add72d5fb24211ab2a0be367b6f2fd/packages/remix-node/form-data.ts
|
3
|
+
export class TestFormData {
|
4
|
+
constructor(body) {
|
5
|
+
this._params = new URLSearchParams(body);
|
6
|
+
}
|
7
|
+
append(name, value, fileName) {
|
8
|
+
if (typeof value !== "string") {
|
9
|
+
throw new Error("formData.append can only accept a string");
|
10
|
+
}
|
11
|
+
this._params.append(name, value);
|
12
|
+
}
|
13
|
+
delete(name) {
|
14
|
+
this._params.delete(name);
|
15
|
+
}
|
16
|
+
get(name) {
|
17
|
+
return this._params.get(name);
|
18
|
+
}
|
19
|
+
getAll(name) {
|
20
|
+
return this._params.getAll(name);
|
21
|
+
}
|
22
|
+
has(name) {
|
23
|
+
return this._params.has(name);
|
24
|
+
}
|
25
|
+
set(name, value, fileName) {
|
26
|
+
if (typeof value !== "string") {
|
27
|
+
throw new Error("formData.set can only accept a string");
|
28
|
+
}
|
29
|
+
this._params.set(name, value);
|
30
|
+
}
|
31
|
+
forEach(callbackfn, thisArg) {
|
32
|
+
this._params.forEach(callbackfn, thisArg);
|
33
|
+
}
|
34
|
+
entries() {
|
35
|
+
return this._params.entries();
|
36
|
+
}
|
37
|
+
keys() {
|
38
|
+
return this._params.keys();
|
39
|
+
}
|
40
|
+
values() {
|
41
|
+
return this._params.values();
|
42
|
+
}
|
43
|
+
*[Symbol.iterator]() {
|
44
|
+
yield* this._params;
|
45
|
+
}
|
46
|
+
}
|
@@ -1,3 +1,7 @@
|
|
1
1
|
import { Validator } from "..";
|
2
|
-
/**
|
2
|
+
/**
|
3
|
+
* Used to create a validator for a form.
|
4
|
+
* It provides built-in handling for unflattening nested objects and
|
5
|
+
* extracting the values from FormData.
|
6
|
+
*/
|
3
7
|
export declare function createValidator<T>(validator: Validator<T>): Validator<T>;
|
@@ -1,8 +1,19 @@
|
|
1
|
-
import {
|
2
|
-
|
1
|
+
import { objectFromPathEntries } from "../internal/flatten";
|
2
|
+
const preprocessFormData = (data) => {
|
3
|
+
// A slightly janky way of determining if the data is a FormData object
|
4
|
+
// since node doesn't really have FormData
|
5
|
+
if ("entries" in data && typeof data.entries === "function")
|
6
|
+
return objectFromPathEntries([...data.entries()]);
|
7
|
+
return objectFromPathEntries(Object.entries(data));
|
8
|
+
};
|
9
|
+
/**
|
10
|
+
* Used to create a validator for a form.
|
11
|
+
* It provides built-in handling for unflattening nested objects and
|
12
|
+
* extracting the values from FormData.
|
13
|
+
*/
|
3
14
|
export function createValidator(validator) {
|
4
15
|
return {
|
5
|
-
validate: (value) => validator.validate(
|
6
|
-
validateField: (data, field) => validator.validateField(
|
16
|
+
validate: (value) => validator.validate(preprocessFormData(value)),
|
17
|
+
validateField: (data, field) => validator.validateField(preprocessFormData(data), field),
|
7
18
|
};
|
8
19
|
}
|
@@ -2,6 +2,9 @@ export declare type FieldErrors = Record<string, string>;
|
|
2
2
|
export declare type GenericObject = {
|
3
3
|
[key: string]: any;
|
4
4
|
};
|
5
|
+
/**
|
6
|
+
* The result when validating a form.
|
7
|
+
*/
|
5
8
|
export declare type ValidationResult<DataType> = {
|
6
9
|
data: DataType;
|
7
10
|
error: undefined;
|
@@ -9,9 +12,15 @@ export declare type ValidationResult<DataType> = {
|
|
9
12
|
error: FieldErrors;
|
10
13
|
data: undefined;
|
11
14
|
};
|
15
|
+
/**
|
16
|
+
* The result when validating an individual field in a form.
|
17
|
+
*/
|
12
18
|
export declare type ValidateFieldResult = {
|
13
19
|
error?: string;
|
14
20
|
};
|
21
|
+
/**
|
22
|
+
* A `Validator` can be passed to the `validator` prop of a `ValidatedForm`.
|
23
|
+
*/
|
15
24
|
export declare type Validator<DataType> = {
|
16
25
|
validate: (unvalidatedData: GenericObject) => ValidationResult<DataType>;
|
17
26
|
validateField: (unvalidatedData: GenericObject, field: string) => ValidateFieldResult;
|
File without changes
|
File without changes
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import * as yup from "yup";
|
2
2
|
import { z } from "zod";
|
3
3
|
import { withYup } from "..";
|
4
|
+
import { TestFormData } from "../test-data/testFormData";
|
4
5
|
import { withZod } from "./withZod";
|
5
6
|
const validationTestCases = [
|
6
7
|
{
|
@@ -80,6 +81,70 @@ describe("Validation", () => {
|
|
80
81
|
},
|
81
82
|
});
|
82
83
|
});
|
84
|
+
it("should unflatten data when validating", () => {
|
85
|
+
const data = {
|
86
|
+
firstName: "John",
|
87
|
+
lastName: "Doe",
|
88
|
+
age: 30,
|
89
|
+
"address.streetAddress": "123 Main St",
|
90
|
+
"address.city": "Anytown",
|
91
|
+
"address.country": "USA",
|
92
|
+
"pets[0].animal": "dog",
|
93
|
+
"pets[0].name": "Fido",
|
94
|
+
};
|
95
|
+
expect(validator.validate(data)).toEqual({
|
96
|
+
data: {
|
97
|
+
firstName: "John",
|
98
|
+
lastName: "Doe",
|
99
|
+
age: 30,
|
100
|
+
address: {
|
101
|
+
streetAddress: "123 Main St",
|
102
|
+
city: "Anytown",
|
103
|
+
country: "USA",
|
104
|
+
},
|
105
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
106
|
+
},
|
107
|
+
error: undefined,
|
108
|
+
});
|
109
|
+
});
|
110
|
+
it("should accept FormData directly and return errors", () => {
|
111
|
+
const formData = new TestFormData();
|
112
|
+
formData.set("firstName", "John");
|
113
|
+
formData.set("lastName", "Doe");
|
114
|
+
formData.set("address.streetAddress", "123 Main St");
|
115
|
+
formData.set("address.country", "USA");
|
116
|
+
formData.set("pets[0].animal", "dog");
|
117
|
+
expect(validator.validate(formData)).toEqual({
|
118
|
+
data: undefined,
|
119
|
+
error: {
|
120
|
+
"address.city": anyString,
|
121
|
+
"pets[0].name": anyString,
|
122
|
+
},
|
123
|
+
});
|
124
|
+
});
|
125
|
+
it("should accept FormData directly and return valid data", () => {
|
126
|
+
const formData = new TestFormData();
|
127
|
+
formData.set("firstName", "John");
|
128
|
+
formData.set("lastName", "Doe");
|
129
|
+
formData.set("address.streetAddress", "123 Main St");
|
130
|
+
formData.set("address.country", "USA");
|
131
|
+
formData.set("address.city", "Anytown");
|
132
|
+
formData.set("pets[0].animal", "dog");
|
133
|
+
formData.set("pets[0].name", "Fido");
|
134
|
+
expect(validator.validate(formData)).toEqual({
|
135
|
+
data: {
|
136
|
+
firstName: "John",
|
137
|
+
lastName: "Doe",
|
138
|
+
address: {
|
139
|
+
streetAddress: "123 Main St",
|
140
|
+
country: "USA",
|
141
|
+
city: "Anytown",
|
142
|
+
},
|
143
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
144
|
+
},
|
145
|
+
error: undefined,
|
146
|
+
});
|
147
|
+
});
|
83
148
|
});
|
84
149
|
describe("validateField", () => {
|
85
150
|
it("should not return an error if field is valid", () => {
|
@@ -1,3 +1,6 @@
|
|
1
1
|
import type { AnyObjectSchema, InferType } from "yup";
|
2
2
|
import { Validator } from "./types";
|
3
|
+
/**
|
4
|
+
* Create a `Validator` using a `yup` schema.
|
5
|
+
*/
|
3
6
|
export declare const withYup: <Schema extends AnyObjectSchema>(validationSchema: Schema) => Validator<InferType<Schema>>;
|
@@ -17,6 +17,9 @@ function pathToString(array) {
|
|
17
17
|
return string + (isNaN(Number(item)) ? prefix + item : "[" + item + "]");
|
18
18
|
}, "");
|
19
19
|
}
|
20
|
+
/**
|
21
|
+
* Create a validator using a `zod` schema.
|
22
|
+
*/
|
20
23
|
export function withZod(zodSchema) {
|
21
24
|
return createValidator({
|
22
25
|
validate: (value) => {
|
package/build/ValidatedForm.d.ts
CHANGED
@@ -2,10 +2,32 @@ import { Form as RemixForm, useFetcher } from "@remix-run/react";
|
|
2
2
|
import React, { ComponentProps } from "react";
|
3
3
|
import { Validator } from "./validation/types";
|
4
4
|
export declare type FormProps<DataType> = {
|
5
|
+
/**
|
6
|
+
* A `Validator` object that describes how to validate the form.
|
7
|
+
*/
|
5
8
|
validator: Validator<DataType>;
|
9
|
+
/**
|
10
|
+
* A submit callback that gets called when the form is submitted
|
11
|
+
* after all validations have been run.
|
12
|
+
*/
|
6
13
|
onSubmit?: (data: DataType, event: React.FormEvent<HTMLFormElement>) => void;
|
14
|
+
/**
|
15
|
+
* Allows you to provide a `fetcher` from remix's `useFetcher` hook.
|
16
|
+
* The form will use the fetcher for loading states, action data, etc
|
17
|
+
* instead of the default form action.
|
18
|
+
*/
|
7
19
|
fetcher?: ReturnType<typeof useFetcher>;
|
20
|
+
/**
|
21
|
+
* Accepts an object of default values for the form
|
22
|
+
* that will automatically be propagated to the form fields via `useField`.
|
23
|
+
*/
|
8
24
|
defaultValues?: Partial<DataType>;
|
25
|
+
/**
|
26
|
+
* A ref to the form element.
|
27
|
+
*/
|
9
28
|
formRef?: React.RefObject<HTMLFormElement>;
|
10
29
|
} & Omit<ComponentProps<typeof RemixForm>, "onSubmit">;
|
30
|
+
/**
|
31
|
+
* The primary form component of `remix-validated-form`.
|
32
|
+
*/
|
11
33
|
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }: FormProps<DataType>): JSX.Element;
|
package/build/ValidatedForm.js
CHANGED
@@ -29,7 +29,10 @@ const useIsSubmitting = (action, fetcher) => {
|
|
29
29
|
: pendingFormSubmit &&
|
30
30
|
pendingFormSubmit.action === (action !== null && action !== void 0 ? action : actionForCurrentPage);
|
31
31
|
};
|
32
|
-
const getDataFromForm = (el) =>
|
32
|
+
const getDataFromForm = (el) => new FormData(el);
|
33
|
+
/**
|
34
|
+
* The primary form component of `remix-validated-form`.
|
35
|
+
*/
|
33
36
|
function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }) {
|
34
37
|
var _a;
|
35
38
|
const [fieldErrors, setFieldErrors] = useFieldErrors(fetcher);
|
package/build/hooks.d.ts
CHANGED
@@ -1,8 +1,33 @@
|
|
1
|
-
export declare
|
2
|
-
|
1
|
+
export declare type FieldProps = {
|
2
|
+
/**
|
3
|
+
* The validation error message if there is one.
|
4
|
+
*/
|
5
|
+
error?: string;
|
6
|
+
/**
|
7
|
+
* Clears the error message.
|
8
|
+
*/
|
3
9
|
clearError: () => void;
|
10
|
+
/**
|
11
|
+
* Validates the field.
|
12
|
+
*/
|
4
13
|
validate: () => void;
|
5
|
-
|
14
|
+
/**
|
15
|
+
* The default value of the field, if there is one.
|
16
|
+
*/
|
17
|
+
defaultValue?: any;
|
6
18
|
};
|
19
|
+
/**
|
20
|
+
* Provides the data and helpers necessary to set up a field.
|
21
|
+
*/
|
22
|
+
export declare const useField: (name: string) => FieldProps;
|
23
|
+
/**
|
24
|
+
* Provides access to the entire form context.
|
25
|
+
* This is not usually necessary, but can be useful for advanced use cases.
|
26
|
+
*/
|
7
27
|
export declare const useFormContext: () => import("./internal/formContext").FormContextValue;
|
28
|
+
/**
|
29
|
+
* Returns whether or not the parent form is currently being submitted.
|
30
|
+
* This is different from remix's `useTransition().submission` in that it
|
31
|
+
* is aware of what form it's in and when _that_ form is being submitted.
|
32
|
+
*/
|
8
33
|
export declare const useIsSubmitting: () => boolean;
|
package/build/hooks.js
CHANGED
@@ -8,6 +8,9 @@ const get_1 = __importDefault(require("lodash/get"));
|
|
8
8
|
const toPath_1 = __importDefault(require("lodash/toPath"));
|
9
9
|
const react_1 = require("react");
|
10
10
|
const formContext_1 = require("./internal/formContext");
|
11
|
+
/**
|
12
|
+
* Provides the data and helpers necessary to set up a field.
|
13
|
+
*/
|
11
14
|
const useField = (name) => {
|
12
15
|
const { fieldErrors, clearError, validateField, defaultValues } = (0, react_1.useContext)(formContext_1.FormContext);
|
13
16
|
const field = (0, react_1.useMemo)(() => ({
|
@@ -23,8 +26,16 @@ const useField = (name) => {
|
|
23
26
|
return field;
|
24
27
|
};
|
25
28
|
exports.useField = useField;
|
26
|
-
|
29
|
+
/**
|
30
|
+
* Provides access to the entire form context.
|
31
|
+
* This is not usually necessary, but can be useful for advanced use cases.
|
32
|
+
*/
|
27
33
|
const useFormContext = () => (0, react_1.useContext)(formContext_1.FormContext);
|
28
34
|
exports.useFormContext = useFormContext;
|
35
|
+
/**
|
36
|
+
* Returns whether or not the parent form is currently being submitted.
|
37
|
+
* This is different from remix's `useTransition().submission` in that it
|
38
|
+
* is aware of what form it's in and when _that_ form is being submitted.
|
39
|
+
*/
|
29
40
|
const useIsSubmitting = () => (0, exports.useFormContext)().isSubmitting;
|
30
41
|
exports.useIsSubmitting = useIsSubmitting;
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
@@ -16,3 +16,4 @@ __exportStar(require("./ValidatedForm"), exports);
|
|
16
16
|
__exportStar(require("./validation/types"), exports);
|
17
17
|
__exportStar(require("./validation/withYup"), exports);
|
18
18
|
__exportStar(require("./validation/withZod"), exports);
|
19
|
+
__exportStar(require("./validation/createValidator"), exports);
|
@@ -1,5 +1,4 @@
|
|
1
|
-
import { GenericObject } from "
|
2
|
-
|
3
|
-
export declare function unflatten(params: GenericObject): {};
|
1
|
+
import { GenericObject } from "..";
|
2
|
+
export declare const objectFromPathEntries: (entries: [string, any][]) => {};
|
4
3
|
/** Flatten an object so there are no nested objects or arrays */
|
5
4
|
export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
|
@@ -3,23 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.flatten = exports.
|
7
|
-
// `flatten`
|
6
|
+
exports.flatten = exports.objectFromPathEntries = void 0;
|
7
|
+
// `flatten` is taken from https://github.com/richie5um/FlattenJS. Decided to implement them here instead of using that package because this is a core functionality of the library and this will add more flexibility in case we need to change the implementation.
|
8
8
|
const assign_1 = __importDefault(require("lodash/assign"));
|
9
9
|
const isArray_1 = __importDefault(require("lodash/isArray"));
|
10
10
|
const isObject_1 = __importDefault(require("lodash/isObject"));
|
11
11
|
const keys_1 = __importDefault(require("lodash/keys"));
|
12
12
|
const mapKeys_1 = __importDefault(require("lodash/mapKeys"));
|
13
|
-
const reduce_1 = __importDefault(require("lodash/reduce"));
|
14
13
|
const set_1 = __importDefault(require("lodash/set"));
|
15
14
|
const transform_1 = __importDefault(require("lodash/transform"));
|
16
|
-
|
17
|
-
|
18
|
-
return (0, reduce_1.default)(params, function (result, value, key) {
|
19
|
-
return (0, set_1.default)(result, key, value);
|
20
|
-
}, {});
|
21
|
-
}
|
22
|
-
exports.unflatten = unflatten;
|
15
|
+
const objectFromPathEntries = (entries) => entries.reduce((acc, [key, value]) => (0, set_1.default)(acc, key, value), {});
|
16
|
+
exports.objectFromPathEntries = objectFromPathEntries;
|
23
17
|
/** Flatten an object so there are no nested objects or arrays */
|
24
18
|
function flatten(obj, preserveEmpty = false) {
|
25
19
|
return (0, transform_1.default)(obj, function (result, value, key) {
|