@tanstack/react-form 0.35.0 → 0.36.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.
- package/dist/cjs/remix/createServerValidate.cjs +43 -0
- package/dist/cjs/remix/createServerValidate.cjs.map +1 -0
- package/dist/cjs/remix/createServerValidate.d.cts +13 -0
- package/dist/cjs/remix/error.cjs +11 -0
- package/dist/cjs/remix/error.cjs.map +1 -0
- package/dist/cjs/remix/error.d.cts +9 -0
- package/dist/cjs/remix/index.cjs +15 -0
- package/dist/cjs/remix/index.cjs.map +1 -0
- package/dist/cjs/remix/index.d.cts +4 -0
- package/dist/cjs/remix/types.d.cts +2 -0
- package/dist/esm/remix/createServerValidate.d.ts +13 -0
- package/dist/esm/remix/createServerValidate.js +43 -0
- package/dist/esm/remix/createServerValidate.js.map +1 -0
- package/dist/esm/remix/error.d.ts +9 -0
- package/dist/esm/remix/error.js +11 -0
- package/dist/esm/remix/error.js.map +1 -0
- package/dist/esm/remix/index.d.ts +4 -0
- package/dist/esm/remix/index.js +9 -0
- package/dist/esm/remix/index.js.map +1 -0
- package/dist/esm/remix/types.d.ts +2 -0
- package/package.json +12 -2
- package/src/remix/createServerValidate.ts +94 -0
- package/src/remix/error.ts +18 -0
- package/src/remix/index.ts +5 -0
- package/src/remix/types.ts +6 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const decodeFormdata = require("decode-formdata");
|
|
4
|
+
const error = require("./error.cjs");
|
|
5
|
+
const isFormValidationError = (error2) => {
|
|
6
|
+
return typeof error2 === "object";
|
|
7
|
+
};
|
|
8
|
+
const createServerValidate = (defaultOpts) => async (formData, info) => {
|
|
9
|
+
const { validatorAdapter, onServerValidate } = defaultOpts;
|
|
10
|
+
const runValidator = async (propsValue) => {
|
|
11
|
+
if (validatorAdapter && typeof onServerValidate !== "function") {
|
|
12
|
+
return validatorAdapter().validateAsync(propsValue, onServerValidate);
|
|
13
|
+
}
|
|
14
|
+
return onServerValidate(propsValue);
|
|
15
|
+
};
|
|
16
|
+
const values = decodeFormdata.decode(formData, info);
|
|
17
|
+
const onServerError = await runValidator({
|
|
18
|
+
value: values,
|
|
19
|
+
validationSource: "form"
|
|
20
|
+
});
|
|
21
|
+
if (!onServerError) return;
|
|
22
|
+
const onServerErrorStr = onServerError && typeof onServerError !== "string" && isFormValidationError(onServerError) ? onServerError.form : onServerError;
|
|
23
|
+
const formState = {
|
|
24
|
+
errorMap: {
|
|
25
|
+
onServer: onServerError
|
|
26
|
+
},
|
|
27
|
+
values,
|
|
28
|
+
errors: onServerErrorStr ? [onServerErrorStr] : []
|
|
29
|
+
};
|
|
30
|
+
throw new error.ServerValidateError({
|
|
31
|
+
formState
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
const initialFormState = {
|
|
35
|
+
errorMap: {
|
|
36
|
+
onServer: void 0
|
|
37
|
+
},
|
|
38
|
+
values: void 0,
|
|
39
|
+
errors: []
|
|
40
|
+
};
|
|
41
|
+
exports.createServerValidate = createServerValidate;
|
|
42
|
+
exports.initialFormState = initialFormState;
|
|
43
|
+
//# sourceMappingURL=createServerValidate.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createServerValidate.cjs","sources":["../../../src/remix/createServerValidate.ts"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport { ServerValidateError } from './error'\nimport type {\n FormOptions,\n FormValidationError,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\nimport type { ServerFormState } from './types'\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError | Promise<ValidationError>\n\ntype OnServerValidateOrFn<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> =\n TFormValidator extends Validator<TFormData, infer FFN>\n ? FFN | OnServerValidateFn<TFormData>\n : OnServerValidateFn<TFormData>\n\ninterface CreateServerValidateOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate: OnServerValidateOrFn<TFormData, TFormValidator>\n}\n\nconst isFormValidationError = (\n error: unknown,\n): error is FormValidationError<unknown> => {\n return typeof error === 'object'\n}\n\nexport const createServerValidate =\n <\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n >(\n defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,\n ) =>\n async (formData: FormData, info?: Parameters<typeof decode>[1]) => {\n const { validatorAdapter, onServerValidate } = defaultOpts\n\n const runValidator = async (propsValue: {\n value: TFormData\n validationSource: 'form'\n }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validateAsync(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const values = decode(formData, info) as never as TFormData\n\n const onServerError = await runValidator({\n value: values,\n validationSource: 'form',\n })\n\n if (!onServerError) return\n\n const onServerErrorStr =\n onServerError &&\n typeof onServerError !== 'string' &&\n isFormValidationError(onServerError)\n ? onServerError.form\n : onServerError\n\n const formState: ServerFormState<TFormData> = {\n errorMap: {\n onServer: onServerError,\n },\n values,\n errors: onServerErrorStr ? [onServerErrorStr] : [],\n }\n\n throw new ServerValidateError({\n formState,\n })\n }\n\nexport const initialFormState: ServerFormState<any> = {\n errorMap: {\n onServer: undefined,\n },\n values: undefined,\n errors: [],\n}\n"],"names":["error","decode","ServerValidateError"],"mappings":";;;;AA6BA,MAAM,wBAAwB,CAC5BA,WAC0C;AAC1C,SAAO,OAAOA,WAAU;AAC1B;AAEO,MAAM,uBACX,CAME,gBAEF,OAAO,UAAoB,SAAwC;AAC3D,QAAA,EAAE,kBAAkB,iBAAA,IAAqB;AAEzC,QAAA,eAAe,OAAO,eAGtB;AACA,QAAA,oBAAoB,OAAO,qBAAqB,YAAY;AAC9D,aAAO,iBAAiB,EAAE,cAAc,YAAY,gBAAgB;AAAA,IAAA;AAGtE,WAAQ,iBAAmD,UAAU;AAAA,EACvE;AAEM,QAAA,SAASC,eAAAA,OAAO,UAAU,IAAI;AAE9B,QAAA,gBAAgB,MAAM,aAAa;AAAA,IACvC,OAAO;AAAA,IACP,kBAAkB;AAAA,EAAA,CACnB;AAED,MAAI,CAAC,cAAe;AAEd,QAAA,mBACJ,iBACA,OAAO,kBAAkB,YACzB,sBAAsB,aAAa,IAC/B,cAAc,OACd;AAEN,QAAM,YAAwC;AAAA,IAC5C,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA;AAAA,IACA,QAAQ,mBAAmB,CAAC,gBAAgB,IAAI,CAAA;AAAA,EAClD;AAEA,QAAM,IAAIC,MAAAA,oBAAoB;AAAA,IAC5B;AAAA,EAAA,CACD;AACH;AAEK,MAAM,mBAAyC;AAAA,EACpD,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ;AAAA,EACR,QAAQ,CAAA;AACV;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata';
|
|
2
|
+
import { FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
+
import { ServerFormState } from './types.cjs';
|
|
4
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
5
|
+
value: TFormData;
|
|
6
|
+
}) => ValidationError | Promise<ValidationError>;
|
|
7
|
+
type OnServerValidateOrFn<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = TFormValidator extends Validator<TFormData, infer FFN> ? FFN | OnServerValidateFn<TFormData> : OnServerValidateFn<TFormData>;
|
|
8
|
+
interface CreateServerValidateOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
9
|
+
onServerValidate: OnServerValidateOrFn<TFormData, TFormValidator>;
|
|
10
|
+
}
|
|
11
|
+
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>) => (formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<void>;
|
|
12
|
+
export declare const initialFormState: ServerFormState<any>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
class ServerValidateError extends Error {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
super("Your form has errors. Please check the fields and try again.");
|
|
6
|
+
this.name = "ServerValidateError";
|
|
7
|
+
this.formState = options.formState;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.ServerValidateError = ServerValidateError;
|
|
11
|
+
//# sourceMappingURL=error.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.cjs","sources":["../../../src/remix/error.ts"],"sourcesContent":["import type { ServerFormState } from './types'\n\ninterface ServerValidateErrorState<TFormData> {\n formState: ServerFormState<TFormData>\n}\n\nexport class ServerValidateError<TFormData>\n extends Error\n implements ServerValidateErrorState<TFormData>\n{\n formState: ServerFormState<TFormData>\n\n constructor(options: ServerValidateErrorState<TFormData>) {\n super('Your form has errors. Please check the fields and try again.')\n this.name = 'ServerValidateError'\n this.formState = options.formState\n }\n}\n"],"names":[],"mappings":";;AAMO,MAAM,4BACH,MAEV;AAAA,EAGE,YAAY,SAA8C;AACxD,UAAM,8DAA8D;AACpE,SAAK,OAAO;AACZ,SAAK,YAAY,QAAQ;AAAA,EAAA;AAE7B;;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ServerFormState } from './types.cjs';
|
|
2
|
+
interface ServerValidateErrorState<TFormData> {
|
|
3
|
+
formState: ServerFormState<TFormData>;
|
|
4
|
+
}
|
|
5
|
+
export declare class ServerValidateError<TFormData> extends Error implements ServerValidateErrorState<TFormData> {
|
|
6
|
+
formState: ServerFormState<TFormData>;
|
|
7
|
+
constructor(options: ServerValidateErrorState<TFormData>);
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const formCore = require("@tanstack/form-core");
|
|
4
|
+
const createServerValidate = require("./createServerValidate.cjs");
|
|
5
|
+
const error = require("./error.cjs");
|
|
6
|
+
exports.createServerValidate = createServerValidate.createServerValidate;
|
|
7
|
+
exports.initialFormState = createServerValidate.initialFormState;
|
|
8
|
+
exports.ServerValidateError = error.ServerValidateError;
|
|
9
|
+
Object.keys(formCore).forEach((k) => {
|
|
10
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: () => formCore[k]
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata';
|
|
2
|
+
import { FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
+
import { ServerFormState } from './types.js';
|
|
4
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
5
|
+
value: TFormData;
|
|
6
|
+
}) => ValidationError | Promise<ValidationError>;
|
|
7
|
+
type OnServerValidateOrFn<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = TFormValidator extends Validator<TFormData, infer FFN> ? FFN | OnServerValidateFn<TFormData> : OnServerValidateFn<TFormData>;
|
|
8
|
+
interface CreateServerValidateOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
9
|
+
onServerValidate: OnServerValidateOrFn<TFormData, TFormValidator>;
|
|
10
|
+
}
|
|
11
|
+
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>) => (formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<void>;
|
|
12
|
+
export declare const initialFormState: ServerFormState<any>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { decode } from "decode-formdata";
|
|
2
|
+
import { ServerValidateError } from "./error.js";
|
|
3
|
+
const isFormValidationError = (error) => {
|
|
4
|
+
return typeof error === "object";
|
|
5
|
+
};
|
|
6
|
+
const createServerValidate = (defaultOpts) => async (formData, info) => {
|
|
7
|
+
const { validatorAdapter, onServerValidate } = defaultOpts;
|
|
8
|
+
const runValidator = async (propsValue) => {
|
|
9
|
+
if (validatorAdapter && typeof onServerValidate !== "function") {
|
|
10
|
+
return validatorAdapter().validateAsync(propsValue, onServerValidate);
|
|
11
|
+
}
|
|
12
|
+
return onServerValidate(propsValue);
|
|
13
|
+
};
|
|
14
|
+
const values = decode(formData, info);
|
|
15
|
+
const onServerError = await runValidator({
|
|
16
|
+
value: values,
|
|
17
|
+
validationSource: "form"
|
|
18
|
+
});
|
|
19
|
+
if (!onServerError) return;
|
|
20
|
+
const onServerErrorStr = onServerError && typeof onServerError !== "string" && isFormValidationError(onServerError) ? onServerError.form : onServerError;
|
|
21
|
+
const formState = {
|
|
22
|
+
errorMap: {
|
|
23
|
+
onServer: onServerError
|
|
24
|
+
},
|
|
25
|
+
values,
|
|
26
|
+
errors: onServerErrorStr ? [onServerErrorStr] : []
|
|
27
|
+
};
|
|
28
|
+
throw new ServerValidateError({
|
|
29
|
+
formState
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
const initialFormState = {
|
|
33
|
+
errorMap: {
|
|
34
|
+
onServer: void 0
|
|
35
|
+
},
|
|
36
|
+
values: void 0,
|
|
37
|
+
errors: []
|
|
38
|
+
};
|
|
39
|
+
export {
|
|
40
|
+
createServerValidate,
|
|
41
|
+
initialFormState
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=createServerValidate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createServerValidate.js","sources":["../../../src/remix/createServerValidate.ts"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport { ServerValidateError } from './error'\nimport type {\n FormOptions,\n FormValidationError,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\nimport type { ServerFormState } from './types'\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError | Promise<ValidationError>\n\ntype OnServerValidateOrFn<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> =\n TFormValidator extends Validator<TFormData, infer FFN>\n ? FFN | OnServerValidateFn<TFormData>\n : OnServerValidateFn<TFormData>\n\ninterface CreateServerValidateOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate: OnServerValidateOrFn<TFormData, TFormValidator>\n}\n\nconst isFormValidationError = (\n error: unknown,\n): error is FormValidationError<unknown> => {\n return typeof error === 'object'\n}\n\nexport const createServerValidate =\n <\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n >(\n defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,\n ) =>\n async (formData: FormData, info?: Parameters<typeof decode>[1]) => {\n const { validatorAdapter, onServerValidate } = defaultOpts\n\n const runValidator = async (propsValue: {\n value: TFormData\n validationSource: 'form'\n }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validateAsync(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const values = decode(formData, info) as never as TFormData\n\n const onServerError = await runValidator({\n value: values,\n validationSource: 'form',\n })\n\n if (!onServerError) return\n\n const onServerErrorStr =\n onServerError &&\n typeof onServerError !== 'string' &&\n isFormValidationError(onServerError)\n ? onServerError.form\n : onServerError\n\n const formState: ServerFormState<TFormData> = {\n errorMap: {\n onServer: onServerError,\n },\n values,\n errors: onServerErrorStr ? [onServerErrorStr] : [],\n }\n\n throw new ServerValidateError({\n formState,\n })\n }\n\nexport const initialFormState: ServerFormState<any> = {\n errorMap: {\n onServer: undefined,\n },\n values: undefined,\n errors: [],\n}\n"],"names":[],"mappings":";;AA6BA,MAAM,wBAAwB,CAC5B,UAC0C;AAC1C,SAAO,OAAO,UAAU;AAC1B;AAEO,MAAM,uBACX,CAME,gBAEF,OAAO,UAAoB,SAAwC;AAC3D,QAAA,EAAE,kBAAkB,iBAAA,IAAqB;AAEzC,QAAA,eAAe,OAAO,eAGtB;AACA,QAAA,oBAAoB,OAAO,qBAAqB,YAAY;AAC9D,aAAO,iBAAiB,EAAE,cAAc,YAAY,gBAAgB;AAAA,IAAA;AAGtE,WAAQ,iBAAmD,UAAU;AAAA,EACvE;AAEM,QAAA,SAAS,OAAO,UAAU,IAAI;AAE9B,QAAA,gBAAgB,MAAM,aAAa;AAAA,IACvC,OAAO;AAAA,IACP,kBAAkB;AAAA,EAAA,CACnB;AAED,MAAI,CAAC,cAAe;AAEd,QAAA,mBACJ,iBACA,OAAO,kBAAkB,YACzB,sBAAsB,aAAa,IAC/B,cAAc,OACd;AAEN,QAAM,YAAwC;AAAA,IAC5C,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA;AAAA,IACA,QAAQ,mBAAmB,CAAC,gBAAgB,IAAI,CAAA;AAAA,EAClD;AAEA,QAAM,IAAI,oBAAoB;AAAA,IAC5B;AAAA,EAAA,CACD;AACH;AAEK,MAAM,mBAAyC;AAAA,EACpD,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ;AAAA,EACR,QAAQ,CAAA;AACV;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ServerFormState } from './types.js';
|
|
2
|
+
interface ServerValidateErrorState<TFormData> {
|
|
3
|
+
formState: ServerFormState<TFormData>;
|
|
4
|
+
}
|
|
5
|
+
export declare class ServerValidateError<TFormData> extends Error implements ServerValidateErrorState<TFormData> {
|
|
6
|
+
formState: ServerFormState<TFormData>;
|
|
7
|
+
constructor(options: ServerValidateErrorState<TFormData>);
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
class ServerValidateError extends Error {
|
|
2
|
+
constructor(options) {
|
|
3
|
+
super("Your form has errors. Please check the fields and try again.");
|
|
4
|
+
this.name = "ServerValidateError";
|
|
5
|
+
this.formState = options.formState;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
ServerValidateError
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sources":["../../../src/remix/error.ts"],"sourcesContent":["import type { ServerFormState } from './types'\n\ninterface ServerValidateErrorState<TFormData> {\n formState: ServerFormState<TFormData>\n}\n\nexport class ServerValidateError<TFormData>\n extends Error\n implements ServerValidateErrorState<TFormData>\n{\n formState: ServerFormState<TFormData>\n\n constructor(options: ServerValidateErrorState<TFormData>) {\n super('Your form has errors. Please check the fields and try again.')\n this.name = 'ServerValidateError'\n this.formState = options.formState\n }\n}\n"],"names":[],"mappings":"AAMO,MAAM,4BACH,MAEV;AAAA,EAGE,YAAY,SAA8C;AACxD,UAAM,8DAA8D;AACpE,SAAK,OAAO;AACZ,SAAK,YAAY,QAAQ;AAAA,EAAA;AAE7B;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "@tanstack/form-core";
|
|
2
|
+
import { createServerValidate, initialFormState } from "./createServerValidate.js";
|
|
3
|
+
import { ServerValidateError } from "./error.js";
|
|
4
|
+
export {
|
|
5
|
+
ServerValidateError,
|
|
6
|
+
createServerValidate,
|
|
7
|
+
initialFormState
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-form",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.1",
|
|
4
4
|
"description": "Powerful, type-safe forms for React.",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,6 +39,16 @@
|
|
|
39
39
|
"default": "./dist/cjs/nextjs/index.cjs"
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
|
+
"./remix": {
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./dist/esm/remix/index.d.ts",
|
|
45
|
+
"default": "./dist/esm/remix/index.js"
|
|
46
|
+
},
|
|
47
|
+
"require": {
|
|
48
|
+
"types": "./dist/cjs/remix/index.d.cts",
|
|
49
|
+
"default": "./dist/cjs/remix/index.cjs"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
42
52
|
"./start": {
|
|
43
53
|
"import": {
|
|
44
54
|
"types": "./dist/esm/start/index.d.ts",
|
|
@@ -60,7 +70,7 @@
|
|
|
60
70
|
"@remix-run/node": "^2.14.0",
|
|
61
71
|
"@tanstack/react-store": "^0.5.6",
|
|
62
72
|
"decode-formdata": "^0.8.0",
|
|
63
|
-
"@tanstack/form-core": "0.
|
|
73
|
+
"@tanstack/form-core": "0.36.1"
|
|
64
74
|
},
|
|
65
75
|
"devDependencies": {
|
|
66
76
|
"@tanstack/start": "^1.81.1",
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata'
|
|
2
|
+
import { ServerValidateError } from './error'
|
|
3
|
+
import type {
|
|
4
|
+
FormOptions,
|
|
5
|
+
FormValidationError,
|
|
6
|
+
ValidationError,
|
|
7
|
+
Validator,
|
|
8
|
+
} from '@tanstack/form-core'
|
|
9
|
+
import type { ServerFormState } from './types'
|
|
10
|
+
|
|
11
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
12
|
+
value: TFormData
|
|
13
|
+
}) => ValidationError | Promise<ValidationError>
|
|
14
|
+
|
|
15
|
+
type OnServerValidateOrFn<
|
|
16
|
+
TFormData,
|
|
17
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
18
|
+
> =
|
|
19
|
+
TFormValidator extends Validator<TFormData, infer FFN>
|
|
20
|
+
? FFN | OnServerValidateFn<TFormData>
|
|
21
|
+
: OnServerValidateFn<TFormData>
|
|
22
|
+
|
|
23
|
+
interface CreateServerValidateOptions<
|
|
24
|
+
TFormData,
|
|
25
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
26
|
+
> extends FormOptions<TFormData, TFormValidator> {
|
|
27
|
+
onServerValidate: OnServerValidateOrFn<TFormData, TFormValidator>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const isFormValidationError = (
|
|
31
|
+
error: unknown,
|
|
32
|
+
): error is FormValidationError<unknown> => {
|
|
33
|
+
return typeof error === 'object'
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const createServerValidate =
|
|
37
|
+
<
|
|
38
|
+
TFormData,
|
|
39
|
+
TFormValidator extends
|
|
40
|
+
| Validator<TFormData, unknown>
|
|
41
|
+
| undefined = undefined,
|
|
42
|
+
>(
|
|
43
|
+
defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,
|
|
44
|
+
) =>
|
|
45
|
+
async (formData: FormData, info?: Parameters<typeof decode>[1]) => {
|
|
46
|
+
const { validatorAdapter, onServerValidate } = defaultOpts
|
|
47
|
+
|
|
48
|
+
const runValidator = async (propsValue: {
|
|
49
|
+
value: TFormData
|
|
50
|
+
validationSource: 'form'
|
|
51
|
+
}) => {
|
|
52
|
+
if (validatorAdapter && typeof onServerValidate !== 'function') {
|
|
53
|
+
return validatorAdapter().validateAsync(propsValue, onServerValidate)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const values = decode(formData, info) as never as TFormData
|
|
60
|
+
|
|
61
|
+
const onServerError = await runValidator({
|
|
62
|
+
value: values,
|
|
63
|
+
validationSource: 'form',
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
if (!onServerError) return
|
|
67
|
+
|
|
68
|
+
const onServerErrorStr =
|
|
69
|
+
onServerError &&
|
|
70
|
+
typeof onServerError !== 'string' &&
|
|
71
|
+
isFormValidationError(onServerError)
|
|
72
|
+
? onServerError.form
|
|
73
|
+
: onServerError
|
|
74
|
+
|
|
75
|
+
const formState: ServerFormState<TFormData> = {
|
|
76
|
+
errorMap: {
|
|
77
|
+
onServer: onServerError,
|
|
78
|
+
},
|
|
79
|
+
values,
|
|
80
|
+
errors: onServerErrorStr ? [onServerErrorStr] : [],
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
throw new ServerValidateError({
|
|
84
|
+
formState,
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const initialFormState: ServerFormState<any> = {
|
|
89
|
+
errorMap: {
|
|
90
|
+
onServer: undefined,
|
|
91
|
+
},
|
|
92
|
+
values: undefined,
|
|
93
|
+
errors: [],
|
|
94
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ServerFormState } from './types'
|
|
2
|
+
|
|
3
|
+
interface ServerValidateErrorState<TFormData> {
|
|
4
|
+
formState: ServerFormState<TFormData>
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class ServerValidateError<TFormData>
|
|
8
|
+
extends Error
|
|
9
|
+
implements ServerValidateErrorState<TFormData>
|
|
10
|
+
{
|
|
11
|
+
formState: ServerFormState<TFormData>
|
|
12
|
+
|
|
13
|
+
constructor(options: ServerValidateErrorState<TFormData>) {
|
|
14
|
+
super('Your form has errors. Please check the fields and try again.')
|
|
15
|
+
this.name = 'ServerValidateError'
|
|
16
|
+
this.formState = options.formState
|
|
17
|
+
}
|
|
18
|
+
}
|