remix-validated-form 1.1.1-beta.0 → 1.1.1-beta.1

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.
Files changed (52) hide show
  1. package/.eslintcache +1 -1
  2. package/README.md +26 -21
  3. package/browser/ValidatedForm.d.ts +22 -0
  4. package/browser/ValidatedForm.js +4 -1
  5. package/browser/flatten.d.ts +1 -2
  6. package/browser/flatten.js +2 -8
  7. package/browser/hooks.d.ts +28 -3
  8. package/browser/hooks.js +12 -1
  9. package/browser/index.d.ts +1 -0
  10. package/browser/index.js +1 -0
  11. package/browser/internal/flatten.d.ts +4 -0
  12. package/browser/internal/flatten.js +35 -0
  13. package/browser/internal/formContext.d.ts +18 -0
  14. package/browser/server.d.ts +5 -0
  15. package/browser/server.js +5 -0
  16. package/browser/test-data/testFormData.d.ts +15 -0
  17. package/browser/test-data/testFormData.js +46 -0
  18. package/browser/validation/createValidator.d.ts +5 -1
  19. package/browser/validation/createValidator.js +15 -4
  20. package/browser/validation/types.d.ts +9 -0
  21. package/browser/validation/validation.test.js +65 -0
  22. package/browser/validation/withYup.d.ts +3 -0
  23. package/browser/validation/withYup.js +3 -0
  24. package/browser/validation/withZod.d.ts +3 -0
  25. package/browser/validation/withZod.js +3 -0
  26. package/build/ValidatedForm.d.ts +22 -0
  27. package/build/ValidatedForm.js +4 -1
  28. package/build/flatten.d.ts +1 -2
  29. package/build/flatten.js +4 -10
  30. package/build/hooks.d.ts +28 -3
  31. package/build/hooks.js +12 -1
  32. package/build/index.d.ts +1 -0
  33. package/build/index.js +1 -0
  34. package/build/internal/flatten.d.ts +4 -0
  35. package/build/internal/flatten.js +43 -0
  36. package/build/internal/formContext.d.ts +18 -0
  37. package/build/server.d.ts +5 -0
  38. package/build/server.js +5 -0
  39. package/build/test-data/testFormData.d.ts +15 -0
  40. package/build/test-data/testFormData.js +50 -0
  41. package/build/validation/createValidator.d.ts +5 -1
  42. package/build/validation/createValidator.js +15 -4
  43. package/build/validation/types.d.ts +9 -0
  44. package/build/validation/validation.test.js +65 -0
  45. package/build/validation/withYup.d.ts +3 -0
  46. package/build/validation/withYup.js +3 -0
  47. package/build/validation/withZod.d.ts +3 -0
  48. package/build/validation/withZod.js +3 -0
  49. package/package.json +1 -1
  50. package/sample-app/app/routes/subjects/$id.edit.tsx +1 -3
  51. package/sample-app/app/routes/subjects/new.tsx +1 -3
  52. package/sample-app/package-lock.json +6292 -19
