v1.0 with SW PWA enabled
This commit is contained in:
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/index.d.ts
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/index.d.ts
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './superstruct';
|
||||
export * from './types';
|
||||
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.d.ts
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.d.ts
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import { Resolver } from './types';
|
||||
export declare const superstructResolver: Resolver;
|
||||
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.js
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.js
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var e=require("@hookform/resolvers"),r=require("superstruct");exports.superstructResolver=function(s,t,a){return void 0===a&&(a={}),function(o,u,i){var l,n=r.validate(o,s,t);return n[0]?{values:{},errors:e.toNestErrors((l=n[0],l.failures().reduce(function(e,r){return(e[r.path.join(".")]={message:r.message,type:r.type})&&e},{})),i)}:(i.shouldUseNativeValidation&&e.validateFieldsNatively({},i),{values:a.raw?o:n[1],errors:{}})}};
|
||||
//# sourceMappingURL=superstruct.js.map
|
||||
1
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.js.map
generated
vendored
Normal file
1
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.js.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"superstruct.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\n\nimport { StructError, validate } from 'superstruct';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (error: StructError) =>\n error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n\nexport const superstructResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? values : result[1],\n errors: {},\n };\n };\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw"],"mappings":"0FAiBE,SAACA,EAAQC,EAAeC,GACxB,YADwBA,IAAAA,IAAAA,EAAkB,CAAA,GAC1C,SAACC,EAAQC,EAAGC,GACV,IAbsBC,EAahBC,EAASC,EAAQA,SAACL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAE,EACVM,OAAQC,EAAAA,cAlBUJ,EAkBoBC,EAAO,GAjBnDD,EAAMK,WAAWC,OACf,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IAWsDR,KAItDA,EAAQa,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEd,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMjB,EAASI,EAAO,GAC9CE,OAAQ,CAAA,GAEZ,CAAC"}
|
||||
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.mjs
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.mjs
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";var t=function(t,s,u){return void 0===u&&(u={}),function(a,i,n){var f,m=o(a,t,s);return m[0]?{values:{},errors:r((f=m[0],f.failures().reduce(function(r,e){return(r[e.path.join(".")]={message:e.message,type:e.type})&&r},{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:u.raw?a:m[1],errors:{}})}};export{t as superstructResolver};
|
||||
//# sourceMappingURL=superstruct.module.js.map
|
||||
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.modern.mjs
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.modern.mjs
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as s}from"superstruct";const o=(o,t,a={})=>(u,i,m)=>{const p=s(u,o,t);return p[0]?{values:{},errors:r((l=p[0],l.failures().reduce((r,e)=>(r[e.path.join(".")]={message:e.message,type:e.type})&&r,{})),m)}:(m.shouldUseNativeValidation&&e({},m),{values:a.raw?u:p[1],errors:{}});var l};export{o as superstructResolver};
|
||||
//# sourceMappingURL=superstruct.modern.mjs.map
|
||||
1
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.modern.mjs.map
generated
vendored
Normal file
1
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.modern.mjs.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"superstruct.modern.mjs","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\n\nimport { StructError, validate } from 'superstruct';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (error: StructError) =>\n error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n\nexport const superstructResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? values : result[1],\n errors: {},\n };\n };\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","result","validate","errors","toNestErrors","error","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw"],"mappings":"sHAMA,MAUaA,EACXA,CAACC,EAAQC,EAAeC,EAAkB,CAAE,IAC5C,CAACC,EAAQC,EAAGC,KACV,MAAMC,EAASC,EAASJ,EAAQH,EAAQC,GAExC,OAAIK,EAAO,GACF,CACLH,OAAQ,CAAA,EACRK,OAAQC,GAlBUC,EAkBoBJ,EAAO,GAjBnDI,EAAMC,WAAWC,OACf,CAACC,EAAUH,KACRG,EAASH,EAAMI,KAAKC,KAAK,MAAQ,CAChCC,QAASN,EAAMM,QACfC,KAAMP,EAAMO,QACRJ,EACR,CAAA,IAWsDR,KAItDA,EAAQa,2BAA6BC,EAAuB,CAAE,EAAEd,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMjB,EAASG,EAAO,GAC9CE,OAAQ,KA1BYE"}
|
||||
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.module.js
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.module.js
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";var t=function(t,s,u){return void 0===u&&(u={}),function(a,i,n){var f,m=o(a,t,s);return m[0]?{values:{},errors:r((f=m[0],f.failures().reduce(function(r,e){return(r[e.path.join(".")]={message:e.message,type:e.type})&&r},{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:u.raw?a:m[1],errors:{}})}};export{t as superstructResolver};
|
||||
//# sourceMappingURL=superstruct.module.js.map
|
||||
1
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.module.js.map
generated
vendored
Normal file
1
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.module.js.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"superstruct.module.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\n\nimport { StructError, validate } from 'superstruct';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (error: StructError) =>\n error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n\nexport const superstructResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? values : result[1],\n errors: {},\n };\n };\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw"],"mappings":"sHAMA,IAUaA,EACX,SAACC,EAAQC,EAAeC,GACxB,YADwBA,IAAAA,IAAAA,EAAkB,CAAA,GAC1C,SAACC,EAAQC,EAAGC,GACV,IAbsBC,EAahBC,EAASC,EAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAE,EACVM,OAAQC,GAlBUJ,EAkBoBC,EAAO,GAjBnDD,EAAMK,WAAWC,OACf,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IAWsDR,KAItDA,EAAQa,2BAA6BC,EAAuB,CAAE,EAAEd,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMjB,EAASI,EAAO,GAC9CE,OAAQ,CAAA,GAEZ,CAAC"}
|
||||
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.umd.js
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.umd.js
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers"),require("superstruct")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","superstruct"],r):r((e||self).hookformResolversSuperstruct={},e.hookformResolvers,e.superstruct)}(this,function(e,r,o){e.superstructResolver=function(e,s,t){return void 0===t&&(t={}),function(u,i,n){var f,l=o.validate(u,e,s);return l[0]?{values:{},errors:r.toNestErrors((f=l[0],f.failures().reduce(function(e,r){return(e[r.path.join(".")]={message:r.message,type:r.type})&&e},{})),n)}:(n.shouldUseNativeValidation&&r.validateFieldsNatively({},n),{values:t.raw?u:l[1],errors:{}})}}});
|
||||
//# sourceMappingURL=superstruct.umd.js.map
|
||||
1
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.umd.js.map
generated
vendored
Normal file
1
frontend/node_modules/@hookform/resolvers/superstruct/dist/superstruct.umd.js.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"superstruct.umd.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\n\nimport { StructError, validate } from 'superstruct';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (error: StructError) =>\n error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n\nexport const superstructResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? values : result[1],\n errors: {},\n };\n };\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw"],"mappings":"4YAiBE,SAACA,EAAQC,EAAeC,GACxB,YADwBA,IAAAA,IAAAA,EAAkB,CAAA,GAC1C,SAACC,EAAQC,EAAGC,GACV,IAbsBC,EAahBC,EAASC,EAAQA,SAACL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAE,EACVM,OAAQC,EAAAA,cAlBUJ,EAkBoBC,EAAO,GAjBnDD,EAAMK,WAAWC,OACf,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IAWsDR,KAItDA,EAAQa,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEd,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMjB,EAASI,EAAO,GAC9CE,OAAQ,CAAA,GAEZ,CAAC"}
|
||||
11
frontend/node_modules/@hookform/resolvers/superstruct/dist/types.d.ts
generated
vendored
Normal file
11
frontend/node_modules/@hookform/resolvers/superstruct/dist/types.d.ts
generated
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
||||
import { Struct, validate } from 'superstruct';
|
||||
type Options = Parameters<typeof validate>[2];
|
||||
export type Resolver = <T extends Struct<any, any>>(schema: T, options?: Options, factoryOptions?: {
|
||||
/**
|
||||
* Return the raw input values rather than the parsed values.
|
||||
* @default false
|
||||
*/
|
||||
raw?: boolean;
|
||||
}) => <TFieldValues extends FieldValues, TContext>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => ResolverResult<TFieldValues>;
|
||||
export {};
|
||||
18
frontend/node_modules/@hookform/resolvers/superstruct/package.json
generated
vendored
Normal file
18
frontend/node_modules/@hookform/resolvers/superstruct/package.json
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "@hookform/resolvers/superstruct",
|
||||
"amdName": "hookformResolversSuperstruct",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"description": "React Hook Form validation resolver: superstruct",
|
||||
"main": "dist/superstruct.js",
|
||||
"module": "dist/superstruct.module.js",
|
||||
"umd:main": "dist/superstruct.umd.js",
|
||||
"source": "src/index.ts",
|
||||
"types": "dist/index.d.ts",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"react-hook-form": "^7.0.0",
|
||||
"@hookform/resolvers": "^2.0.0",
|
||||
"superstruct": ">=0.12.0"
|
||||
}
|
||||
}
|
||||
82
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
82
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { Infer, object, size, string } from 'superstruct';
|
||||
import { superstructResolver } from '..';
|
||||
|
||||
const schema = object({
|
||||
username: size(string(), 2),
|
||||
password: size(string(), 6),
|
||||
});
|
||||
|
||||
type FormData = Infer<typeof schema>;
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const { register, handleSubmit } = useForm<FormData>({
|
||||
resolver: superstructResolver(schema),
|
||||
shouldUseNativeValidation: true,
|
||||
});
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<input {...register('username')} placeholder="username" />
|
||||
|
||||
<input {...register('password')} placeholder="password" />
|
||||
|
||||
<button type="submit">submit</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
test("form's native validation with Superstruct", async () => {
|
||||
const handleSubmit = vi.fn();
|
||||
render(<TestComponent onSubmit={handleSubmit} />);
|
||||
|
||||
// username
|
||||
let usernameField = screen.getByPlaceholderText(
|
||||
/username/i,
|
||||
) as HTMLInputElement;
|
||||
expect(usernameField.validity.valid).toBe(true);
|
||||
expect(usernameField.validationMessage).toBe('');
|
||||
|
||||
// password
|
||||
let passwordField = screen.getByPlaceholderText(
|
||||
/password/i,
|
||||
) as HTMLInputElement;
|
||||
expect(passwordField.validity.valid).toBe(true);
|
||||
expect(passwordField.validationMessage).toBe('');
|
||||
|
||||
await user.click(screen.getByText(/submit/i));
|
||||
|
||||
// username
|
||||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
||||
expect(usernameField.validity.valid).toBe(false);
|
||||
expect(usernameField.validationMessage).toBe(
|
||||
'Expected a string with a length of `2` but received one with a length of `0`',
|
||||
);
|
||||
|
||||
// password
|
||||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
||||
expect(passwordField.validity.valid).toBe(false);
|
||||
expect(passwordField.validationMessage).toBe(
|
||||
'Expected a string with a length of `6` but received one with a length of `0`',
|
||||
);
|
||||
|
||||
await user.type(screen.getByPlaceholderText(/username/i), 'jo');
|
||||
await user.type(screen.getByPlaceholderText(/password/i), 'passwo');
|
||||
|
||||
// username
|
||||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
||||
expect(usernameField.validity.valid).toBe(true);
|
||||
expect(usernameField.validationMessage).toBe('');
|
||||
|
||||
// password
|
||||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
||||
expect(passwordField.validity.valid).toBe(true);
|
||||
expect(passwordField.validationMessage).toBe('');
|
||||
});
|
||||
60
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/Form.tsx
generated
vendored
Normal file
60
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/Form.tsx
generated
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { Infer, object, size, string } from 'superstruct';
|
||||
import { superstructResolver } from '..';
|
||||
|
||||
const schema = object({
|
||||
username: size(string(), 2),
|
||||
password: size(string(), 6),
|
||||
});
|
||||
|
||||
type FormData = Infer<typeof schema>;
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const {
|
||||
register,
|
||||
formState: { errors },
|
||||
handleSubmit,
|
||||
} = useForm<FormData>({
|
||||
resolver: superstructResolver(schema), // Useful to check TypeScript regressions
|
||||
});
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<input {...register('username')} />
|
||||
{errors.username && <span role="alert">{errors.username.message}</span>}
|
||||
|
||||
<input {...register('password')} />
|
||||
{errors.password && <span role="alert">{errors.password.message}</span>}
|
||||
|
||||
<button type="submit">submit</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
test("form's validation with Superstruct and TypeScript's integration", async () => {
|
||||
const handleSubmit = vi.fn();
|
||||
render(<TestComponent onSubmit={handleSubmit} />);
|
||||
|
||||
expect(screen.queryAllByRole('alert')).toHaveLength(0);
|
||||
|
||||
await user.click(screen.getByText(/submit/i));
|
||||
|
||||
expect(
|
||||
screen.getByText(
|
||||
/Expected a string with a length of `2` but received one with a length of `0`/i,
|
||||
),
|
||||
).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByText(
|
||||
/Expected a string with a length of `6` but received one with a length of `0`/i,
|
||||
),
|
||||
).toBeInTheDocument();
|
||||
expect(handleSubmit).not.toHaveBeenCalled();
|
||||
});
|
||||
75
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
75
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
@ -0,0 +1,75 @@
|
||||
import { Field, InternalFieldName } from 'react-hook-form';
|
||||
import {
|
||||
Infer,
|
||||
array,
|
||||
boolean,
|
||||
define,
|
||||
max,
|
||||
min,
|
||||
number,
|
||||
object,
|
||||
optional,
|
||||
pattern,
|
||||
size,
|
||||
string,
|
||||
union,
|
||||
} from 'superstruct';
|
||||
|
||||
const Password = define(
|
||||
'Password',
|
||||
(value, ctx) => value === ctx.branch[0].password,
|
||||
);
|
||||
|
||||
export const schema = object({
|
||||
username: size(pattern(string(), /^\w+$/), 3, 30),
|
||||
password: pattern(string(), /^[a-zA-Z0-9]{3,30}/),
|
||||
repeatPassword: Password,
|
||||
accessToken: optional(union([string(), number()])),
|
||||
birthYear: optional(max(min(number(), 1900), 2013)),
|
||||
email: optional(pattern(string(), /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/)),
|
||||
tags: array(string()),
|
||||
enabled: boolean(),
|
||||
like: optional(array(object({ id: number(), name: size(string(), 4) }))),
|
||||
});
|
||||
|
||||
export const validData: Infer<typeof schema> = {
|
||||
username: 'Doe',
|
||||
password: 'Password123',
|
||||
repeatPassword: 'Password123',
|
||||
birthYear: 2000,
|
||||
email: 'john@doe.com',
|
||||
tags: ['tag1', 'tag2'],
|
||||
enabled: true,
|
||||
like: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'name',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export const invalidData = {
|
||||
password: '___',
|
||||
email: '',
|
||||
birthYear: 'birthYear',
|
||||
like: [{ id: 'z' }],
|
||||
};
|
||||
|
||||
export const fields: Record<InternalFieldName, Field['_f']> = {
|
||||
username: {
|
||||
ref: { name: 'username' },
|
||||
name: 'username',
|
||||
},
|
||||
password: {
|
||||
ref: { name: 'password' },
|
||||
name: 'password',
|
||||
},
|
||||
email: {
|
||||
ref: { name: 'email' },
|
||||
name: 'email',
|
||||
},
|
||||
birthday: {
|
||||
ref: { name: 'birthday' },
|
||||
name: 'birthday',
|
||||
},
|
||||
};
|
||||
64
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap
generated
vendored
Normal file
64
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap
generated
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`superstructResolver > should return a single error from superstructResolver when validation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"birthYear": {
|
||||
"message": "Expected a number, but received: "birthYear"",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
},
|
||||
"email": {
|
||||
"message": "Expected a string matching \`/^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$/\` but received """,
|
||||
"ref": {
|
||||
"name": "email",
|
||||
},
|
||||
"type": "string",
|
||||
},
|
||||
"enabled": {
|
||||
"message": "Expected a value of type \`boolean\`, but received: \`undefined\`",
|
||||
"ref": undefined,
|
||||
"type": "boolean",
|
||||
},
|
||||
"like": [
|
||||
{
|
||||
"id": {
|
||||
"message": "Expected a number, but received: "z"",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
},
|
||||
"name": {
|
||||
"message": "Expected a string, but received: undefined",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
],
|
||||
"password": {
|
||||
"message": "Expected a string matching \`/^[a-zA-Z0-9]{3,30}/\` but received "___"",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "string",
|
||||
},
|
||||
"repeatPassword": {
|
||||
"message": "Expected a value of type \`Password\`, but received: \`undefined\`",
|
||||
"ref": undefined,
|
||||
"type": "Password",
|
||||
},
|
||||
"tags": {
|
||||
"message": "Expected an array value, but received: undefined",
|
||||
"ref": undefined,
|
||||
"type": "array",
|
||||
},
|
||||
"username": {
|
||||
"message": "Expected a string, but received: undefined",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
37
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/superstruct.ts
generated
vendored
Normal file
37
frontend/node_modules/@hookform/resolvers/superstruct/src/__tests__/superstruct.ts
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
import { superstructResolver } from '..';
|
||||
import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
||||
|
||||
const shouldUseNativeValidation = false;
|
||||
|
||||
describe('superstructResolver', () => {
|
||||
it('should return values from superstructResolver when validation pass', async () => {
|
||||
const result = await superstructResolver(schema)(validData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ errors: {}, values: validData });
|
||||
});
|
||||
|
||||
it('should return a single error from superstructResolver when validation fails', async () => {
|
||||
const result = await superstructResolver(schema)(invalidData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return values from superstructResolver when validation pass & raw=true', async () => {
|
||||
const result = await superstructResolver(schema, undefined, { raw: true })(
|
||||
validData,
|
||||
undefined,
|
||||
{
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual({ errors: {}, values: validData });
|
||||
});
|
||||
});
|
||||
2
frontend/node_modules/@hookform/resolvers/superstruct/src/index.ts
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/superstruct/src/index.ts
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './superstruct';
|
||||
export * from './types';
|
||||
35
frontend/node_modules/@hookform/resolvers/superstruct/src/superstruct.ts
generated
vendored
Normal file
35
frontend/node_modules/@hookform/resolvers/superstruct/src/superstruct.ts
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
||||
import { FieldError } from 'react-hook-form';
|
||||
|
||||
import { StructError, validate } from 'superstruct';
|
||||
import { Resolver } from './types';
|
||||
|
||||
const parseErrorSchema = (error: StructError) =>
|
||||
error.failures().reduce<Record<string, FieldError>>(
|
||||
(previous, error) =>
|
||||
(previous[error.path.join('.')] = {
|
||||
message: error.message,
|
||||
type: error.type,
|
||||
}) && previous,
|
||||
{},
|
||||
);
|
||||
|
||||
export const superstructResolver: Resolver =
|
||||
(schema, schemaOptions, resolverOptions = {}) =>
|
||||
(values, _, options) => {
|
||||
const result = validate(values, schema, schemaOptions);
|
||||
|
||||
if (result[0]) {
|
||||
return {
|
||||
values: {},
|
||||
errors: toNestErrors(parseErrorSchema(result[0]), options),
|
||||
};
|
||||
}
|
||||
|
||||
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
||||
|
||||
return {
|
||||
values: resolverOptions.raw ? values : result[1],
|
||||
errors: {},
|
||||
};
|
||||
};
|
||||
20
frontend/node_modules/@hookform/resolvers/superstruct/src/types.ts
generated
vendored
Normal file
20
frontend/node_modules/@hookform/resolvers/superstruct/src/types.ts
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
||||
import { Struct, validate } from 'superstruct';
|
||||
|
||||
type Options = Parameters<typeof validate>[2];
|
||||
|
||||
export type Resolver = <T extends Struct<any, any>>(
|
||||
schema: T,
|
||||
options?: Options,
|
||||
factoryOptions?: {
|
||||
/**
|
||||
* Return the raw input values rather than the parsed values.
|
||||
* @default false
|
||||
*/
|
||||
raw?: boolean;
|
||||
},
|
||||
) => <TFieldValues extends FieldValues, TContext>(
|
||||
values: TFieldValues,
|
||||
context: TContext | undefined,
|
||||
options: ResolverOptions<TFieldValues>,
|
||||
) => ResolverResult<TFieldValues>;
|
||||
Reference in New Issue
Block a user