v1.0 with SW PWA enabled
This commit is contained in:
2
frontend/node_modules/@hookform/resolvers/nope/dist/index.d.ts
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/nope/dist/index.d.ts
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './nope';
|
||||
export * from './types';
|
||||
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.d.ts
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.d.ts
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import type { Resolver } from './types';
|
||||
export declare const nopeResolver: Resolver;
|
||||
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.js
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.js
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var r=require("@hookform/resolvers"),e=function r(e,o,t){return void 0===o&&(o={}),void 0===t&&(t=""),Object.keys(e).reduce(function(o,s){var a=t?t+"."+s:s,i=e[s];return"string"==typeof i?o[a]={message:i}:r(i,o,a),o},o)};exports.nopeResolver=function(o,t){return void 0===t&&(t={abortEarly:!1}),function(s,a,i){var n=o.validate(s,a,t);return n?{values:{},errors:r.toNestErrors(e(n),i)}:(i.shouldUseNativeValidation&&r.validateFieldsNatively({},i),{values:s,errors:{}})}};
|
||||
//# sourceMappingURL=nope.js.map
|
||||
1
frontend/node_modules/@hookform/resolvers/nope/dist/nope.js.map
generated
vendored
Normal file
1
frontend/node_modules/@hookform/resolvers/nope/dist/nope.js.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"nope.js","sources":["../src/nope.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ShapeErrors } from 'nope-validator/lib/cjs/types';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (\n errors: ShapeErrors,\n parsedErrors: FieldErrors = {},\n path = '',\n) => {\n return Object.keys(errors).reduce((acc, key) => {\n const _path = path ? `${path}.${key}` : key;\n const error = errors[key];\n\n if (typeof error === 'string') {\n acc[_path] = {\n message: error,\n } as FieldError;\n } else {\n parseErrors(error, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n};\n\nexport const nopeResolver: Resolver =\n (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n ) =>\n (values, context, options) => {\n const result = schema.validate(values, context, schemaOptions) as\n | ShapeErrors\n | undefined;\n\n if (result) {\n return { values: {}, errors: toNestErrors(parseErrors(result), options) };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrors","errors","parsedErrors","path","Object","keys","reduce","acc","key","_path","error","message","schema","schemaOptions","abortEarly","values","context","options","result","validate","toNestErrors","shouldUseNativeValidation","validateFieldsNatively"],"mappings":"qCAKMA,EAAc,SAAdA,EACJC,EACAC,EACAC,GAEA,YAHAD,IAAAA,IAAAA,EAA4B,CAAE,QAC1B,IAAJC,IAAAA,EAAO,IAEAC,OAAOC,KAAKJ,GAAQK,OAAO,SAACC,EAAKC,GACtC,IAAMC,EAAQN,EAAUA,EAAI,IAAIK,EAAQA,EAClCE,EAAQT,EAAOO,GAUrB,MARqB,iBAAVE,EACTH,EAAIE,GAAS,CACXE,QAASD,GAGXV,EAAYU,EAAOH,EAAKE,GAGnBF,CACT,EAAGL,EACL,uBAGE,SACEU,EACAC,GAEC,YAFY,IAAbA,IAAAA,EAAgB,CACdC,YAAY,aAGfC,EAAQC,EAASC,GAChB,IAAMC,EAASN,EAAOO,SAASJ,EAAQC,EAASH,GAIhD,OAAIK,EACK,CAAEH,OAAQ,CAAE,EAAEd,OAAQmB,EAAAA,aAAapB,EAAYkB,GAASD,KAGjEA,EAAQI,2BAA6BC,EAAsBA,uBAAC,CAAE,EAAEL,GAEzD,CAAEF,OAAAA,EAAQd,OAAQ,IAC3B,CAAC"}
|
||||
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.mjs
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.mjs
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";var o=function r(e,o,t){return void 0===o&&(o={}),void 0===t&&(t=""),Object.keys(e).reduce(function(o,n){var a=t?t+"."+n:n,i=e[n];return"string"==typeof i?o[a]={message:i}:r(i,o,a),o},o)},t=function(t,n){return void 0===n&&(n={abortEarly:!1}),function(a,i,s){var u=t.validate(a,i,n);return u?{values:{},errors:r(o(u),s)}:(s.shouldUseNativeValidation&&e({},s),{values:a,errors:{}})}};export{t as nopeResolver};
|
||||
//# sourceMappingURL=nope.module.js.map
|
||||
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.modern.mjs
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.modern.mjs
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";const o=(r,e={},s="")=>Object.keys(r).reduce((e,t)=>{const a=s?`${s}.${t}`:t,l=r[t];return"string"==typeof l?e[a]={message:l}:o(l,e,a),e},e),s=(s,t={abortEarly:!1})=>(a,l,n)=>{const i=s.validate(a,l,t);return i?{values:{},errors:r(o(i),n)}:(n.shouldUseNativeValidation&&e({},n),{values:a,errors:{}})};export{s as nopeResolver};
|
||||
//# sourceMappingURL=nope.modern.mjs.map
|
||||
1
frontend/node_modules/@hookform/resolvers/nope/dist/nope.modern.mjs.map
generated
vendored
Normal file
1
frontend/node_modules/@hookform/resolvers/nope/dist/nope.modern.mjs.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"nope.modern.mjs","sources":["../src/nope.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ShapeErrors } from 'nope-validator/lib/cjs/types';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (\n errors: ShapeErrors,\n parsedErrors: FieldErrors = {},\n path = '',\n) => {\n return Object.keys(errors).reduce((acc, key) => {\n const _path = path ? `${path}.${key}` : key;\n const error = errors[key];\n\n if (typeof error === 'string') {\n acc[_path] = {\n message: error,\n } as FieldError;\n } else {\n parseErrors(error, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n};\n\nexport const nopeResolver: Resolver =\n (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n ) =>\n (values, context, options) => {\n const result = schema.validate(values, context, schemaOptions) as\n | ShapeErrors\n | undefined;\n\n if (result) {\n return { values: {}, errors: toNestErrors(parseErrors(result), options) };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrors","errors","parsedErrors","path","Object","keys","reduce","acc","key","_path","error","message","nopeResolver","schema","schemaOptions","abortEarly","values","context","options","result","validate","toNestErrors","shouldUseNativeValidation","validateFieldsNatively"],"mappings":"+EAKA,MAAMA,EAAcA,CAClBC,EACAC,EAA4B,CAAA,EAC5BC,EAAO,KAEAC,OAAOC,KAAKJ,GAAQK,OAAO,CAACC,EAAKC,KACtC,MAAMC,EAAQN,EAAO,GAAGA,KAAQK,IAAQA,EAClCE,EAAQT,EAAOO,GAUrB,MARqB,iBAAVE,EACTH,EAAIE,GAAS,CACXE,QAASD,GAGXV,EAAYU,EAAOH,EAAKE,GAGnBF,GACNL,GAGQU,EACXA,CACEC,EACAC,EAAgB,CACdC,YAAY,KAGhB,CAACC,EAAQC,EAASC,KAChB,MAAMC,EAASN,EAAOO,SAASJ,EAAQC,EAASH,GAIhD,OAAIK,EACK,CAAEH,OAAQ,CAAE,EAAEf,OAAQoB,EAAarB,EAAYmB,GAASD,KAGjEA,EAAQI,2BAA6BC,EAAuB,GAAIL,GAEzD,CAAEF,SAAQf,OAAQ"}
|
||||
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.module.js
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.module.js
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";var o=function r(e,o,t){return void 0===o&&(o={}),void 0===t&&(t=""),Object.keys(e).reduce(function(o,n){var a=t?t+"."+n:n,i=e[n];return"string"==typeof i?o[a]={message:i}:r(i,o,a),o},o)},t=function(t,n){return void 0===n&&(n={abortEarly:!1}),function(a,i,s){var u=t.validate(a,i,n);return u?{values:{},errors:r(o(u),s)}:(s.shouldUseNativeValidation&&e({},s),{values:a,errors:{}})}};export{t as nopeResolver};
|
||||
//# sourceMappingURL=nope.module.js.map
|
||||
1
frontend/node_modules/@hookform/resolvers/nope/dist/nope.module.js.map
generated
vendored
Normal file
1
frontend/node_modules/@hookform/resolvers/nope/dist/nope.module.js.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"nope.module.js","sources":["../src/nope.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ShapeErrors } from 'nope-validator/lib/cjs/types';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (\n errors: ShapeErrors,\n parsedErrors: FieldErrors = {},\n path = '',\n) => {\n return Object.keys(errors).reduce((acc, key) => {\n const _path = path ? `${path}.${key}` : key;\n const error = errors[key];\n\n if (typeof error === 'string') {\n acc[_path] = {\n message: error,\n } as FieldError;\n } else {\n parseErrors(error, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n};\n\nexport const nopeResolver: Resolver =\n (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n ) =>\n (values, context, options) => {\n const result = schema.validate(values, context, schemaOptions) as\n | ShapeErrors\n | undefined;\n\n if (result) {\n return { values: {}, errors: toNestErrors(parseErrors(result), options) };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrors","errors","parsedErrors","path","Object","keys","reduce","acc","key","_path","error","message","nopeResolver","schema","schemaOptions","abortEarly","values","context","options","result","validate","toNestErrors","shouldUseNativeValidation","validateFieldsNatively"],"mappings":"+EAKA,IAAMA,EAAc,SAAdA,EACJC,EACAC,EACAC,GAEA,YAHAD,IAAAA,IAAAA,EAA4B,CAAE,QAC1B,IAAJC,IAAAA,EAAO,IAEAC,OAAOC,KAAKJ,GAAQK,OAAO,SAACC,EAAKC,GACtC,IAAMC,EAAQN,EAAUA,EAAI,IAAIK,EAAQA,EAClCE,EAAQT,EAAOO,GAUrB,MARqB,iBAAVE,EACTH,EAAIE,GAAS,CACXE,QAASD,GAGXV,EAAYU,EAAOH,EAAKE,GAGnBF,CACT,EAAGL,EACL,EAEaU,EACX,SACEC,EACAC,GAEC,YAFY,IAAbA,IAAAA,EAAgB,CACdC,YAAY,aAGfC,EAAQC,EAASC,GAChB,IAAMC,EAASN,EAAOO,SAASJ,EAAQC,EAASH,GAIhD,OAAIK,EACK,CAAEH,OAAQ,CAAE,EAAEf,OAAQoB,EAAarB,EAAYmB,GAASD,KAGjEA,EAAQI,2BAA6BC,EAAuB,CAAE,EAAEL,GAEzD,CAAEF,OAAAA,EAAQf,OAAQ,IAC3B,CAAC"}
|
||||
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.umd.js
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/nope/dist/nope.umd.js
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],o):o((e||self).hookformResolversNope={},e.hookformResolvers)}(this,function(e,o){var r=function e(o,r,t){return void 0===r&&(r={}),void 0===t&&(t=""),Object.keys(o).reduce(function(r,s){var n=t?t+"."+s:s,i=o[s];return"string"==typeof i?r[n]={message:i}:e(i,r,n),r},r)};e.nopeResolver=function(e,t){return void 0===t&&(t={abortEarly:!1}),function(s,n,i){var f=e.validate(s,n,t);return f?{values:{},errors:o.toNestErrors(r(f),i)}:(i.shouldUseNativeValidation&&o.validateFieldsNatively({},i),{values:s,errors:{}})}}});
|
||||
//# sourceMappingURL=nope.umd.js.map
|
||||
1
frontend/node_modules/@hookform/resolvers/nope/dist/nope.umd.js.map
generated
vendored
Normal file
1
frontend/node_modules/@hookform/resolvers/nope/dist/nope.umd.js.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"nope.umd.js","sources":["../src/nope.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ShapeErrors } from 'nope-validator/lib/cjs/types';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (\n errors: ShapeErrors,\n parsedErrors: FieldErrors = {},\n path = '',\n) => {\n return Object.keys(errors).reduce((acc, key) => {\n const _path = path ? `${path}.${key}` : key;\n const error = errors[key];\n\n if (typeof error === 'string') {\n acc[_path] = {\n message: error,\n } as FieldError;\n } else {\n parseErrors(error, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n};\n\nexport const nopeResolver: Resolver =\n (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n ) =>\n (values, context, options) => {\n const result = schema.validate(values, context, schemaOptions) as\n | ShapeErrors\n | undefined;\n\n if (result) {\n return { values: {}, errors: toNestErrors(parseErrors(result), options) };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrors","errors","parsedErrors","path","Object","keys","reduce","acc","key","_path","error","message","schema","schemaOptions","abortEarly","values","context","options","result","validate","toNestErrors","shouldUseNativeValidation","validateFieldsNatively"],"mappings":"0TAKA,IAAMA,EAAc,SAAdA,EACJC,EACAC,EACAC,GAEA,YAHAD,IAAAA,IAAAA,EAA4B,CAAE,QAC1B,IAAJC,IAAAA,EAAO,IAEAC,OAAOC,KAAKJ,GAAQK,OAAO,SAACC,EAAKC,GACtC,IAAMC,EAAQN,EAAUA,EAAI,IAAIK,EAAQA,EAClCE,EAAQT,EAAOO,GAUrB,MARqB,iBAAVE,EACTH,EAAIE,GAAS,CACXE,QAASD,GAGXV,EAAYU,EAAOH,EAAKE,GAGnBF,CACT,EAAGL,EACL,iBAGE,SACEU,EACAC,GAEC,YAFY,IAAbA,IAAAA,EAAgB,CACdC,YAAY,aAGfC,EAAQC,EAASC,GAChB,IAAMC,EAASN,EAAOO,SAASJ,EAAQC,EAASH,GAIhD,OAAIK,EACK,CAAEH,OAAQ,CAAE,EAAEd,OAAQmB,EAAAA,aAAapB,EAAYkB,GAASD,KAGjEA,EAAQI,2BAA6BC,EAAsBA,uBAAC,CAAE,EAAEL,GAEzD,CAAEF,OAAAA,EAAQd,OAAQ,IAC3B,CAAC"}
|
||||
9
frontend/node_modules/@hookform/resolvers/nope/dist/types.d.ts
generated
vendored
Normal file
9
frontend/node_modules/@hookform/resolvers/nope/dist/types.d.ts
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
import type { NopeObject } from 'nope-validator/lib/cjs/NopeObject';
|
||||
import type { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
||||
type ValidateOptions = Parameters<NopeObject['validate']>[2];
|
||||
type Context = Parameters<NopeObject['validate']>[1];
|
||||
export type Resolver = <T extends NopeObject>(schema: T, schemaOptions?: ValidateOptions, resolverOptions?: {
|
||||
mode?: 'async' | 'sync';
|
||||
rawValues?: boolean;
|
||||
}) => <TFieldValues extends FieldValues, TContext extends Context>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => ResolverResult<TFieldValues>;
|
||||
export {};
|
||||
18
frontend/node_modules/@hookform/resolvers/nope/package.json
generated
vendored
Normal file
18
frontend/node_modules/@hookform/resolvers/nope/package.json
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "@hookform/resolvers/nope",
|
||||
"amdName": "hookformResolversNope",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"description": "React Hook Form validation resolver: nope",
|
||||
"main": "dist/nope.js",
|
||||
"module": "dist/nope.module.js",
|
||||
"umd:main": "dist/nope.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",
|
||||
"nope-validator": "^0.12.0"
|
||||
}
|
||||
}
|
||||
85
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
85
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import Nope from 'nope-validator';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { nopeResolver } from '..';
|
||||
|
||||
const USERNAME_REQUIRED_MESSAGE = 'username field is required';
|
||||
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';
|
||||
|
||||
const schema = Nope.object().shape({
|
||||
username: Nope.string().required(USERNAME_REQUIRED_MESSAGE),
|
||||
password: Nope.string().required(PASSWORD_REQUIRED_MESSAGE),
|
||||
});
|
||||
|
||||
interface FormData {
|
||||
unusedProperty: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const { register, handleSubmit } = useForm<FormData>({
|
||||
resolver: nopeResolver(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 Nope", 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(USERNAME_REQUIRED_MESSAGE);
|
||||
|
||||
// password
|
||||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
||||
expect(passwordField.validity.valid).toBe(false);
|
||||
expect(passwordField.validationMessage).toBe(PASSWORD_REQUIRED_MESSAGE);
|
||||
|
||||
await user.type(screen.getByPlaceholderText(/username/i), 'joe');
|
||||
await user.type(screen.getByPlaceholderText(/password/i), 'password');
|
||||
|
||||
// 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('');
|
||||
});
|
||||
55
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/Form.tsx
generated
vendored
Normal file
55
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/Form.tsx
generated
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import Nope from 'nope-validator';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { nopeResolver } from '..';
|
||||
|
||||
const schema = Nope.object().shape({
|
||||
username: Nope.string().required(),
|
||||
password: Nope.string().required(),
|
||||
});
|
||||
|
||||
interface FormData {
|
||||
unusedProperty: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const {
|
||||
register,
|
||||
formState: { errors },
|
||||
handleSubmit,
|
||||
} = useForm<FormData>({
|
||||
resolver: nopeResolver(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 Yup 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.getAllByText(/This field is required/i)).toHaveLength(2);
|
||||
expect(handleSubmit).not.toHaveBeenCalled();
|
||||
});
|
||||
70
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
70
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
import Nope from 'nope-validator';
|
||||
import { Field, InternalFieldName } from 'react-hook-form';
|
||||
|
||||
export const schema = Nope.object().shape({
|
||||
username: Nope.string().regex(/^\w+$/).min(2).max(30).required(),
|
||||
password: Nope.string()
|
||||
.regex(new RegExp('.*[A-Z].*'), 'One uppercase character')
|
||||
.regex(new RegExp('.*[a-z].*'), 'One lowercase character')
|
||||
.regex(new RegExp('.*\\d.*'), 'One number')
|
||||
.regex(
|
||||
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
|
||||
'One special character',
|
||||
)
|
||||
.min(8, 'Must be at least 8 characters in length')
|
||||
.required('New Password is required'),
|
||||
repeatPassword: Nope.string()
|
||||
.oneOf([Nope.ref('password')], "Passwords don't match")
|
||||
.required(),
|
||||
accessToken: Nope.string(),
|
||||
birthYear: Nope.number().min(1900).max(2013),
|
||||
email: Nope.string().email(),
|
||||
tags: Nope.array().of(Nope.string()).required(),
|
||||
enabled: Nope.boolean(),
|
||||
like: Nope.object().shape({
|
||||
id: Nope.number().required(),
|
||||
name: Nope.string().atLeast(4).required(),
|
||||
}),
|
||||
});
|
||||
|
||||
export const validData = {
|
||||
username: 'Doe',
|
||||
password: 'Password123_',
|
||||
repeatPassword: 'Password123_',
|
||||
birthYear: 2000,
|
||||
email: 'john@doe.com',
|
||||
tags: ['tag1', 'tag2'],
|
||||
enabled: true,
|
||||
accessToken: 'accessToken',
|
||||
like: {
|
||||
id: 1,
|
||||
name: 'name',
|
||||
},
|
||||
};
|
||||
|
||||
export const invalidData = {
|
||||
password: '___',
|
||||
email: '',
|
||||
birthYear: 'birthYear',
|
||||
like: { id: 'z' },
|
||||
tags: [1, 2, 3],
|
||||
};
|
||||
|
||||
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',
|
||||
},
|
||||
};
|
||||
43
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/__snapshots__/nope.ts.snap
generated
vendored
Normal file
43
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/__snapshots__/nope.ts.snap
generated
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`nopeResolver > should return a single error from nopeResolver when validation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"birthYear": {
|
||||
"message": "The field is not a valid number",
|
||||
"ref": undefined,
|
||||
},
|
||||
"like": {
|
||||
"id": {
|
||||
"message": "The field is not a valid number",
|
||||
"ref": undefined,
|
||||
},
|
||||
"name": {
|
||||
"message": "This field is required",
|
||||
"ref": undefined,
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
},
|
||||
"repeatPassword": {
|
||||
"message": "This field is required",
|
||||
"ref": undefined,
|
||||
},
|
||||
"tags": {
|
||||
"message": "One or more elements are of invalid type",
|
||||
"ref": undefined,
|
||||
},
|
||||
"username": {
|
||||
"message": "This field is required",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
28
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/nope.ts
generated
vendored
Normal file
28
frontend/node_modules/@hookform/resolvers/nope/src/__tests__/nope.ts
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
|
||||
import { nopeResolver } from '..';
|
||||
import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
||||
|
||||
const shouldUseNativeValidation = false;
|
||||
|
||||
describe('nopeResolver', () => {
|
||||
it('should return values from nopeResolver when validation pass', async () => {
|
||||
const schemaSpy = vi.spyOn(schema, 'validate');
|
||||
|
||||
const result = await nopeResolver(schema)(validData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(schemaSpy).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual({ errors: {}, values: validData });
|
||||
});
|
||||
|
||||
it('should return a single error from nopeResolver when validation fails', async () => {
|
||||
const result = await nopeResolver(schema)(invalidData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
2
frontend/node_modules/@hookform/resolvers/nope/src/index.ts
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/nope/src/index.ts
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './nope';
|
||||
export * from './types';
|
||||
46
frontend/node_modules/@hookform/resolvers/nope/src/nope.ts
generated
vendored
Normal file
46
frontend/node_modules/@hookform/resolvers/nope/src/nope.ts
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
||||
import type { ShapeErrors } from 'nope-validator/lib/cjs/types';
|
||||
import type { FieldError, FieldErrors } from 'react-hook-form';
|
||||
import type { Resolver } from './types';
|
||||
|
||||
const parseErrors = (
|
||||
errors: ShapeErrors,
|
||||
parsedErrors: FieldErrors = {},
|
||||
path = '',
|
||||
) => {
|
||||
return Object.keys(errors).reduce((acc, key) => {
|
||||
const _path = path ? `${path}.${key}` : key;
|
||||
const error = errors[key];
|
||||
|
||||
if (typeof error === 'string') {
|
||||
acc[_path] = {
|
||||
message: error,
|
||||
} as FieldError;
|
||||
} else {
|
||||
parseErrors(error, acc, _path);
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, parsedErrors);
|
||||
};
|
||||
|
||||
export const nopeResolver: Resolver =
|
||||
(
|
||||
schema,
|
||||
schemaOptions = {
|
||||
abortEarly: false,
|
||||
},
|
||||
) =>
|
||||
(values, context, options) => {
|
||||
const result = schema.validate(values, context, schemaOptions) as
|
||||
| ShapeErrors
|
||||
| undefined;
|
||||
|
||||
if (result) {
|
||||
return { values: {}, errors: toNestErrors(parseErrors(result), options) };
|
||||
}
|
||||
|
||||
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
||||
|
||||
return { values, errors: {} };
|
||||
};
|
||||
19
frontend/node_modules/@hookform/resolvers/nope/src/types.ts
generated
vendored
Normal file
19
frontend/node_modules/@hookform/resolvers/nope/src/types.ts
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
import type { NopeObject } from 'nope-validator/lib/cjs/NopeObject';
|
||||
import type {
|
||||
FieldValues,
|
||||
ResolverOptions,
|
||||
ResolverResult,
|
||||
} from 'react-hook-form';
|
||||
|
||||
type ValidateOptions = Parameters<NopeObject['validate']>[2];
|
||||
type Context = Parameters<NopeObject['validate']>[1];
|
||||
|
||||
export type Resolver = <T extends NopeObject>(
|
||||
schema: T,
|
||||
schemaOptions?: ValidateOptions,
|
||||
resolverOptions?: { mode?: 'async' | 'sync'; rawValues?: boolean },
|
||||
) => <TFieldValues extends FieldValues, TContext extends Context>(
|
||||
values: TFieldValues,
|
||||
context: TContext | undefined,
|
||||
options: ResolverOptions<TFieldValues>,
|
||||
) => ResolverResult<TFieldValues>;
|
||||
Reference in New Issue
Block a user