@@ -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;
@@ -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) => Object.fromEntries(new FormData(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);
@@ -1,5 +1,4 @@
1
1
  import { GenericObject } from ".";
2
- /** Unflatten a previously flatten object */
3
- export declare function unflatten(params: GenericObject): {};
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;
package/build/flatten.js CHANGED
@@ -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.unflatten = void 0;
7
- // `flatten` and `unflatten` are 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.
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
- /** Unflatten a previously flatten object */
17
- function unflatten(params) {
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) {
package/build/hooks.d.ts CHANGED
@@ -1,8 +1,33 @@
1
- export declare const useField: (name: string) => {
2
- error: string;
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
- defaultValue: any;
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
- // test commit
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
@@ -4,3 +4,4 @@ export * from "./ValidatedForm";
4
4
  export * from "./validation/types";
5
5
  export * from "./validation/withYup";
6
6
  export * from "./validation/withZod";
7
+ export * from "./validation/createValidator";
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);
@@ -0,0 +1,4 @@
1
+ import { GenericObject } from "..";
2
+ export declare const objectFromPathEntries: (entries: [string, any][]) => {};
3
+ /** Flatten an object so there are no nested objects or arrays */
4
+ export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
@@ -0,0 +1,43 @@
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.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
+ const assign_1 = __importDefault(require("lodash/assign"));
9
+ const isArray_1 = __importDefault(require("lodash/isArray"));
10
+ const isObject_1 = __importDefault(require("lodash/isObject"));
11
+ const keys_1 = __importDefault(require("lodash/keys"));
12
+ const mapKeys_1 = __importDefault(require("lodash/mapKeys"));
13
+ const set_1 = __importDefault(require("lodash/set"));
14
+ const transform_1 = __importDefault(require("lodash/transform"));
15
+ const objectFromPathEntries = (entries) => entries.reduce((acc, [key, value]) => (0, set_1.default)(acc, key, value), {});
16
+ exports.objectFromPathEntries = objectFromPathEntries;
17
+ /** Flatten an object so there are no nested objects or arrays */
18
+ function flatten(obj, preserveEmpty = false) {
19
+ return (0, transform_1.default)(obj, function (result, value, key) {
20
+ if ((0, isObject_1.default)(value)) {
21
+ let flatMap = (0, mapKeys_1.default)(flatten(value, preserveEmpty), function (_mvalue, mkey) {
22
+ if ((0, isArray_1.default)(value)) {
23
+ let index = mkey.indexOf(".");
24
+ if (-1 !== index) {
25
+ return `${key}[${mkey.slice(0, index)}]${mkey.slice(index)}`;
26
+ }
27
+ return `${key}[${mkey}]`;
28
+ }
29
+ return `${key}.${mkey}`;
30
+ });
31
+ (0, assign_1.default)(result, flatMap);
32
+ // Preverve Empty arrays and objects
33
+ if (preserveEmpty && (0, keys_1.default)(flatMap).length === 0) {
34
+ result[key] = value;
35
+ }
36
+ }
37
+ else {
38
+ result[key] = value;
39
+ }
40
+ return result;
41
+ }, {});
42
+ }
43
+ exports.flatten = flatten;
@@ -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
  };
package/build/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/build/server.js CHANGED
@@ -2,5 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validationError = void 0;
4
4
  const server_runtime_1 = require("@remix-run/server-runtime");
5
+ /**
6
+ * Takes the errors from a `Validator` and returns a `Response`.
7
+ * The `ValidatedForm` on the frontend will automatically display the errors
8
+ * if this is returned from the action.
9
+ */
5
10
  const validationError = (errors) => (0, server_runtime_1.json)({ fieldErrors: errors }, { status: 422 });
6
11
  exports.validationError = validationError;
@@ -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,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TestFormData = void 0;
4
+ // Copied from remix to use in tests
5
+ // https://github.com/remix-run/remix/blob/a69a631cb5add72d5fb24211ab2a0be367b6f2fd/packages/remix-node/form-data.ts
6
+ class TestFormData {
7
+ constructor(body) {
8
+ this._params = new URLSearchParams(body);
9
+ }
10
+ append(name, value, fileName) {
11
+ if (typeof value !== "string") {
12
+ throw new Error("formData.append can only accept a string");
13
+ }
14
+ this._params.append(name, value);
15
+ }
16
+ delete(name) {
17
+ this._params.delete(name);
18
+ }
19
+ get(name) {
20
+ return this._params.get(name);
21
+ }
22
+ getAll(name) {
23
+ return this._params.getAll(name);
24
+ }
25
+ has(name) {
26
+ return this._params.has(name);
27
+ }
28
+ set(name, value, fileName) {
29
+ if (typeof value !== "string") {
30
+ throw new Error("formData.set can only accept a string");
31
+ }
32
+ this._params.set(name, value);
33
+ }
34
+ forEach(callbackfn, thisArg) {
35
+ this._params.forEach(callbackfn, thisArg);
36
+ }
37
+ entries() {
38
+ return this._params.entries();
39
+ }
40
+ keys() {
41
+ return this._params.keys();
42
+ }
43
+ values() {
44
+ return this._params.values();
45
+ }
46
+ *[Symbol.iterator]() {
47
+ yield* this._params;
48
+ }
49
+ }
50
+ exports.TestFormData = TestFormData;
@@ -1,3 +1,7 @@
1
1
  import { Validator } from "..";
2
- /** Handles data manipulation such us flattening the data to send to the validator */
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,12 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createValidator = void 0;
4
- const flatten_1 = require("../flatten");
5
- /** Handles data manipulation such us flattening the data to send to the validator */
4
+ const flatten_1 = require("../internal/flatten");
5
+ const preprocessFormData = (data) => {
6
+ // A slightly janky way of determining if the data is a FormData object
7
+ // since node doesn't really have FormData
8
+ if ("entries" in data && typeof data.entries === "function")
9
+ return (0, flatten_1.objectFromPathEntries)([...data.entries()]);
10
+ return (0, flatten_1.objectFromPathEntries)(Object.entries(data));
11
+ };
12
+ /**
13
+ * Used to create a validator for a form.
14
+ * It provides built-in handling for unflattening nested objects and
15
+ * extracting the values from FormData.
16
+ */
6
17
  function createValidator(validator) {
7
18
  return {
8
- validate: (value) => validator.validate((0, flatten_1.unflatten)(value)),
9
- validateField: (data, field) => validator.validateField((0, flatten_1.unflatten)(data), field),
19
+ validate: (value) => validator.validate(preprocessFormData(value)),
20
+ validateField: (data, field) => validator.validateField(preprocessFormData(data), field),
10
21
  };
11
22
  }
12
23
  exports.createValidator = createValidator;
@@ -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;
@@ -22,6 +22,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
22
22
  const yup = __importStar(require("yup"));
23
23
  const zod_1 = require("zod");
24
24
  const __1 = require("..");
25
+ const testFormData_1 = require("../test-data/testFormData");
25
26
  const withZod_1 = require("./withZod");
26
27
  const validationTestCases = [
27
28
  {
@@ -101,6 +102,70 @@ describe("Validation", () => {
101
102
  },
102
103
  });
103
104
  });
