remix-validated-form 1.1.0 → 2.0.0-beta.2
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 +78 -0
- package/.turbo/turbo-test.log +36 -0
- package/README.md +72 -19
- package/browser/ValidatedForm.d.ts +35 -1
- package/browser/ValidatedForm.js +76 -20
- package/browser/hooks.d.ts +28 -3
- package/browser/hooks.js +17 -2
- package/browser/index.d.ts +3 -0
- package/browser/index.js +2 -0
- package/browser/internal/flatten.d.ts +4 -0
- package/browser/internal/flatten.js +35 -0
- package/browser/internal/formContext.d.ts +18 -0
- package/browser/internal/formContext.js +0 -0
- package/browser/internal/submissionCallbacks.d.ts +1 -0
- package/browser/internal/submissionCallbacks.js +13 -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 +7 -0
- package/browser/validation/createValidator.js +30 -0
- package/browser/validation/types.d.ts +17 -2
- package/browser/validation/types.js +0 -0
- package/browser/validation/validation.test.d.ts +0 -0
- package/browser/validation/validation.test.js +162 -8
- package/browser/validation/withYup.d.ts +3 -0
- package/browser/validation/withYup.js +31 -25
- package/browser/validation/withZod.d.ts +3 -0
- package/browser/validation/withZod.js +19 -4
- package/build/ValidatedForm.d.ts +35 -1
- package/build/ValidatedForm.js +75 -19
- package/build/hooks.d.ts +28 -3
- package/build/hooks.js +20 -2
- package/build/index.d.ts +3 -0
- package/build/index.js +2 -0
- package/build/internal/flatten.d.ts +4 -0
- package/build/internal/flatten.js +43 -0
- package/build/internal/formContext.d.ts +18 -0
- package/build/internal/formContext.js +0 -0
- package/build/internal/submissionCallbacks.d.ts +1 -0
- package/build/internal/submissionCallbacks.js +17 -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 +7 -0
- package/build/validation/createValidator.js +34 -0
- package/build/validation/types.d.ts +17 -2
- package/build/validation/types.js +0 -0
- package/build/validation/validation.test.d.ts +0 -0
- package/build/validation/validation.test.js +162 -8
- package/build/validation/withYup.d.ts +3 -0
- package/build/validation/withYup.js +31 -25
- package/build/validation/withZod.d.ts +3 -0
- package/build/validation/withZod.js +22 -4
- package/jest.config.js +5 -0
- package/package.json +18 -38
- package/src/ValidatedForm.tsx +227 -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/submissionCallbacks.ts +15 -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 +34 -0
- package/src/validation/types.ts +28 -0
- package/src/validation/validation.test.ts +322 -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 -8
- package/LICENSE +0 -21
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
@@ -1,8 +1,16 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
6
|
exports.useIsSubmitting = exports.useFormContext = exports.useField = void 0;
|
7
|
+
const get_1 = __importDefault(require("lodash/get"));
|
8
|
+
const toPath_1 = __importDefault(require("lodash/toPath"));
|
4
9
|
const react_1 = require("react");
|
5
10
|
const formContext_1 = require("./internal/formContext");
|
11
|
+
/**
|
12
|
+
* Provides the data and helpers necessary to set up a field.
|
13
|
+
*/
|
6
14
|
const useField = (name) => {
|
7
15
|
const { fieldErrors, clearError, validateField, defaultValues } = (0, react_1.useContext)(formContext_1.FormContext);
|
8
16
|
const field = (0, react_1.useMemo)(() => ({
|
@@ -11,13 +19,23 @@ const useField = (name) => {
|
|
11
19
|
clearError(name);
|
12
20
|
},
|
13
21
|
validate: () => validateField(name),
|
14
|
-
defaultValue: defaultValues
|
22
|
+
defaultValue: defaultValues
|
23
|
+
? (0, get_1.default)(defaultValues, (0, toPath_1.default)(name), undefined)
|
24
|
+
: undefined,
|
15
25
|
}), [clearError, defaultValues, fieldErrors, name, validateField]);
|
16
26
|
return field;
|
17
27
|
};
|
18
28
|
exports.useField = useField;
|
19
|
-
|
29
|
+
/**
|
30
|
+
* Provides access to the entire form context.
|
31
|
+
* This is not usually necessary, but can be useful for advanced use cases.
|
32
|
+
*/
|
20
33
|
const useFormContext = () => (0, react_1.useContext)(formContext_1.FormContext);
|
21
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
|
+
*/
|
22
40
|
const useIsSubmitting = () => (0, exports.useFormContext)().isSubmitting;
|
23
41
|
exports.useIsSubmitting = useIsSubmitting;
|
package/build/index.d.ts
CHANGED
@@ -3,3 +3,6 @@ export * from "./server";
|
|
3
3
|
export * from "./ValidatedForm";
|
4
4
|
export * from "./validation/types";
|
5
5
|
export * from "./validation/withYup";
|
6
|
+
export * from "./validation/withZod";
|
7
|
+
export * from "./validation/createValidator";
|
8
|
+
export type { FormContextValue } from "./internal/formContext";
|
package/build/index.js
CHANGED
@@ -15,3 +15,5 @@ __exportStar(require("./server"), exports);
|
|
15
15
|
__exportStar(require("./ValidatedForm"), exports);
|
16
16
|
__exportStar(require("./validation/types"), exports);
|
17
17
|
__exportStar(require("./validation/withYup"), exports);
|
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
|
};
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function useSubmitComplete(isSubmitting: boolean, callback: () => void): void;
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useSubmitComplete = void 0;
|
4
|
+
const react_1 = require("react");
|
5
|
+
function useSubmitComplete(isSubmitting, callback) {
|
6
|
+
const isPending = (0, react_1.useRef)(false);
|
7
|
+
(0, react_1.useEffect)(() => {
|
8
|
+
if (isSubmitting) {
|
9
|
+
isPending.current = true;
|
10
|
+
}
|
11
|
+
if (!isSubmitting && isPending.current) {
|
12
|
+
isPending.current = false;
|
13
|
+
callback();
|
14
|
+
}
|
15
|
+
});
|
16
|
+
}
|
17
|
+
exports.useSubmitComplete = useSubmitComplete;
|
package/build/internal/util.d.ts
CHANGED
File without changes
|
package/build/internal/util.js
CHANGED
File without changes
|
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;
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { Validator } from "..";
|
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
|
+
*/
|
7
|
+
export declare function createValidator<T>(validator: Validator<T>): Validator<T>;
|
@@ -0,0 +1,34 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.createValidator = void 0;
|
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
|
+
*/
|
17
|
+
function createValidator(validator) {
|
18
|
+
return {
|
19
|
+
validate: (value) => {
|
20
|
+
const data = preprocessFormData(value);
|
21
|
+
const result = validator.validate(data);
|
22
|
+
if (result.error) {
|
23
|
+
// Ideally, we should probably be returning a nested object like
|
24
|
+
// { fieldErrors: {}, submittedData: {} }
|
25
|
+
// We should do this in the next major version of the library
|
26
|
+
// but for now, we can sneak it in with the fieldErrors.
|
27
|
+
result.error._submittedData = data;
|
28
|
+
}
|
29
|
+
return result;
|
30
|
+
},
|
31
|
+
validateField: (data, field) => validator.validateField(preprocessFormData(data), field),
|
32
|
+
};
|
33
|
+
}
|
34
|
+
exports.createValidator = createValidator;
|
@@ -1,4 +1,13 @@
|
|
1
1
|
export declare type FieldErrors = Record<string, string>;
|
2
|
+
export declare type FieldErrorsWithData = FieldErrors & {
|
3
|
+
_submittedData: any;
|
4
|
+
};
|
5
|
+
export declare type GenericObject = {
|
6
|
+
[key: string]: any;
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* The result when validating a form.
|
10
|
+
*/
|
2
11
|
export declare type ValidationResult<DataType> = {
|
3
12
|
data: DataType;
|
4
13
|
error: undefined;
|
@@ -6,10 +15,16 @@ export declare type ValidationResult<DataType> = {
|
|
6
15
|
error: FieldErrors;
|
7
16
|
data: undefined;
|
8
17
|
};
|
18
|
+
/**
|
19
|
+
* The result when validating an individual field in a form.
|
20
|
+
*/
|
9
21
|
export declare type ValidateFieldResult = {
|
10
22
|
error?: string;
|
11
23
|
};
|
24
|
+
/**
|
25
|
+
* A `Validator` can be passed to the `validator` prop of a `ValidatedForm`.
|
26
|
+
*/
|
12
27
|
export declare type Validator<DataType> = {
|
13
|
-
validate: (unvalidatedData:
|
14
|
-
validateField: (unvalidatedData:
|
28
|
+
validate: (unvalidatedData: GenericObject) => ValidationResult<DataType>;
|
29
|
+
validateField: (unvalidatedData: GenericObject, field: string) => ValidateFieldResult;
|
15
30
|
};
|
File without changes
|
File without changes
|
@@ -22,6 +22,8 @@ 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 flatten_1 = require("../internal/flatten");
|
26
|
+
const testFormData_1 = require("../test-data/testFormData");
|
25
27
|
const withZod_1 = require("./withZod");
|
26
28
|
const validationTestCases = [
|
27
29
|
{
|
@@ -30,6 +32,17 @@ const validationTestCases = [
|
|
30
32
|
firstName: yup.string().required(),
|
31
33
|
lastName: yup.string().required(),
|
32
34
|
age: yup.number(),
|
35
|
+
address: yup
|
36
|
+
.object({
|
37
|
+
streetAddress: yup.string().required(),
|
38
|
+
city: yup.string().required(),
|
39
|
+
country: yup.string().required(),
|
40
|
+
})
|
41
|
+
.required(),
|
42
|
+
pets: yup.array().of(yup.object({
|
43
|
+
animal: yup.string().required(),
|
44
|
+
name: yup.string().required(),
|
45
|
+
})),
|
33
46
|
})),
|
34
47
|
},
|
35
48
|
{
|
@@ -38,6 +51,18 @@ const validationTestCases = [
|
|
38
51
|
firstName: zod_1.z.string().nonempty(),
|
39
52
|
lastName: zod_1.z.string().nonempty(),
|
40
53
|
age: zod_1.z.optional(zod_1.z.number()),
|
54
|
+
address: zod_1.z.preprocess((value) => (value == null ? {} : value), zod_1.z.object({
|
55
|
+
streetAddress: zod_1.z.string().nonempty(),
|
56
|
+
city: zod_1.z.string().nonempty(),
|
57
|
+
country: zod_1.z.string().nonempty(),
|
58
|
+
})),
|
59
|
+
pets: zod_1.z
|
60
|
+
.object({
|
61
|
+
animal: zod_1.z.string().nonempty(),
|
62
|
+
name: zod_1.z.string().nonempty(),
|
63
|
+
})
|
64
|
+
.array()
|
65
|
+
.optional(),
|
41
66
|
})),
|
42
67
|
},
|
43
68
|
];
|
@@ -47,41 +72,168 @@ describe("Validation", () => {
|
|
47
72
|
describe.each(validationTestCases)("Adapter for $name", ({ validator }) => {
|
48
73
|
describe("validate", () => {
|
49
74
|
it("should return the data when valid", () => {
|
50
|
-
const
|
75
|
+
const person = {
|
51
76
|
firstName: "John",
|
52
77
|
lastName: "Doe",
|
53
78
|
age: 30,
|
79
|
+
address: {
|
80
|
+
streetAddress: "123 Main St",
|
81
|
+
city: "Anytown",
|
82
|
+
country: "USA",
|
83
|
+
},
|
84
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
54
85
|
};
|
55
|
-
expect(validator.validate(
|
56
|
-
data:
|
86
|
+
expect(validator.validate(person)).toEqual({
|
87
|
+
data: person,
|
57
88
|
error: undefined,
|
58
89
|
});
|
59
90
|
});
|
60
91
|
it("should return field errors when invalid", () => {
|
61
|
-
const obj = { age: "hi!" };
|
92
|
+
const obj = { age: "hi!", pets: [{ animal: "dog" }] };
|
62
93
|
expect(validator.validate(obj)).toEqual({
|
63
94
|
data: undefined,
|
64
95
|
error: {
|
65
96
|
firstName: anyString,
|
66
97
|
lastName: anyString,
|
67
98
|
age: anyString,
|
99
|
+
"address.city": anyString,
|
100
|
+
"address.country": anyString,
|
101
|
+
"address.streetAddress": anyString,
|
102
|
+
"pets[0].name": anyString,
|
103
|
+
_submittedData: obj,
|
104
|
+
},
|
105
|
+
});
|
106
|
+
});
|
107
|
+
it("should unflatten data when validating", () => {
|
108
|
+
const data = {
|
109
|
+
firstName: "John",
|
110
|
+
lastName: "Doe",
|
111
|
+
age: 30,
|
112
|
+
"address.streetAddress": "123 Main St",
|
113
|
+
"address.city": "Anytown",
|
114
|
+
"address.country": "USA",
|
115
|
+
"pets[0].animal": "dog",
|
116
|
+
"pets[0].name": "Fido",
|
117
|
+
};
|
118
|
+
expect(validator.validate(data)).toEqual({
|
119
|
+
data: {
|
120
|
+
firstName: "John",
|
121
|
+
lastName: "Doe",
|
122
|
+
age: 30,
|
123
|
+
address: {
|
124
|
+
streetAddress: "123 Main St",
|
125
|
+
city: "Anytown",
|
126
|
+
country: "USA",
|
127
|
+
},
|
128
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
129
|
+
},
|
130
|
+
error: undefined,
|
131
|
+
});
|
132
|
+
});
|
133
|
+
it("should accept FormData directly and return errors", () => {
|
134
|
+
const formData = new testFormData_1.TestFormData();
|
135
|
+
formData.set("firstName", "John");
|
136
|
+
formData.set("lastName", "Doe");
|
137
|
+
formData.set("address.streetAddress", "123 Main St");
|
138
|
+
formData.set("address.country", "USA");
|
139
|
+
formData.set("pets[0].animal", "dog");
|
140
|
+
expect(validator.validate(formData)).toEqual({
|
141
|
+
data: undefined,
|
142
|
+
error: {
|
143
|
+
"address.city": anyString,
|
144
|
+
"pets[0].name": anyString,
|
145
|
+
_submittedData: (0, flatten_1.objectFromPathEntries)([...formData.entries()]),
|
68
146
|
},
|
69
147
|
});
|
70
148
|
});
|
149
|
+
it("should accept FormData directly and return valid data", () => {
|
150
|
+
const formData = new testFormData_1.TestFormData();
|
151
|
+
formData.set("firstName", "John");
|
152
|
+
formData.set("lastName", "Doe");
|
153
|
+
formData.set("address.streetAddress", "123 Main St");
|
154
|
+
formData.set("address.country", "USA");
|
155
|
+
formData.set("address.city", "Anytown");
|
156
|
+
formData.set("pets[0].animal", "dog");
|
157
|
+
formData.set("pets[0].name", "Fido");
|
158
|
+
expect(validator.validate(formData)).toEqual({
|
159
|
+
data: {
|
160
|
+
firstName: "John",
|
161
|
+
lastName: "Doe",
|
162
|
+
address: {
|
163
|
+
streetAddress: "123 Main St",
|
164
|
+
country: "USA",
|
165
|
+
city: "Anytown",
|
166
|
+
},
|
167
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
168
|
+
},
|
169
|
+
error: undefined,
|
170
|
+
});
|
171
|
+
});
|
71
172
|
});
|
72
173
|
describe("validateField", () => {
|
73
174
|
it("should not return an error if field is valid", () => {
|
74
|
-
const
|
175
|
+
const person = {
|
75
176
|
firstName: "John",
|
76
177
|
lastName: {}, // invalid, but we should only be validating firstName
|
77
178
|
};
|
78
|
-
expect(validator.validateField(
|
179
|
+
expect(validator.validateField(person, "firstName")).toEqual({
|
180
|
+
error: undefined,
|
181
|
+
});
|
182
|
+
});
|
183
|
+
it("should not return an error if a nested field is valid", () => {
|
184
|
+
const person = {
|
185
|
+
firstName: "John",
|
186
|
+
lastName: {},
|
187
|
+
address: {
|
188
|
+
streetAddress: "123 Main St",
|
189
|
+
city: "Anytown",
|
190
|
+
country: "USA",
|
191
|
+
},
|
192
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
193
|
+
};
|
194
|
+
expect(validator.validateField(person, "address.streetAddress")).toEqual({
|
195
|
+
error: undefined,
|
196
|
+
});
|
197
|
+
expect(validator.validateField(person, "address.city")).toEqual({
|
198
|
+
error: undefined,
|
199
|
+
});
|
200
|
+
expect(validator.validateField(person, "address.country")).toEqual({
|
201
|
+
error: undefined,
|
202
|
+
});
|
203
|
+
expect(validator.validateField(person, "pets[0].animal")).toEqual({
|
204
|
+
error: undefined,
|
205
|
+
});
|
206
|
+
expect(validator.validateField(person, "pets[0].name")).toEqual({
|
79
207
|
error: undefined,
|
80
208
|
});
|
81
209
|
});
|
82
210
|
it("should return an error if field is invalid", () => {
|
83
|
-
const
|
84
|
-
|
211
|
+
const person = {
|
212
|
+
firstName: "John",
|
213
|
+
lastName: {},
|
214
|
+
address: {
|
215
|
+
streetAddress: "123 Main St",
|
216
|
+
city: 1234,
|
217
|
+
},
|
218
|
+
};
|
219
|
+
expect(validator.validateField(person, "lastName")).toEqual({
|
220
|
+
error: anyString,
|
221
|
+
});
|
222
|
+
});
|
223
|
+
it("should return an error if a nested field is invalid", () => {
|
224
|
+
const person = {
|
225
|
+
firstName: "John",
|
226
|
+
lastName: {},
|
227
|
+
address: {
|
228
|
+
streetAddress: "123 Main St",
|
229
|
+
city: 1234,
|
230
|
+
},
|
231
|
+
pets: [{ animal: "dog" }],
|
232
|
+
};
|
233
|
+
expect(validator.validateField(person, "address.country")).toEqual({
|
234
|
+
error: anyString,
|
235
|
+
});
|
236
|
+
expect(validator.validateField(person, "pets[0].name")).toEqual({
|
85
237
|
error: anyString,
|
86
238
|
});
|
87
239
|
});
|
@@ -111,6 +263,7 @@ describe("withZod", () => {
|
|
111
263
|
type: anyString,
|
112
264
|
bar: anyString,
|
113
265
|
foo: anyString,
|
266
|
+
_submittedData: obj,
|
114
267
|
},
|
115
268
|
});
|
116
269
|
});
|
@@ -129,6 +282,7 @@ describe("withZod", () => {
|
|
129
282
|
error: {
|
130
283
|
field1: anyString,
|
131
284
|
field2: anyString,
|
285
|
+
_submittedData: obj,
|
132
286
|
},
|
133
287
|
});
|
134
288
|
expect(validator.validateField(obj, "field1")).toEqual({
|
@@ -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>>;
|