build revisions and layout updates for toast
This commit is contained in:
parent
b341a3675e
commit
39235193e6
1116 changed files with 130517 additions and 12 deletions
29
node_modules/@hookform/resolvers/class-validator/dist/class-validator.d.ts
generated
vendored
Normal file
29
node_modules/@hookform/resolvers/class-validator/dist/class-validator.d.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { ClassConstructor, ClassTransformOptions } from 'class-transformer';
|
||||
import { ValidatorOptions } from 'class-validator';
|
||||
import { Resolver } from 'react-hook-form';
|
||||
/**
|
||||
* Creates a resolver for react-hook-form using class-validator schema validation
|
||||
* @param {ClassConstructor<Schema>} schema - The class-validator schema to validate against
|
||||
* @param {Object} schemaOptions - Additional schema validation options
|
||||
* @param {Object} resolverOptions - Additional resolver configuration
|
||||
* @param {string} [resolverOptions.mode='async'] - Validation mode
|
||||
* @returns {Resolver<Schema>} A resolver function compatible with react-hook-form
|
||||
* @example
|
||||
* class Schema {
|
||||
* @Matches(/^\w+$/)
|
||||
* @Length(3, 30)
|
||||
* username: string;
|
||||
* age: number
|
||||
* }
|
||||
*
|
||||
* useForm({
|
||||
* resolver: classValidatorResolver(Schema)
|
||||
* });
|
||||
*/
|
||||
export declare function classValidatorResolver<Schema extends Record<string, any>>(schema: ClassConstructor<Schema>, schemaOptions?: {
|
||||
validator?: ValidatorOptions;
|
||||
transformer?: ClassTransformOptions;
|
||||
}, resolverOptions?: {
|
||||
mode?: 'async' | 'sync';
|
||||
raw?: boolean;
|
||||
}): Resolver<Schema>;
|
||||
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.js
generated
vendored
Normal file
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.js
generated
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var r=require("@hookform/resolvers"),e=require("class-transformer"),t=require("class-validator");function s(r,e,t,a){return void 0===t&&(t={}),void 0===a&&(a=""),r.reduce(function(r,t){var i=a?a+"."+t.property:t.property;if(t.constraints){var o=Object.keys(t.constraints)[0];r[i]={type:o,message:t.constraints[o]};var n=r[i];e&&n&&Object.assign(n,{types:t.constraints})}return t.children&&t.children.length&&s(t.children,e,r,i),r},t)}exports.classValidatorResolver=function(a,i,o){return void 0===i&&(i={}),void 0===o&&(o={}),function(n,l,c){try{var v=i.validator,d=e.plainToClass(a,n,i.transformer);return Promise.resolve(("sync"===o.mode?t.validateSync:t.validate)(d,v)).then(function(e){return e.length?{values:{},errors:r.toNestErrors(s(e,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)}:(c.shouldUseNativeValidation&&r.validateFieldsNatively({},c),{values:o.raw?Object.assign({},n):d,errors:{}})})}catch(r){return Promise.reject(r)}}};
|
||||
//# sourceMappingURL=class-validator.js.map
|
||||
1
node_modules/@hookform/resolvers/class-validator/dist/class-validator.js.map
generated
vendored
Normal file
1
node_modules/@hookform/resolvers/class-validator/dist/class-validator.js.map
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"class-validator.js","sources":["../src/class-validator.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n ClassConstructor,\n ClassTransformOptions,\n plainToClass,\n} from 'class-transformer';\nimport {\n ValidationError,\n ValidatorOptions,\n validate,\n validateSync,\n} from 'class-validator';\nimport { FieldErrors, Resolver } from 'react-hook-form';\n\nfunction parseErrorSchema(\n errors: ValidationError[],\n validateAllFieldCriteria: boolean,\n parsedErrors: FieldErrors = {},\n path = '',\n) {\n return errors.reduce((acc, error) => {\n const _path = path ? `${path}.${error.property}` : error.property;\n\n if (error.constraints) {\n const key = Object.keys(error.constraints)[0];\n acc[_path] = {\n type: key,\n message: error.constraints[key],\n };\n\n const _e = acc[_path];\n if (validateAllFieldCriteria && _e) {\n Object.assign(_e, { types: error.constraints });\n }\n }\n\n if (error.children && error.children.length) {\n parseErrorSchema(error.children, validateAllFieldCriteria, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n}\n\n/**\n * Creates a resolver for react-hook-form using class-validator schema validation\n * @param {ClassConstructor<Schema>} schema - The class-validator schema to validate against\n * @param {Object} schemaOptions - Additional schema validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver<Schema>} A resolver function compatible with react-hook-form\n * @example\n * class Schema {\n * @Matches(/^\\w+$/)\n * @Length(3, 30)\n * username: string;\n * age: number\n * }\n *\n * useForm({\n * resolver: classValidatorResolver(Schema)\n * });\n */\nexport function classValidatorResolver<Schema extends Record<string, any>>(\n schema: ClassConstructor<Schema>,\n schemaOptions: {\n validator?: ValidatorOptions;\n transformer?: ClassTransformOptions;\n } = {},\n resolverOptions: { mode?: 'async' | 'sync'; raw?: boolean } = {},\n): Resolver<Schema> {\n return async (values, _, options) => {\n const { transformer, validator } = schemaOptions;\n const data = plainToClass(schema, values, transformer);\n\n const rawErrors = await (resolverOptions.mode === 'sync'\n ? validateSync\n : validate)(data, validator);\n\n if (rawErrors.length) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n rawErrors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : data,\n errors: {},\n };\n };\n}\n"],"names":["parseErrorSchema","errors","validateAllFieldCriteria","parsedErrors","path","reduce","acc","error","_path","property","constraints","key","Object","keys","type","message","_e","assign","types","children","length","schema","schemaOptions","resolverOptions","values","_","options","validator","data","plainToClass","transformer","Promise","resolve","mode","validateSync","validate","then","rawErrors","toNestErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","raw","e","reject"],"mappings":"iGAcA,SAASA,EACPC,EACAC,EACAC,EACAC,GAEA,YAHA,IAAAD,IAAAA,EAA4B,CAAE,YAC9BC,IAAAA,EAAO,IAEAH,EAAOI,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAQJ,EAAUA,EAAI,IAAIG,EAAME,SAAaF,EAAME,SAEzD,GAAIF,EAAMG,YAAa,CACrB,IAAMC,EAAMC,OAAOC,KAAKN,EAAMG,aAAa,GAC3CJ,EAAIE,GAAS,CACXM,KAAMH,EACNI,QAASR,EAAMG,YAAYC,IAG7B,IAAMK,EAAKV,EAAIE,GACXN,GAA4Bc,GAC9BJ,OAAOK,OAAOD,EAAI,CAAEE,MAAOX,EAAMG,aAErC,CAMA,OAJIH,EAAMY,UAAYZ,EAAMY,SAASC,QACnCpB,EAAiBO,EAAMY,SAAUjB,EAA0BI,EAAKE,GAG3DF,CACT,EAAGH,EACL,gCAqBM,SACJkB,EACAC,EAIAC,GAEA,gBANAD,IAAAA,EAGI,CAAE,QACN,IAAAC,IAAAA,EAA8D,CAAE,GAEhE,SAAcC,EAAQC,EAAGC,OACvB,IAAqBC,EAAcL,EAAdK,UACfC,EAAOC,EAAYA,aAACR,EAAQG,EADCF,EAA3BQ,aAC+C,OAAAC,QAAAC,SAEL,SAAzBT,EAAgBU,KACrCC,eACAC,EAAAA,UAAUP,EAAMD,IAAUS,KAFxBC,SAAAA,GAIN,OAAIA,EAAUjB,OACL,CACLI,OAAQ,CAAA,EACRvB,OAAQqC,eACNtC,EACEqC,GACCX,EAAQa,2BACkB,QAAzBb,EAAQc,cAEZd,KAKNA,EAAQa,2BAA6BE,EAAsBA,uBAAC,CAAE,EAAEf,GAEzD,CACLF,OAAQD,EAAgBmB,IAAM9B,OAAOK,OAAO,CAAE,EAAEO,GAAUI,EAC1D3B,OAAQ,CAAA,GACR,EACJ,CAAC,MAAA0C,GAAAZ,OAAAA,QAAAa,OAAAD,EACH,CAAA,CAAA"}
|
||||
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.mjs
generated
vendored
Normal file
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.mjs
generated
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{plainToClass as t}from"class-transformer";import{validateSync as o,validate as n}from"class-validator";function s(r,e,t,o){return void 0===t&&(t={}),void 0===o&&(o=""),r.reduce(function(r,t){var n=o?o+"."+t.property:t.property;if(t.constraints){var i=Object.keys(t.constraints)[0];r[n]={type:i,message:t.constraints[i]};var a=r[n];e&&a&&Object.assign(a,{types:t.constraints})}return t.children&&t.children.length&&s(t.children,e,r,n),r},t)}function i(i,a,c){return void 0===a&&(a={}),void 0===c&&(c={}),function(l,d,u){try{var v=a.validator,m=t(i,l,a.transformer);return Promise.resolve(("sync"===c.mode?o:n)(m,v)).then(function(t){return t.length?{values:{},errors:r(s(t,!u.shouldUseNativeValidation&&"all"===u.criteriaMode),u)}:(u.shouldUseNativeValidation&&e({},u),{values:c.raw?Object.assign({},l):m,errors:{}})})}catch(r){return Promise.reject(r)}}}export{i as classValidatorResolver};
|
||||
//# sourceMappingURL=class-validator.module.js.map
|
||||
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.modern.mjs
generated
vendored
Normal file
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.modern.mjs
generated
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import{toNestErrors as r,validateFieldsNatively as t}from"@hookform/resolvers";import{plainToClass as s}from"class-transformer";import{validateSync as e,validate as o}from"class-validator";function n(r,t,s={},e=""){return r.reduce((r,s)=>{const o=e?`${e}.${s.property}`:s.property;if(s.constraints){const e=Object.keys(s.constraints)[0];r[o]={type:e,message:s.constraints[e]};const n=r[o];t&&n&&Object.assign(n,{types:s.constraints})}return s.children&&s.children.length&&n(s.children,t,r,o),r},s)}function a(a,i={},c={}){return async(l,d,m)=>{const{transformer:u,validator:p}=i,f=s(a,l,u),h=await("sync"===c.mode?e:o)(f,p);return h.length?{values:{},errors:r(n(h,!m.shouldUseNativeValidation&&"all"===m.criteriaMode),m)}:(m.shouldUseNativeValidation&&t({},m),{values:c.raw?Object.assign({},l):f,errors:{}})}}export{a as classValidatorResolver};
|
||||
//# sourceMappingURL=class-validator.modern.mjs.map
|
||||
1
node_modules/@hookform/resolvers/class-validator/dist/class-validator.modern.mjs.map
generated
vendored
Normal file
1
node_modules/@hookform/resolvers/class-validator/dist/class-validator.modern.mjs.map
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"class-validator.modern.mjs","sources":["../src/class-validator.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n ClassConstructor,\n ClassTransformOptions,\n plainToClass,\n} from 'class-transformer';\nimport {\n ValidationError,\n ValidatorOptions,\n validate,\n validateSync,\n} from 'class-validator';\nimport { FieldErrors, Resolver } from 'react-hook-form';\n\nfunction parseErrorSchema(\n errors: ValidationError[],\n validateAllFieldCriteria: boolean,\n parsedErrors: FieldErrors = {},\n path = '',\n) {\n return errors.reduce((acc, error) => {\n const _path = path ? `${path}.${error.property}` : error.property;\n\n if (error.constraints) {\n const key = Object.keys(error.constraints)[0];\n acc[_path] = {\n type: key,\n message: error.constraints[key],\n };\n\n const _e = acc[_path];\n if (validateAllFieldCriteria && _e) {\n Object.assign(_e, { types: error.constraints });\n }\n }\n\n if (error.children && error.children.length) {\n parseErrorSchema(error.children, validateAllFieldCriteria, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n}\n\n/**\n * Creates a resolver for react-hook-form using class-validator schema validation\n * @param {ClassConstructor<Schema>} schema - The class-validator schema to validate against\n * @param {Object} schemaOptions - Additional schema validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver<Schema>} A resolver function compatible with react-hook-form\n * @example\n * class Schema {\n * @Matches(/^\\w+$/)\n * @Length(3, 30)\n * username: string;\n * age: number\n * }\n *\n * useForm({\n * resolver: classValidatorResolver(Schema)\n * });\n */\nexport function classValidatorResolver<Schema extends Record<string, any>>(\n schema: ClassConstructor<Schema>,\n schemaOptions: {\n validator?: ValidatorOptions;\n transformer?: ClassTransformOptions;\n } = {},\n resolverOptions: { mode?: 'async' | 'sync'; raw?: boolean } = {},\n): Resolver<Schema> {\n return async (values, _, options) => {\n const { transformer, validator } = schemaOptions;\n const data = plainToClass(schema, values, transformer);\n\n const rawErrors = await (resolverOptions.mode === 'sync'\n ? validateSync\n : validate)(data, validator);\n\n if (rawErrors.length) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n rawErrors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : data,\n errors: {},\n };\n };\n}\n"],"names":["parseErrorSchema","errors","validateAllFieldCriteria","parsedErrors","path","reduce","acc","error","_path","property","constraints","key","Object","keys","type","message","_e","assign","types","children","length","classValidatorResolver","schema","schemaOptions","resolverOptions","values","_","options","transformer","validator","data","plainToClass","rawErrors","mode","validateSync","validate","toNestErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","raw"],"mappings":"6LAcA,SAASA,EACPC,EACAC,EACAC,EAA4B,CAAA,EAC5BC,EAAO,IAEP,OAAOH,EAAOI,OAAO,CAACC,EAAKC,KACzB,MAAMC,EAAQJ,EAAO,GAAGA,KAAQG,EAAME,WAAaF,EAAME,SAEzD,GAAIF,EAAMG,YAAa,CACrB,MAAMC,EAAMC,OAAOC,KAAKN,EAAMG,aAAa,GAC3CJ,EAAIE,GAAS,CACXM,KAAMH,EACNI,QAASR,EAAMG,YAAYC,IAG7B,MAAMK,EAAKV,EAAIE,GACXN,GAA4Bc,GAC9BJ,OAAOK,OAAOD,EAAI,CAAEE,MAAOX,EAAMG,aAErC,CAMA,OAJIH,EAAMY,UAAYZ,EAAMY,SAASC,QACnCpB,EAAiBO,EAAMY,SAAUjB,EAA0BI,EAAKE,GAG3DF,GACNH,EACL,CAqBgB,SAAAkB,EACdC,EACAC,EAGI,GACJC,EAA8D,CAAA,GAE9D,OAAcC,MAAAA,EAAQC,EAAGC,KACvB,MAAMC,YAAEA,EAAWC,UAAEA,GAAcN,EAC7BO,EAAOC,EAAaT,EAAQG,EAAQG,GAEpCI,QAA4C,SAAzBR,EAAgBS,KACrCC,EACAC,GAAUL,EAAMD,GAEpB,OAAIG,EAAUZ,OACL,CACLK,OAAQ,CAAA,EACRxB,OAAQmC,EACNpC,EACEgC,GACCL,EAAQU,2BACkB,QAAzBV,EAAQW,cAEZX,KAKNA,EAAQU,2BAA6BE,EAAuB,GAAIZ,GAEzD,CACLF,OAAQD,EAAgBgB,IAAM5B,OAAOK,OAAO,CAAA,EAAIQ,GAAUK,EAC1D7B,OAAQ,CAAA,IAGd"}
|
||||
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.module.js
generated
vendored
Normal file
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.module.js
generated
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{plainToClass as t}from"class-transformer";import{validateSync as o,validate as n}from"class-validator";function s(r,e,t,o){return void 0===t&&(t={}),void 0===o&&(o=""),r.reduce(function(r,t){var n=o?o+"."+t.property:t.property;if(t.constraints){var i=Object.keys(t.constraints)[0];r[n]={type:i,message:t.constraints[i]};var a=r[n];e&&a&&Object.assign(a,{types:t.constraints})}return t.children&&t.children.length&&s(t.children,e,r,n),r},t)}function i(i,a,c){return void 0===a&&(a={}),void 0===c&&(c={}),function(l,d,u){try{var v=a.validator,m=t(i,l,a.transformer);return Promise.resolve(("sync"===c.mode?o:n)(m,v)).then(function(t){return t.length?{values:{},errors:r(s(t,!u.shouldUseNativeValidation&&"all"===u.criteriaMode),u)}:(u.shouldUseNativeValidation&&e({},u),{values:c.raw?Object.assign({},l):m,errors:{}})})}catch(r){return Promise.reject(r)}}}export{i as classValidatorResolver};
|
||||
//# sourceMappingURL=class-validator.module.js.map
|
||||
1
node_modules/@hookform/resolvers/class-validator/dist/class-validator.module.js.map
generated
vendored
Normal file
1
node_modules/@hookform/resolvers/class-validator/dist/class-validator.module.js.map
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"class-validator.module.js","sources":["../src/class-validator.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n ClassConstructor,\n ClassTransformOptions,\n plainToClass,\n} from 'class-transformer';\nimport {\n ValidationError,\n ValidatorOptions,\n validate,\n validateSync,\n} from 'class-validator';\nimport { FieldErrors, Resolver } from 'react-hook-form';\n\nfunction parseErrorSchema(\n errors: ValidationError[],\n validateAllFieldCriteria: boolean,\n parsedErrors: FieldErrors = {},\n path = '',\n) {\n return errors.reduce((acc, error) => {\n const _path = path ? `${path}.${error.property}` : error.property;\n\n if (error.constraints) {\n const key = Object.keys(error.constraints)[0];\n acc[_path] = {\n type: key,\n message: error.constraints[key],\n };\n\n const _e = acc[_path];\n if (validateAllFieldCriteria && _e) {\n Object.assign(_e, { types: error.constraints });\n }\n }\n\n if (error.children && error.children.length) {\n parseErrorSchema(error.children, validateAllFieldCriteria, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n}\n\n/**\n * Creates a resolver for react-hook-form using class-validator schema validation\n * @param {ClassConstructor<Schema>} schema - The class-validator schema to validate against\n * @param {Object} schemaOptions - Additional schema validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver<Schema>} A resolver function compatible with react-hook-form\n * @example\n * class Schema {\n * @Matches(/^\\w+$/)\n * @Length(3, 30)\n * username: string;\n * age: number\n * }\n *\n * useForm({\n * resolver: classValidatorResolver(Schema)\n * });\n */\nexport function classValidatorResolver<Schema extends Record<string, any>>(\n schema: ClassConstructor<Schema>,\n schemaOptions: {\n validator?: ValidatorOptions;\n transformer?: ClassTransformOptions;\n } = {},\n resolverOptions: { mode?: 'async' | 'sync'; raw?: boolean } = {},\n): Resolver<Schema> {\n return async (values, _, options) => {\n const { transformer, validator } = schemaOptions;\n const data = plainToClass(schema, values, transformer);\n\n const rawErrors = await (resolverOptions.mode === 'sync'\n ? validateSync\n : validate)(data, validator);\n\n if (rawErrors.length) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n rawErrors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : data,\n errors: {},\n };\n };\n}\n"],"names":["parseErrorSchema","errors","validateAllFieldCriteria","parsedErrors","path","reduce","acc","error","_path","property","constraints","key","Object","keys","type","message","_e","assign","types","children","length","classValidatorResolver","schema","schemaOptions","resolverOptions","values","_","options","validator","data","plainToClass","transformer","Promise","resolve","mode","validateSync","validate","then","rawErrors","toNestErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","raw","e","reject"],"mappings":"6LAcA,SAASA,EACPC,EACAC,EACAC,EACAC,GAEA,YAHA,IAAAD,IAAAA,EAA4B,CAAE,YAC9BC,IAAAA,EAAO,IAEAH,EAAOI,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAQJ,EAAUA,EAAI,IAAIG,EAAME,SAAaF,EAAME,SAEzD,GAAIF,EAAMG,YAAa,CACrB,IAAMC,EAAMC,OAAOC,KAAKN,EAAMG,aAAa,GAC3CJ,EAAIE,GAAS,CACXM,KAAMH,EACNI,QAASR,EAAMG,YAAYC,IAG7B,IAAMK,EAAKV,EAAIE,GACXN,GAA4Bc,GAC9BJ,OAAOK,OAAOD,EAAI,CAAEE,MAAOX,EAAMG,aAErC,CAMA,OAJIH,EAAMY,UAAYZ,EAAMY,SAASC,QACnCpB,EAAiBO,EAAMY,SAAUjB,EAA0BI,EAAKE,GAG3DF,CACT,EAAGH,EACL,CAqBM,SAAUkB,EACdC,EACAC,EAIAC,GAEA,gBANAD,IAAAA,EAGI,CAAE,QACN,IAAAC,IAAAA,EAA8D,CAAE,GAEhE,SAAcC,EAAQC,EAAGC,OACvB,IAAqBC,EAAcL,EAAdK,UACfC,EAAOC,EAAaR,EAAQG,EADCF,EAA3BQ,aAC+C,OAAAC,QAAAC,SAEL,SAAzBT,EAAgBU,KACrCC,EACAC,GAAUP,EAAMD,IAAUS,KAFxBC,SAAAA,GAIN,OAAIA,EAAUlB,OACL,CACLK,OAAQ,CAAA,EACRxB,OAAQsC,EACNvC,EACEsC,GACCX,EAAQa,2BACkB,QAAzBb,EAAQc,cAEZd,KAKNA,EAAQa,2BAA6BE,EAAuB,CAAE,EAAEf,GAEzD,CACLF,OAAQD,EAAgBmB,IAAM/B,OAAOK,OAAO,CAAE,EAAEQ,GAAUI,EAC1D5B,OAAQ,CAAA,GACR,EACJ,CAAC,MAAA2C,GAAAZ,OAAAA,QAAAa,OAAAD,EACH,CAAA,CAAA"}
|
||||
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.umd.js
generated
vendored
Normal file
2
node_modules/@hookform/resolvers/class-validator/dist/class-validator.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("class-transformer"),require("class-validator")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","class-transformer","class-validator"],r):r((e||self).hookformResolversClassValidator={},e.hookformResolvers,e.classTransformer,e.classValidator)}(this,function(e,r,o,s){function t(e,r,o,s){return void 0===o&&(o={}),void 0===s&&(s=""),e.reduce(function(e,o){var a=s?s+"."+o.property:o.property;if(o.constraints){var i=Object.keys(o.constraints)[0];e[a]={type:i,message:o.constraints[i]};var n=e[a];r&&n&&Object.assign(n,{types:o.constraints})}return o.children&&o.children.length&&t(o.children,r,e,a),e},o)}e.classValidatorResolver=function(e,a,i){return void 0===a&&(a={}),void 0===i&&(i={}),function(n,l,c){try{var d=a.validator,f=o.plainToClass(e,n,a.transformer);return Promise.resolve(("sync"===i.mode?s.validateSync:s.validate)(f,d)).then(function(e){return e.length?{values:{},errors:r.toNestErrors(t(e,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)}:(c.shouldUseNativeValidation&&r.validateFieldsNatively({},c),{values:i.raw?Object.assign({},n):f,errors:{}})})}catch(e){return Promise.reject(e)}}}});
|
||||
//# sourceMappingURL=class-validator.umd.js.map
|
||||
1
node_modules/@hookform/resolvers/class-validator/dist/class-validator.umd.js.map
generated
vendored
Normal file
1
node_modules/@hookform/resolvers/class-validator/dist/class-validator.umd.js.map
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"class-validator.umd.js","sources":["../src/class-validator.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n ClassConstructor,\n ClassTransformOptions,\n plainToClass,\n} from 'class-transformer';\nimport {\n ValidationError,\n ValidatorOptions,\n validate,\n validateSync,\n} from 'class-validator';\nimport { FieldErrors, Resolver } from 'react-hook-form';\n\nfunction parseErrorSchema(\n errors: ValidationError[],\n validateAllFieldCriteria: boolean,\n parsedErrors: FieldErrors = {},\n path = '',\n) {\n return errors.reduce((acc, error) => {\n const _path = path ? `${path}.${error.property}` : error.property;\n\n if (error.constraints) {\n const key = Object.keys(error.constraints)[0];\n acc[_path] = {\n type: key,\n message: error.constraints[key],\n };\n\n const _e = acc[_path];\n if (validateAllFieldCriteria && _e) {\n Object.assign(_e, { types: error.constraints });\n }\n }\n\n if (error.children && error.children.length) {\n parseErrorSchema(error.children, validateAllFieldCriteria, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n}\n\n/**\n * Creates a resolver for react-hook-form using class-validator schema validation\n * @param {ClassConstructor<Schema>} schema - The class-validator schema to validate against\n * @param {Object} schemaOptions - Additional schema validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver<Schema>} A resolver function compatible with react-hook-form\n * @example\n * class Schema {\n * @Matches(/^\\w+$/)\n * @Length(3, 30)\n * username: string;\n * age: number\n * }\n *\n * useForm({\n * resolver: classValidatorResolver(Schema)\n * });\n */\nexport function classValidatorResolver<Schema extends Record<string, any>>(\n schema: ClassConstructor<Schema>,\n schemaOptions: {\n validator?: ValidatorOptions;\n transformer?: ClassTransformOptions;\n } = {},\n resolverOptions: { mode?: 'async' | 'sync'; raw?: boolean } = {},\n): Resolver<Schema> {\n return async (values, _, options) => {\n const { transformer, validator } = schemaOptions;\n const data = plainToClass(schema, values, transformer);\n\n const rawErrors = await (resolverOptions.mode === 'sync'\n ? validateSync\n : validate)(data, validator);\n\n if (rawErrors.length) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n rawErrors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : data,\n errors: {},\n };\n };\n}\n"],"names":["parseErrorSchema","errors","validateAllFieldCriteria","parsedErrors","path","reduce","acc","error","_path","property","constraints","key","Object","keys","type","message","_e","assign","types","children","length","schema","schemaOptions","resolverOptions","values","_","options","validator","data","plainToClass","transformer","Promise","resolve","mode","validateSync","validate","then","rawErrors","toNestErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","raw","e","reject"],"mappings":"0cAcA,SAASA,EACPC,EACAC,EACAC,EACAC,GAEA,YAHA,IAAAD,IAAAA,EAA4B,CAAE,YAC9BC,IAAAA,EAAO,IAEAH,EAAOI,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAQJ,EAAUA,EAAI,IAAIG,EAAME,SAAaF,EAAME,SAEzD,GAAIF,EAAMG,YAAa,CACrB,IAAMC,EAAMC,OAAOC,KAAKN,EAAMG,aAAa,GAC3CJ,EAAIE,GAAS,CACXM,KAAMH,EACNI,QAASR,EAAMG,YAAYC,IAG7B,IAAMK,EAAKV,EAAIE,GACXN,GAA4Bc,GAC9BJ,OAAOK,OAAOD,EAAI,CAAEE,MAAOX,EAAMG,aAErC,CAMA,OAJIH,EAAMY,UAAYZ,EAAMY,SAASC,QACnCpB,EAAiBO,EAAMY,SAAUjB,EAA0BI,EAAKE,GAG3DF,CACT,EAAGH,EACL,0BAqBM,SACJkB,EACAC,EAIAC,GAEA,gBANAD,IAAAA,EAGI,CAAE,QACN,IAAAC,IAAAA,EAA8D,CAAE,GAEhE,SAAcC,EAAQC,EAAGC,OACvB,IAAqBC,EAAcL,EAAdK,UACfC,EAAOC,EAAYA,aAACR,EAAQG,EADCF,EAA3BQ,aAC+C,OAAAC,QAAAC,SAEL,SAAzBT,EAAgBU,KACrCC,eACAC,EAAAA,UAAUP,EAAMD,IAAUS,KAFxBC,SAAAA,GAIN,OAAIA,EAAUjB,OACL,CACLI,OAAQ,CAAA,EACRvB,OAAQqC,eACNtC,EACEqC,GACCX,EAAQa,2BACkB,QAAzBb,EAAQc,cAEZd,KAKNA,EAAQa,2BAA6BE,EAAsBA,uBAAC,CAAE,EAAEf,GAEzD,CACLF,OAAQD,EAAgBmB,IAAM9B,OAAOK,OAAO,CAAE,EAAEO,GAAUI,EAC1D3B,OAAQ,CAAA,GACR,EACJ,CAAC,MAAA0C,GAAAZ,OAAAA,QAAAa,OAAAD,EACH,CAAA,CAAA"}
|
||||
1
node_modules/@hookform/resolvers/class-validator/dist/index.d.ts
generated
vendored
Normal file
1
node_modules/@hookform/resolvers/class-validator/dist/index.d.ts
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from './class-validator';
|
||||
19
node_modules/@hookform/resolvers/class-validator/package.json
generated
vendored
Normal file
19
node_modules/@hookform/resolvers/class-validator/package.json
generated
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "@hookform/resolvers/class-validator",
|
||||
"amdName": "hookformResolversClassValidator",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"description": "React Hook Form validation resolver: class-validator",
|
||||
"main": "dist/class-validator.js",
|
||||
"module": "dist/class-validator.module.js",
|
||||
"umd:main": "dist/class-validator.umd.js",
|
||||
"source": "src/index.ts",
|
||||
"types": "dist/index.d.ts",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"react-hook-form": ">=6.6.0",
|
||||
"@hookform/resolvers": ">=2.0.0",
|
||||
"class-transformer": "^0.4.0",
|
||||
"class-validator": "^0.12.0"
|
||||
}
|
||||
}
|
||||
79
node_modules/@hookform/resolvers/class-validator/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
79
node_modules/@hookform/resolvers/class-validator/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
import React from 'react';
|
||||
import { SubmitHandler, useForm } from 'react-hook-form';
|
||||
import { classValidatorResolver } from '..';
|
||||
|
||||
class Schema {
|
||||
@IsNotEmpty()
|
||||
username: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
onSubmit: SubmitHandler<Schema>;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const { register, handleSubmit } = useForm<Schema>({
|
||||
resolver: classValidatorResolver(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 Class Validator", 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 should not be empty');
|
||||
|
||||
// password
|
||||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
||||
expect(passwordField.validity.valid).toBe(false);
|
||||
expect(passwordField.validationMessage).toBe('password should not be empty');
|
||||
|
||||
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('');
|
||||
});
|
||||
53
node_modules/@hookform/resolvers/class-validator/src/__tests__/Form.tsx
generated
vendored
Normal file
53
node_modules/@hookform/resolvers/class-validator/src/__tests__/Form.tsx
generated
vendored
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
import React from 'react';
|
||||
import { SubmitHandler, useForm } from 'react-hook-form';
|
||||
import { classValidatorResolver } from '..';
|
||||
|
||||
class Schema {
|
||||
@IsNotEmpty()
|
||||
username: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
onSubmit: SubmitHandler<Schema>;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const {
|
||||
register,
|
||||
formState: { errors },
|
||||
handleSubmit,
|
||||
} = useForm<Schema>({
|
||||
resolver: classValidatorResolver(Schema),
|
||||
});
|
||||
|
||||
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 Class Validator 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(/username should not be empty/i)).toBeInTheDocument();
|
||||
expect(screen.getByText(/password should not be empty/i)).toBeInTheDocument();
|
||||
expect(handleSubmit).not.toHaveBeenCalled();
|
||||
});
|
||||
88
node_modules/@hookform/resolvers/class-validator/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
88
node_modules/@hookform/resolvers/class-validator/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
import 'reflect-metadata';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsEmail,
|
||||
IsNotEmpty,
|
||||
Length,
|
||||
Matches,
|
||||
Max,
|
||||
Min,
|
||||
ValidateNested,
|
||||
} from 'class-validator';
|
||||
import { Field, InternalFieldName } from 'react-hook-form';
|
||||
|
||||
class Like {
|
||||
@IsNotEmpty()
|
||||
id: number;
|
||||
|
||||
@Length(4)
|
||||
name: string;
|
||||
}
|
||||
|
||||
export class Schema {
|
||||
@Matches(/^\w+$/)
|
||||
@Length(3, 30)
|
||||
username: string;
|
||||
|
||||
@Matches(/^[a-zA-Z0-9]{3,30}/)
|
||||
password: string;
|
||||
|
||||
@Min(1900)
|
||||
@Max(2013)
|
||||
birthYear: number;
|
||||
|
||||
@IsEmail()
|
||||
email: string;
|
||||
|
||||
accessToken: string;
|
||||
|
||||
tags: string[];
|
||||
|
||||
enabled: boolean;
|
||||
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => Like)
|
||||
like: Like[];
|
||||
}
|
||||
|
||||
export const validData: Schema = {
|
||||
username: 'Doe',
|
||||
password: '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' }],
|
||||
} as any as Schema;
|
||||
|
||||
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',
|
||||
},
|
||||
};
|
||||
242
node_modules/@hookform/resolvers/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap
generated
vendored
Normal file
242
node_modules/@hookform/resolvers/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap
generated
vendored
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`classValidatorResolver > should return a single error from classValidatorResolver when validation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"birthYear": {
|
||||
"message": "birthYear must not be greater than 2013",
|
||||
"ref": undefined,
|
||||
"type": "max",
|
||||
},
|
||||
"email": {
|
||||
"message": "email must be an email",
|
||||
"ref": {
|
||||
"name": "email",
|
||||
},
|
||||
"type": "isEmail",
|
||||
},
|
||||
"like": [
|
||||
{
|
||||
"name": {
|
||||
"message": "name must be longer than or equal to 4 characters",
|
||||
"ref": undefined,
|
||||
"type": "isLength",
|
||||
},
|
||||
},
|
||||
],
|
||||
"password": {
|
||||
"message": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "matches",
|
||||
},
|
||||
"username": {
|
||||
"message": "username must be longer than or equal to 3 characters",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "isLength",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`classValidatorResolver > should return a single error from classValidatorResolver with \`mode: sync\` when validation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"birthYear": {
|
||||
"message": "birthYear must not be greater than 2013",
|
||||
"ref": undefined,
|
||||
"type": "max",
|
||||
},
|
||||
"email": {
|
||||
"message": "email must be an email",
|
||||
"ref": {
|
||||
"name": "email",
|
||||
},
|
||||
"type": "isEmail",
|
||||
},
|
||||
"like": [
|
||||
{
|
||||
"name": {
|
||||
"message": "name must be longer than or equal to 4 characters",
|
||||
"ref": undefined,
|
||||
"type": "isLength",
|
||||
},
|
||||
},
|
||||
],
|
||||
"password": {
|
||||
"message": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "matches",
|
||||
},
|
||||
"username": {
|
||||
"message": "username must be longer than or equal to 3 characters",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "isLength",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`classValidatorResolver > should return all the errors from classValidatorResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"birthYear": {
|
||||
"message": "birthYear must not be greater than 2013",
|
||||
"ref": undefined,
|
||||
"type": "max",
|
||||
"types": {
|
||||
"max": "birthYear must not be greater than 2013",
|
||||
"min": "birthYear must not be less than 1900",
|
||||
},
|
||||
},
|
||||
"email": {
|
||||
"message": "email must be an email",
|
||||
"ref": {
|
||||
"name": "email",
|
||||
},
|
||||
"type": "isEmail",
|
||||
"types": {
|
||||
"isEmail": "email must be an email",
|
||||
},
|
||||
},
|
||||
"like": [
|
||||
{
|
||||
"name": {
|
||||
"message": "name must be longer than or equal to 4 characters",
|
||||
"ref": undefined,
|
||||
"type": "isLength",
|
||||
"types": {
|
||||
"isLength": "name must be longer than or equal to 4 characters",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
"password": {
|
||||
"message": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "matches",
|
||||
"types": {
|
||||
"matches": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
|
||||
},
|
||||
},
|
||||
"username": {
|
||||
"message": "username must be longer than or equal to 3 characters",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "isLength",
|
||||
"types": {
|
||||
"isLength": "username must be longer than or equal to 3 characters",
|
||||
"matches": "username must match /^\\w+$/ regular expression",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`classValidatorResolver > should return all the errors from classValidatorResolver when validation fails with \`validateAllFieldCriteria\` set to true and \`mode: sync\` 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"birthYear": {
|
||||
"message": "birthYear must not be greater than 2013",
|
||||
"ref": undefined,
|
||||
"type": "max",
|
||||
"types": {
|
||||
"max": "birthYear must not be greater than 2013",
|
||||
"min": "birthYear must not be less than 1900",
|
||||
},
|
||||
},
|
||||
"email": {
|
||||
"message": "email must be an email",
|
||||
"ref": {
|
||||
"name": "email",
|
||||
},
|
||||
"type": "isEmail",
|
||||
"types": {
|
||||
"isEmail": "email must be an email",
|
||||
},
|
||||
},
|
||||
"like": [
|
||||
{
|
||||
"name": {
|
||||
"message": "name must be longer than or equal to 4 characters",
|
||||
"ref": undefined,
|
||||
"type": "isLength",
|
||||
"types": {
|
||||
"isLength": "name must be longer than or equal to 4 characters",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
"password": {
|
||||
"message": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "matches",
|
||||
"types": {
|
||||
"matches": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
|
||||
},
|
||||
},
|
||||
"username": {
|
||||
"message": "username must be longer than or equal to 3 characters",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "isLength",
|
||||
"types": {
|
||||
"isLength": "username must be longer than or equal to 3 characters",
|
||||
"matches": "username must match /^\\w+$/ regular expression",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`validate data with transformer option 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"random": {
|
||||
"message": "All fields must be defined.",
|
||||
"ref": undefined,
|
||||
"type": "isDefined",
|
||||
"types": {
|
||||
"isDefined": "All fields must be defined.",
|
||||
"isNumber": "Must be a number",
|
||||
"max": "Cannot be greater than 255",
|
||||
"min": "Cannot be lower than 0",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`validate data with validator option 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"random": {
|
||||
"message": "All fields must be defined.",
|
||||
"ref": undefined,
|
||||
"type": "isDefined",
|
||||
"types": {
|
||||
"isDefined": "All fields must be defined.",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
199
node_modules/@hookform/resolvers/class-validator/src/__tests__/class-validator.ts
generated
vendored
Normal file
199
node_modules/@hookform/resolvers/class-validator/src/__tests__/class-validator.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
import { Expose, Type } from 'class-transformer';
|
||||
import * as classValidator from 'class-validator';
|
||||
import { IsDefined, IsNumber, Max, Min } from 'class-validator';
|
||||
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
|
||||
import { classValidatorResolver } from '..';
|
||||
import { Schema, fields, invalidData, validData } from './__fixtures__/data';
|
||||
|
||||
const shouldUseNativeValidation = false;
|
||||
|
||||
describe('classValidatorResolver', () => {
|
||||
it('should return values from classValidatorResolver when validation pass', async () => {
|
||||
const schemaSpy = vi.spyOn(classValidator, 'validate');
|
||||
const schemaSyncSpy = vi.spyOn(classValidator, 'validateSync');
|
||||
|
||||
const result = await classValidatorResolver(Schema)(validData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(schemaSpy).toHaveBeenCalledTimes(1);
|
||||
expect(schemaSyncSpy).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ errors: {}, values: validData });
|
||||
expect(result.values).toBeInstanceOf(Schema);
|
||||
});
|
||||
|
||||
it('should return values as a raw object from classValidatorResolver when `rawValues` set to true', async () => {
|
||||
const result = await classValidatorResolver(Schema, undefined, {
|
||||
raw: true,
|
||||
})(validData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ errors: {}, values: validData });
|
||||
expect(result.values).not.toBeInstanceOf(Schema);
|
||||
});
|
||||
|
||||
it('should return values from classValidatorResolver with `mode: sync` when validation pass', async () => {
|
||||
const validateSyncSpy = vi.spyOn(classValidator, 'validateSync');
|
||||
const validateSpy = vi.spyOn(classValidator, 'validate');
|
||||
|
||||
const result = await classValidatorResolver(Schema, undefined, {
|
||||
mode: 'sync',
|
||||
})(validData, undefined, { fields, shouldUseNativeValidation });
|
||||
|
||||
expect(validateSyncSpy).toHaveBeenCalledTimes(1);
|
||||
expect(validateSpy).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ errors: {}, values: validData });
|
||||
expect(result.values).toBeInstanceOf(Schema);
|
||||
});
|
||||
|
||||
it('should return a single error from classValidatorResolver when validation fails', async () => {
|
||||
const result = await classValidatorResolver(Schema)(
|
||||
invalidData,
|
||||
undefined,
|
||||
{
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return a single error from classValidatorResolver with `mode: sync` when validation fails', async () => {
|
||||
const validateSyncSpy = vi.spyOn(classValidator, 'validateSync');
|
||||
const validateSpy = vi.spyOn(classValidator, 'validate');
|
||||
|
||||
const result = await classValidatorResolver(Schema, undefined, {
|
||||
mode: 'sync',
|
||||
})(invalidData, undefined, { fields, shouldUseNativeValidation });
|
||||
|
||||
expect(validateSyncSpy).toHaveBeenCalledTimes(1);
|
||||
expect(validateSpy).not.toHaveBeenCalled();
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return all the errors from classValidatorResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
||||
const result = await classValidatorResolver(Schema)(
|
||||
invalidData,
|
||||
undefined,
|
||||
{
|
||||
fields,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return all the errors from classValidatorResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
|
||||
const result = await classValidatorResolver(Schema, undefined, {
|
||||
mode: 'sync',
|
||||
})(invalidData, undefined, {
|
||||
fields,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
it('validate data with transformer option', async () => {
|
||||
class SchemaTest {
|
||||
@Expose({ groups: ['find', 'create', 'update'] })
|
||||
@Type(() => Number)
|
||||
@IsDefined({
|
||||
message: `All fields must be defined.`,
|
||||
groups: ['publish'],
|
||||
})
|
||||
@IsNumber({}, { message: `Must be a number`, always: true })
|
||||
@Min(0, { message: `Cannot be lower than 0`, always: true })
|
||||
@Max(255, { message: `Cannot be greater than 255`, always: true })
|
||||
random: number;
|
||||
}
|
||||
|
||||
const result = await classValidatorResolver(
|
||||
SchemaTest,
|
||||
{ transformer: { groups: ['update'] } },
|
||||
{
|
||||
mode: 'sync',
|
||||
},
|
||||
)(
|
||||
// @ts-expect-error - Just for testing purposes
|
||||
invalidData,
|
||||
undefined,
|
||||
{
|
||||
fields,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('validate data with validator option', async () => {
|
||||
class SchemaTest {
|
||||
@Expose({ groups: ['find', 'create', 'update'] })
|
||||
@Type(() => Number)
|
||||
@IsDefined({
|
||||
message: `All fields must be defined.`,
|
||||
groups: ['publish'],
|
||||
})
|
||||
@IsNumber({}, { message: `Must be a number`, always: true })
|
||||
@Min(0, { message: `Cannot be lower than 0`, always: true })
|
||||
@Max(255, { message: `Cannot be greater than 255`, always: true })
|
||||
random: number;
|
||||
}
|
||||
|
||||
const result = await classValidatorResolver(
|
||||
SchemaTest,
|
||||
{ validator: { stopAtFirstError: true } },
|
||||
{
|
||||
mode: 'sync',
|
||||
},
|
||||
)(
|
||||
// @ts-expect-error - Just for testing purposes
|
||||
invalidData,
|
||||
undefined,
|
||||
{
|
||||
fields,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return from classValidatorResolver with `excludeExtraneousValues` set to true', async () => {
|
||||
class SchemaTest {
|
||||
@Expose()
|
||||
@IsNumber({}, { message: `Must be a number`, always: true })
|
||||
random: number;
|
||||
}
|
||||
|
||||
const result = await classValidatorResolver(SchemaTest, {
|
||||
transformer: {
|
||||
excludeExtraneousValues: true,
|
||||
},
|
||||
})(
|
||||
{
|
||||
random: 10,
|
||||
// @ts-expect-error - Just for testing purposes
|
||||
extraneousField: true,
|
||||
},
|
||||
undefined,
|
||||
{
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual({ errors: {}, values: { random: 10 } });
|
||||
expect(result.values).toBeInstanceOf(SchemaTest);
|
||||
});
|
||||
101
node_modules/@hookform/resolvers/class-validator/src/class-validator.ts
generated
vendored
Normal file
101
node_modules/@hookform/resolvers/class-validator/src/class-validator.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
||||
import {
|
||||
ClassConstructor,
|
||||
ClassTransformOptions,
|
||||
plainToClass,
|
||||
} from 'class-transformer';
|
||||
import {
|
||||
ValidationError,
|
||||
ValidatorOptions,
|
||||
validate,
|
||||
validateSync,
|
||||
} from 'class-validator';
|
||||
import { FieldErrors, Resolver } from 'react-hook-form';
|
||||
|
||||
function parseErrorSchema(
|
||||
errors: ValidationError[],
|
||||
validateAllFieldCriteria: boolean,
|
||||
parsedErrors: FieldErrors = {},
|
||||
path = '',
|
||||
) {
|
||||
return errors.reduce((acc, error) => {
|
||||
const _path = path ? `${path}.${error.property}` : error.property;
|
||||
|
||||
if (error.constraints) {
|
||||
const key = Object.keys(error.constraints)[0];
|
||||
acc[_path] = {
|
||||
type: key,
|
||||
message: error.constraints[key],
|
||||
};
|
||||
|
||||
const _e = acc[_path];
|
||||
if (validateAllFieldCriteria && _e) {
|
||||
Object.assign(_e, { types: error.constraints });
|
||||
}
|
||||
}
|
||||
|
||||
if (error.children && error.children.length) {
|
||||
parseErrorSchema(error.children, validateAllFieldCriteria, acc, _path);
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, parsedErrors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a resolver for react-hook-form using class-validator schema validation
|
||||
* @param {ClassConstructor<Schema>} schema - The class-validator schema to validate against
|
||||
* @param {Object} schemaOptions - Additional schema validation options
|
||||
* @param {Object} resolverOptions - Additional resolver configuration
|
||||
* @param {string} [resolverOptions.mode='async'] - Validation mode
|
||||
* @returns {Resolver<Schema>} A resolver function compatible with react-hook-form
|
||||
* @example
|
||||
* class Schema {
|
||||
* @Matches(/^\w+$/)
|
||||
* @Length(3, 30)
|
||||
* username: string;
|
||||
* age: number
|
||||
* }
|
||||
*
|
||||
* useForm({
|
||||
* resolver: classValidatorResolver(Schema)
|
||||
* });
|
||||
*/
|
||||
export function classValidatorResolver<Schema extends Record<string, any>>(
|
||||
schema: ClassConstructor<Schema>,
|
||||
schemaOptions: {
|
||||
validator?: ValidatorOptions;
|
||||
transformer?: ClassTransformOptions;
|
||||
} = {},
|
||||
resolverOptions: { mode?: 'async' | 'sync'; raw?: boolean } = {},
|
||||
): Resolver<Schema> {
|
||||
return async (values, _, options) => {
|
||||
const { transformer, validator } = schemaOptions;
|
||||
const data = plainToClass(schema, values, transformer);
|
||||
|
||||
const rawErrors = await (resolverOptions.mode === 'sync'
|
||||
? validateSync
|
||||
: validate)(data, validator);
|
||||
|
||||
if (rawErrors.length) {
|
||||
return {
|
||||
values: {},
|
||||
errors: toNestErrors(
|
||||
parseErrorSchema(
|
||||
rawErrors,
|
||||
!options.shouldUseNativeValidation &&
|
||||
options.criteriaMode === 'all',
|
||||
),
|
||||
options,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
||||
|
||||
return {
|
||||
values: resolverOptions.raw ? Object.assign({}, values) : data,
|
||||
errors: {},
|
||||
};
|
||||
};
|
||||
}
|
||||
1
node_modules/@hookform/resolvers/class-validator/src/index.ts
generated
vendored
Normal file
1
node_modules/@hookform/resolvers/class-validator/src/index.ts
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from './class-validator';
|
||||
Loading…
Add table
Add a link
Reference in a new issue