105
+ it("should unflatten data when validating", () => {
106
+ const data = {
107
+ firstName: "John",
108
+ lastName: "Doe",
109
+ age: 30,
110
+ "address.streetAddress": "123 Main St",
111
+ "address.city": "Anytown",
112
+ "address.country": "USA",
113
+ "pets[0].animal": "dog",
114
+ "pets[0].name": "Fido",
115
+ };
116
+ expect(validator.validate(data)).toEqual({
117
+ data: {
118
+ firstName: "John",
119
+ lastName: "Doe",
120
+ age: 30,
121
+ address: {
122
+ streetAddress: "123 Main St",
123
+ city: "Anytown",
124
+ country: "USA",
125
+ },
126
+ pets: [{ animal: "dog", name: "Fido" }],
127
+ },
128
+ error: undefined,
129
+ });
130
+ });
131
+ it("should accept FormData directly and return errors", () => {
132
+ const formData = new testFormData_1.TestFormData();
133
+ formData.set("firstName", "John");
134
+ formData.set("lastName", "Doe");
135
+ formData.set("address.streetAddress", "123 Main St");
136
+ formData.set("address.country", "USA");
137
+ formData.set("pets[0].animal", "dog");
138
+ expect(validator.validate(formData)).toEqual({
139
+ data: undefined,
140
+ error: {
141
+ "address.city": anyString,
142
+ "pets[0].name": anyString,
143
+ },
144
+ });
145
+ });
146
+ it("should accept FormData directly and return valid data", () => {
147
+ const formData = new testFormData_1.TestFormData();
148
+ formData.set("firstName", "John");
149
+ formData.set("lastName", "Doe");
150
+ formData.set("address.streetAddress", "123 Main St");
151
+ formData.set("address.country", "USA");
152
+ formData.set("address.city", "Anytown");
153
+ formData.set("pets[0].animal", "dog");
154
+ formData.set("pets[0].name", "Fido");
155
+ expect(validator.validate(formData)).toEqual({
156
+ data: {
157
+ firstName: "John",
158
+ lastName: "Doe",
159
+ address: {
160
+ streetAddress: "123 Main St",
161
+ country: "USA",
162
+ city: "Anytown",
163
+ },
164
+ pets: [{ animal: "dog", name: "Fido" }],
165
+ },
166
+ error: undefined,
167
+ });
168
+ });
104
169
  });
105
170
  describe("validateField", () => {
106
171
  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>>;
@@ -11,6 +11,9 @@ const validationErrorToFieldErrors = (error) => {
11
11
  });
12
12
  return fieldErrors;
13
13
  };
14
+ /**
15
+ * Create a `Validator` using a `yup` schema.
16
+ */
14
17
  const withYup = (validationSchema) => {
15
18
  return (0, createValidator_1.createValidator)({
16
19
  validate: (data) => {
@@ -1,3 +1,6 @@
1
1
  import type { z } from "zod";
2
2
  import { Validator } from "..";
3
+ /**
4
+ * Create a validator using a `zod` schema.
5
+ */
3
6
  export declare function withZod<T>(zodSchema: z.Schema<T>): Validator<T>;
@@ -23,6 +23,9 @@ function pathToString(array) {
23
23
  return string + (isNaN(Number(item)) ? prefix + item : "[" + item + "]");
24
24
  }, "");
25
25
  }
26
+ /**
27
+ * Create a validator using a `zod` schema.
28
+ */
26
29
  function withZod(zodSchema) {
27
30
  return (0, createValidator_1.createValidator)({
28
31
  validate: (value) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remix-validated-form",
3
- "version": "1.1.1-beta.0",
3
+ "version": "1.1.1-beta.1",
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",
@@ -38,9 +38,7 @@ export const loader: LoaderFunction = async ({ params }) => {
38
38
  export const action: ActionFunction = async ({ request, params }) => {
39
39
  const { id } = z.object({ id: z.string() }).parse(params);
40
40
 
41
- const fieldValues = subjectFormValidator.validate(
42
- Object.fromEntries(await request.formData())
43
- );
41
+ const fieldValues = subjectFormValidator.validate(await request.formData());
44
42
  if (fieldValues.error) return validationError(fieldValues.error);
45
43
 
46
44
  const { teacher, subjectDays, ...updatedSubject } = fieldValues.data;
@@ -5,9 +5,7 @@ import { db } from "~/services/db.server";
5
5
  import { validationError } from "../../../remix-validated-form";
6
6
 
7
7
  export const action: ActionFunction = async ({ request }) => {
8
- const fieldValues = subjectFormValidator.validate(
9
- Object.fromEntries(await request.formData())
10
- );
8
+ const fieldValues = subjectFormValidator.validate(await request.formData());
11
9
  if (fieldValues.error) return validationError(fieldValues.error);
12
10
 
13
11
  const { teacher, subjectDays, ...newSubject } = fieldValues.data;