@hookform/resolvers 3.10.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -6
- package/ajv/dist/ajv.d.ts +20 -0
- package/ajv/dist/ajv.js +1 -1
- package/ajv/dist/ajv.js.map +1 -1
- package/ajv/dist/ajv.mjs +1 -1
- package/ajv/dist/ajv.modern.mjs +1 -1
- package/ajv/dist/ajv.modern.mjs.map +1 -1
- package/ajv/dist/ajv.module.js +1 -1
- package/ajv/dist/ajv.module.js.map +1 -1
- package/ajv/dist/ajv.umd.js +1 -1
- package/ajv/dist/ajv.umd.js.map +1 -1
- package/ajv/dist/types.d.ts +8 -0
- package/ajv/src/__tests__/__fixtures__/data-errors.ts +216 -0
- package/ajv/src/__tests__/__snapshots__/ajv-errors.ts.snap +462 -0
- package/ajv/src/__tests__/__snapshots__/ajv.ts.snap +6 -6
- package/ajv/src/__tests__/ajv-errors.ts +227 -0
- package/ajv/src/ajv.ts +47 -14
- package/ajv/src/types.ts +8 -0
- package/arktype/dist/arktype.d.ts +20 -2
- package/arktype/dist/arktype.js +1 -1
- package/arktype/dist/arktype.js.map +1 -1
- package/arktype/dist/arktype.mjs +1 -1
- package/arktype/dist/arktype.modern.mjs +1 -1
- package/arktype/dist/arktype.modern.mjs.map +1 -1
- package/arktype/dist/arktype.module.js +1 -1
- package/arktype/dist/arktype.module.js.map +1 -1
- package/arktype/dist/arktype.umd.js +1 -1
- package/arktype/dist/arktype.umd.js.map +1 -1
- package/arktype/dist/index.d.ts +0 -1
- package/arktype/package.json +1 -1
- package/arktype/src/__tests__/Form-native-validation.tsx +3 -3
- package/arktype/src/__tests__/Form.tsx +34 -8
- package/arktype/src/__tests__/__fixtures__/data.ts +2 -2
- package/arktype/src/__tests__/__snapshots__/arktype.ts.snap +22 -415
- package/arktype/src/arktype.ts +46 -15
- package/arktype/src/index.ts +0 -1
- package/class-validator/dist/class-validator.d.ts +29 -2
- package/class-validator/dist/class-validator.js +1 -1
- package/class-validator/dist/class-validator.js.map +1 -1
- package/class-validator/dist/class-validator.mjs +1 -1
- package/class-validator/dist/class-validator.modern.mjs +1 -1
- package/class-validator/dist/class-validator.modern.mjs.map +1 -1
- package/class-validator/dist/class-validator.module.js +1 -1
- package/class-validator/dist/class-validator.module.js.map +1 -1
- package/class-validator/dist/class-validator.umd.js +1 -1
- package/class-validator/dist/class-validator.umd.js.map +1 -1
- package/class-validator/dist/index.d.ts +0 -1
- package/class-validator/src/__tests__/Form.tsx +26 -0
- package/class-validator/src/__tests__/__fixtures__/data.ts +1 -1
- package/class-validator/src/__tests__/class-validator.ts +22 -11
- package/class-validator/src/class-validator.ts +47 -13
- package/class-validator/src/index.ts +0 -1
- package/computed-types/dist/computed-types.d.ts +18 -2
- package/computed-types/dist/computed-types.js.map +1 -1
- package/computed-types/dist/computed-types.mjs +1 -1
- package/computed-types/dist/computed-types.modern.mjs +1 -1
- package/computed-types/dist/computed-types.modern.mjs.map +1 -1
- package/computed-types/dist/computed-types.module.js +1 -1
- package/computed-types/dist/computed-types.module.js.map +1 -1
- package/computed-types/dist/computed-types.umd.js.map +1 -1
- package/computed-types/dist/index.d.ts +0 -1
- package/computed-types/src/__tests__/Form-native-validation.tsx +2 -4
- package/computed-types/src/__tests__/Form.tsx +30 -6
- package/computed-types/src/__tests__/__fixtures__/data.ts +1 -1
- package/computed-types/src/__tests__/computed-types.ts +2 -0
- package/computed-types/src/computed-types.ts +24 -7
- package/computed-types/src/index.ts +0 -1
- package/dist/resolvers.js +1 -1
- package/dist/resolvers.js.map +1 -1
- package/dist/resolvers.mjs +1 -1
- package/dist/resolvers.mjs.map +1 -1
- package/dist/resolvers.module.js +1 -1
- package/dist/resolvers.module.js.map +1 -1
- package/dist/resolvers.umd.js +1 -1
- package/dist/resolvers.umd.js.map +1 -1
- package/effect-ts/dist/effect-ts.d.ts +19 -2
- package/effect-ts/dist/effect-ts.js +1 -1
- package/effect-ts/dist/effect-ts.js.map +1 -1
- package/effect-ts/dist/effect-ts.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs.map +1 -1
- package/effect-ts/dist/effect-ts.module.js +1 -1
- package/effect-ts/dist/effect-ts.module.js.map +1 -1
- package/effect-ts/dist/effect-ts.umd.js +1 -1
- package/effect-ts/dist/effect-ts.umd.js.map +1 -1
- package/effect-ts/dist/index.d.ts +0 -1
- package/effect-ts/src/__tests__/Form.tsx +30 -4
- package/effect-ts/src/__tests__/__fixtures__/data.ts +1 -1
- package/effect-ts/src/__tests__/__snapshots__/effect-ts.ts.snap +35 -1
- package/effect-ts/src/__tests__/effect-ts.ts +61 -0
- package/effect-ts/src/effect-ts.ts +60 -13
- package/effect-ts/src/index.ts +0 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.d.ts +21 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.js +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.js.map +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.mjs +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs.map +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js.map +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js.map +1 -1
- package/fluentvalidation-ts/src/fluentvalidation-ts.ts +24 -3
- package/io-ts/dist/errorsToRecord.d.ts +5 -1
- package/io-ts/dist/index.d.ts +0 -1
- package/io-ts/dist/io-ts.d.ts +19 -2
- package/io-ts/dist/io-ts.js +1 -1
- package/io-ts/dist/io-ts.js.map +1 -1
- package/io-ts/dist/io-ts.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs.map +1 -1
- package/io-ts/dist/io-ts.module.js +1 -1
- package/io-ts/dist/io-ts.module.js.map +1 -1
- package/io-ts/dist/io-ts.umd.js +1 -1
- package/io-ts/dist/io-ts.umd.js.map +1 -1
- package/io-ts/src/__tests__/Form-native-validation.tsx +4 -11
- package/io-ts/src/__tests__/Form.tsx +30 -6
- package/io-ts/src/__tests__/__fixtures__/data.ts +2 -19
- package/io-ts/src/errorsToRecord.ts +4 -1
- package/io-ts/src/index.ts +0 -1
- package/io-ts/src/io-ts.ts +47 -25
- package/joi/dist/joi.d.ts +17 -0
- package/joi/dist/joi.js.map +1 -1
- package/joi/dist/joi.modern.mjs.map +1 -1
- package/joi/dist/joi.module.js.map +1 -1
- package/joi/dist/joi.umd.js.map +1 -1
- package/joi/src/joi.ts +17 -0
- package/nope/dist/nope.d.ts +17 -0
- package/nope/dist/nope.js +1 -1
- package/nope/dist/nope.js.map +1 -1
- package/nope/dist/nope.mjs +1 -1
- package/nope/dist/nope.modern.mjs.map +1 -1
- package/nope/dist/nope.module.js +1 -1
- package/nope/dist/nope.module.js.map +1 -1
- package/nope/dist/nope.umd.js +1 -1
- package/nope/dist/nope.umd.js.map +1 -1
- package/nope/src/nope.ts +17 -0
- package/package.json +49 -34
- package/standard-schema/dist/index.d.ts +1 -0
- package/standard-schema/dist/standard-schema.d.ts +25 -0
- package/standard-schema/dist/standard-schema.js +2 -0
- package/standard-schema/dist/standard-schema.js.map +1 -0
- package/standard-schema/dist/standard-schema.mjs +2 -0
- package/standard-schema/dist/standard-schema.modern.mjs +2 -0
- package/standard-schema/dist/standard-schema.modern.mjs.map +1 -0
- package/standard-schema/dist/standard-schema.module.js +2 -0
- package/standard-schema/dist/standard-schema.module.js.map +1 -0
- package/standard-schema/dist/standard-schema.umd.js +2 -0
- package/standard-schema/dist/standard-schema.umd.js.map +1 -0
- package/standard-schema/package.json +18 -0
- package/standard-schema/src/__tests__/Form-native-validation.tsx +82 -0
- package/standard-schema/src/__tests__/Form.tsx +82 -0
- package/standard-schema/src/__tests__/__fixtures__/data.ts +88 -0
- package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +250 -0
- package/standard-schema/src/__tests__/standard-schema.ts +56 -0
- package/standard-schema/src/index.ts +1 -0
- package/standard-schema/src/standard-schema.ts +88 -0
- package/superstruct/dist/index.d.ts +0 -1
- package/superstruct/dist/superstruct.d.ts +22 -2
- package/superstruct/dist/superstruct.js +1 -1
- package/superstruct/dist/superstruct.js.map +1 -1
- package/superstruct/dist/superstruct.mjs +1 -1
- package/superstruct/dist/superstruct.modern.mjs +1 -1
- package/superstruct/dist/superstruct.modern.mjs.map +1 -1
- package/superstruct/dist/superstruct.module.js +1 -1
- package/superstruct/dist/superstruct.module.js.map +1 -1
- package/superstruct/dist/superstruct.umd.js +1 -1
- package/superstruct/dist/superstruct.umd.js.map +1 -1
- package/superstruct/src/__tests__/Form.tsx +26 -0
- package/superstruct/src/__tests__/__fixtures__/data.ts +1 -1
- package/superstruct/src/index.ts +0 -1
- package/superstruct/src/superstruct.ts +32 -10
- package/typanion/dist/index.d.ts +0 -1
- package/typanion/dist/typanion.d.ts +18 -2
- package/typanion/dist/typanion.js.map +1 -1
- package/typanion/dist/typanion.mjs +1 -1
- package/typanion/dist/typanion.modern.mjs +1 -1
- package/typanion/dist/typanion.modern.mjs.map +1 -1
- package/typanion/dist/typanion.module.js +1 -1
- package/typanion/dist/typanion.module.js.map +1 -1
- package/typanion/dist/typanion.umd.js.map +1 -1
- package/typanion/src/__tests__/Form-native-validation.tsx +6 -14
- package/typanion/src/__tests__/Form.tsx +32 -8
- package/typanion/src/__tests__/__fixtures__/data.ts +9 -9
- package/typanion/src/__tests__/typanion.ts +3 -3
- package/typanion/src/index.ts +0 -1
- package/typanion/src/typanion.ts +32 -9
- package/typebox/dist/index.d.ts +0 -1
- package/typebox/dist/typebox.d.ts +20 -2
- package/typebox/dist/typebox.js +1 -1
- package/typebox/dist/typebox.js.map +1 -1
- package/typebox/dist/typebox.mjs +1 -1
- package/typebox/dist/typebox.modern.mjs +1 -1
- package/typebox/dist/typebox.modern.mjs.map +1 -1
- package/typebox/dist/typebox.module.js +1 -1
- package/typebox/dist/typebox.module.js.map +1 -1
- package/typebox/dist/typebox.umd.js +1 -1
- package/typebox/dist/typebox.umd.js.map +1 -1
- package/typebox/src/__tests__/Form-compiler.tsx +4 -8
- package/typebox/src/__tests__/Form.tsx +32 -6
- package/typebox/src/__tests__/__fixtures__/data.ts +1 -1
- package/typebox/src/__tests__/__snapshots__/typebox-compiler.ts.snap +21 -21
- package/typebox/src/__tests__/__snapshots__/typebox.ts.snap +21 -21
- package/typebox/src/index.ts +0 -1
- package/typebox/src/typebox.ts +33 -7
- package/typeschema/dist/typeschema.d.ts +17 -0
- package/typeschema/dist/typeschema.js +1 -1
- package/typeschema/dist/typeschema.js.map +1 -1
- package/typeschema/dist/typeschema.mjs +1 -1
- package/typeschema/dist/typeschema.modern.mjs +1 -1
- package/typeschema/dist/typeschema.modern.mjs.map +1 -1
- package/typeschema/dist/typeschema.module.js +1 -1
- package/typeschema/dist/typeschema.module.js.map +1 -1
- package/typeschema/dist/typeschema.umd.js +1 -1
- package/typeschema/dist/typeschema.umd.js.map +1 -1
- package/typeschema/src/typeschema.ts +20 -1
- package/valibot/dist/index.d.ts +0 -1
- package/valibot/dist/valibot.d.ts +31 -2
- package/valibot/dist/valibot.js +1 -1
- package/valibot/dist/valibot.js.map +1 -1
- package/valibot/dist/valibot.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs.map +1 -1
- package/valibot/dist/valibot.module.js +1 -1
- package/valibot/dist/valibot.module.js.map +1 -1
- package/valibot/dist/valibot.umd.js +1 -1
- package/valibot/dist/valibot.umd.js.map +1 -1
- package/valibot/package.json +1 -1
- package/valibot/src/__tests__/Form.tsx +26 -0
- package/valibot/src/__tests__/__fixtures__/data.ts +7 -3
- package/valibot/src/__tests__/valibot.ts +1 -1
- package/valibot/src/index.ts +0 -1
- package/valibot/src/valibot.ts +55 -6
- package/vest/dist/vest.d.ts +18 -0
- package/vest/dist/vest.js.map +1 -1
- package/vest/dist/vest.modern.mjs.map +1 -1
- package/vest/dist/vest.module.js.map +1 -1
- package/vest/dist/vest.umd.js.map +1 -1
- package/vest/src/vest.ts +18 -0
- package/vine/dist/index.d.ts +0 -1
- package/vine/dist/vine.d.ts +25 -2
- package/vine/dist/vine.js +1 -1
- package/vine/dist/vine.js.map +1 -1
- package/vine/dist/vine.mjs +1 -1
- package/vine/dist/vine.modern.mjs +1 -1
- package/vine/dist/vine.modern.mjs.map +1 -1
- package/vine/dist/vine.module.js +1 -1
- package/vine/dist/vine.module.js.map +1 -1
- package/vine/dist/vine.umd.js +1 -1
- package/vine/dist/vine.umd.js.map +1 -1
- package/vine/src/__tests__/Form-native-validation.tsx +2 -4
- package/vine/src/__tests__/Form.tsx +30 -6
- package/vine/src/__tests__/__fixtures__/data.ts +1 -1
- package/vine/src/index.ts +0 -1
- package/vine/src/vine.ts +38 -10
- package/yup/dist/yup.d.ts +18 -7
- package/yup/dist/yup.js +1 -1
- package/yup/dist/yup.js.map +1 -1
- package/yup/dist/yup.mjs +1 -1
- package/yup/dist/yup.modern.mjs +1 -1
- package/yup/dist/yup.modern.mjs.map +1 -1
- package/yup/dist/yup.module.js +1 -1
- package/yup/dist/yup.module.js.map +1 -1
- package/yup/dist/yup.umd.js +1 -1
- package/yup/dist/yup.umd.js.map +1 -1
- package/yup/src/yup.ts +22 -11
- package/zod/dist/index.d.ts +0 -1
- package/zod/dist/zod.d.ts +25 -2
- package/zod/dist/zod.js +1 -1
- package/zod/dist/zod.js.map +1 -1
- package/zod/dist/zod.mjs +1 -1
- package/zod/dist/zod.modern.mjs +1 -1
- package/zod/dist/zod.modern.mjs.map +1 -1
- package/zod/dist/zod.module.js +1 -1
- package/zod/dist/zod.module.js.map +1 -1
- package/zod/dist/zod.umd.js +1 -1
- package/zod/dist/zod.umd.js.map +1 -1
- package/zod/src/__tests__/Form.tsx +30 -6
- package/zod/src/__tests__/__fixtures__/data.ts +3 -3
- package/zod/src/index.ts +0 -1
- package/zod/src/zod.ts +40 -9
- package/arktype/dist/types.d.ts +0 -9
- package/arktype/src/types.ts +0 -18
- package/class-validator/dist/types.d.ts +0 -12
- package/class-validator/src/types.ts +0 -16
- package/computed-types/dist/types.d.ts +0 -2
- package/computed-types/src/types.ts +0 -13
- package/effect-ts/dist/types.d.ts +0 -4
- package/effect-ts/src/types.ts +0 -12
- package/io-ts/dist/types.d.ts +0 -7
- package/io-ts/src/types.ts +0 -18
- package/superstruct/dist/types.d.ts +0 -11
- package/superstruct/src/types.ts +0 -20
- package/typanion/dist/types.d.ts +0 -9
- package/typanion/src/types.ts +0 -20
- package/typebox/dist/types.d.ts +0 -4
- package/typebox/src/types.ts +0 -11
- package/valibot/dist/types.d.ts +0 -13
- package/valibot/src/types.ts +0 -34
- package/vine/dist/types.d.ts +0 -6
- package/vine/src/types.ts +0 -13
- package/zod/dist/types.d.ts +0 -13
- package/zod/src/types.ts +0 -22
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{toNestErrors as e,validateFieldsNatively as r}from"@hookform/resolvers";function n(){return n=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var n=arguments[r];for(var t in n)({}).hasOwnProperty.call(n,t)&&(e[t]=n[t])}return e},n.apply(null,arguments)}function t(t,s){return void 0===s&&(s={}),function(a,i,o){try{var u=function(){if(l.issues){var t=function(e,r){for(var t={},s=0;s<e.length;s++){var a,i=e[s],o=null==(a=i.path)?void 0:a.join(".");if(o&&(t[o]||(t[o]={message:i.message,type:""}),r)){var u,l=t[o].types||{};t[o].types=n({},l,((u={})[Object.keys(l).length]=i.message,u))}}return t}(l.issues,!o.shouldUseNativeValidation&&"all"===o.criteriaMode);return{values:{},errors:e(t,o)}}return o.shouldUseNativeValidation&&r({},o),{values:s.raw?Object.assign({},a):l.value,errors:{}}},l=t["~standard"].validate(a),v=function(){if(l instanceof Promise)return Promise.resolve(l).then(function(e){l=e})}();return Promise.resolve(v&&v.then?v.then(u):u())}catch(e){return Promise.reject(e)}}}export{t as standardSchemaResolver};
|
|
2
|
+
//# sourceMappingURL=standard-schema.module.js.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{toNestErrors as e,validateFieldsNatively as s}from"@hookform/resolvers";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var s=1;s<arguments.length;s++){var t=arguments[s];for(var r in t)({}).hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},t.apply(null,arguments)}function r(r,n={}){return async(a,o,i)=>{let l=r["~standard"].validate(a);if(l instanceof Promise&&(l=await l),l.issues){const s=function(e,s){const r={};for(let a=0;a<e.length;a++){var n;const o=e[a],i=null==(n=o.path)?void 0:n.join(".");if(i&&(r[i]||(r[i]={message:o.message,type:""}),s)){const e=r[i].types||{};r[i].types=t({},e,{[Object.keys(e).length]:o.message})}}return r}(l.issues,!i.shouldUseNativeValidation&&"all"===i.criteriaMode);return{values:{},errors:e(s,i)}}return i.shouldUseNativeValidation&&s({},i),{values:n.raw?Object.assign({},a):l.value,errors:{}}}}export{r as standardSchemaResolver};
|
|
2
|
+
//# sourceMappingURL=standard-schema.modern.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard-schema.modern.mjs","sources":["../src/standard-schema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { StandardSchemaV1 } from '@standard-schema/spec';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction parseIssues(\n issues: readonly StandardSchemaV1.Issue[],\n validateAllFieldCriteria: boolean,\n) {\n const errors: Record<string, FieldError> = {};\n\n for (let i = 0; i < issues.length; i++) {\n const error = issues[i];\n const path = error.path?.join('.');\n\n if (path) {\n if (!errors[path]) {\n errors[path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[path].types || {};\n\n errors[path].types = {\n ...types,\n [Object.keys(types).length]: error.message,\n };\n }\n }\n }\n\n return errors;\n}\n\n/**\n * Creates a resolver for react-hook-form that validates data using a Standard Schema.\n *\n * @param {Schema} schema - The Standard Schema to validate against\n * @param {Object} resolverOptions - Options for the resolver\n * @param {boolean} [resolverOptions.raw=false] - Whether to return raw input values instead of parsed values\n * @returns {Resolver} A resolver function compatible with react-hook-form\n *\n * @example\n * ```ts\n * const schema = z.object({\n * name: z.string().min(2),\n * age: z.number().min(18)\n * });\n *\n * useForm({\n * resolver: standardSchemaResolver(schema)\n * });\n * ```\n */\nexport function standardSchemaResolver<\n TFieldValues extends FieldValues,\n Schema extends StandardSchemaV1<TFieldValues, any>,\n>(\n schema: Schema,\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<NonNullable<(typeof schema)['~standard']['types']>['output']> {\n return async (values: TFieldValues, _, options) => {\n let result = schema['~standard'].validate(values);\n if (result instanceof Promise) {\n result = await result;\n }\n\n if (result.issues) {\n const errors = parseIssues(\n result.issues,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n );\n\n return {\n values: {},\n errors: toNestErrors(errors, options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result.value,\n errors: {},\n };\n };\n}\n"],"names":["standardSchemaResolver","schema","resolverOptions","values","_","options","result","validate","Promise","issues","errors","validateAllFieldCriteria","i","length","_error$path","error","path","join","message","type","types","_extends","Object","keys","parseIssues","shouldUseNativeValidation","criteriaMode","toNestErrors","validateFieldsNatively","raw","assign","value"],"mappings":"uSAqDgB,SAAAA,EAIdC,EACAC,EAEI,CAAA,GAEJ,OAAcC,MAAAA,EAAsBC,EAAGC,KACrC,IAAIC,EAASL,EAAO,aAAaM,SAASJ,GAK1C,GAJIG,aAAkBE,UACpBF,QAAeA,GAGbA,EAAOG,OAAQ,CACjB,MAAMC,EAjEZ,SACED,EACAE,GAEA,MAAMD,EAAqC,CAAE,EAE7C,IAAK,IAAIE,EAAI,EAAGA,EAAIH,EAAOI,OAAQD,IAAK,CAAA,IAAAE,EACtC,MAAMC,EAAQN,EAAOG,GACfI,EAAOF,OAAHA,EAAGC,EAAMC,WAANF,EAAAA,EAAYG,KAAK,KAE9B,GAAID,IACGN,EAAOM,KACVN,EAAOM,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAM,KAG7CR,GAA0B,CAC5B,MAAMS,EAAQV,EAAOM,GAAMI,OAAS,CAAA,EAEpCV,EAAOM,GAAMI,MAAKC,EAAA,CAAA,EACbD,EAAK,CACR,CAACE,OAAOC,KAAKH,GAAOP,QAASE,EAAMG,SAEvC,CAEJ,CAEA,OAAOR,CACT,CAsCqBc,CACblB,EAAOG,QACNJ,EAAQoB,2BAAsD,QAAzBpB,EAAQqB,cAGhD,MAAO,CACLvB,OAAQ,CAAA,EACRO,OAAQiB,EAAajB,EAAQL,GAEjC,CAIA,OAFAA,EAAQoB,2BAA6BG,EAAuB,CAAE,EAAEvB,GAEzD,CACLF,OAAQD,EAAgB2B,IAAMP,OAAOQ,OAAO,CAAE,EAAE3B,GAAUG,EAAOyB,MACjErB,OAAQ,CAAA,GAGd"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{toNestErrors as e,validateFieldsNatively as r}from"@hookform/resolvers";function n(){return n=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var n=arguments[r];for(var t in n)({}).hasOwnProperty.call(n,t)&&(e[t]=n[t])}return e},n.apply(null,arguments)}function t(t,s){return void 0===s&&(s={}),function(a,i,o){try{var u=function(){if(l.issues){var t=function(e,r){for(var t={},s=0;s<e.length;s++){var a,i=e[s],o=null==(a=i.path)?void 0:a.join(".");if(o&&(t[o]||(t[o]={message:i.message,type:""}),r)){var u,l=t[o].types||{};t[o].types=n({},l,((u={})[Object.keys(l).length]=i.message,u))}}return t}(l.issues,!o.shouldUseNativeValidation&&"all"===o.criteriaMode);return{values:{},errors:e(t,o)}}return o.shouldUseNativeValidation&&r({},o),{values:s.raw?Object.assign({},a):l.value,errors:{}}},l=t["~standard"].validate(a),v=function(){if(l instanceof Promise)return Promise.resolve(l).then(function(e){l=e})}();return Promise.resolve(v&&v.then?v.then(u):u())}catch(e){return Promise.reject(e)}}}export{t as standardSchemaResolver};
|
|
2
|
+
//# sourceMappingURL=standard-schema.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard-schema.module.js","sources":["../src/standard-schema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { StandardSchemaV1 } from '@standard-schema/spec';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction parseIssues(\n issues: readonly StandardSchemaV1.Issue[],\n validateAllFieldCriteria: boolean,\n) {\n const errors: Record<string, FieldError> = {};\n\n for (let i = 0; i < issues.length; i++) {\n const error = issues[i];\n const path = error.path?.join('.');\n\n if (path) {\n if (!errors[path]) {\n errors[path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[path].types || {};\n\n errors[path].types = {\n ...types,\n [Object.keys(types).length]: error.message,\n };\n }\n }\n }\n\n return errors;\n}\n\n/**\n * Creates a resolver for react-hook-form that validates data using a Standard Schema.\n *\n * @param {Schema} schema - The Standard Schema to validate against\n * @param {Object} resolverOptions - Options for the resolver\n * @param {boolean} [resolverOptions.raw=false] - Whether to return raw input values instead of parsed values\n * @returns {Resolver} A resolver function compatible with react-hook-form\n *\n * @example\n * ```ts\n * const schema = z.object({\n * name: z.string().min(2),\n * age: z.number().min(18)\n * });\n *\n * useForm({\n * resolver: standardSchemaResolver(schema)\n * });\n * ```\n */\nexport function standardSchemaResolver<\n TFieldValues extends FieldValues,\n Schema extends StandardSchemaV1<TFieldValues, any>,\n>(\n schema: Schema,\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<NonNullable<(typeof schema)['~standard']['types']>['output']> {\n return async (values: TFieldValues, _, options) => {\n let result = schema['~standard'].validate(values);\n if (result instanceof Promise) {\n result = await result;\n }\n\n if (result.issues) {\n const errors = parseIssues(\n result.issues,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n );\n\n return {\n values: {},\n errors: toNestErrors(errors, options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result.value,\n errors: {},\n };\n };\n}\n"],"names":["standardSchemaResolver","schema","resolverOptions","values","_","options","_temp2","result","issues","errors","validateAllFieldCriteria","i","length","_error$path","error","path","join","message","type","_extends2","types","_extends","Object","keys","parseIssues","shouldUseNativeValidation","criteriaMode","toNestErrors","validateFieldsNatively","raw","assign","value","validate","_temp","Promise","resolve","then","_result","e","reject"],"mappings":"gTAqDgBA,EAIdC,EACAC,GAIA,YAJAA,IAAAA,IAAAA,EAEI,CAAE,GAEN,SAAcC,EAAsBC,EAAGC,GAAW,IAAA,IAAAC,EAAAA,WAMhD,GAAIC,EAAOC,OAAQ,CACjB,IAAMC,EAjEZ,SACED,EACAE,GAIA,IAFA,IAAMD,EAAqC,CAAA,EAElCE,EAAI,EAAGA,EAAIH,EAAOI,OAAQD,IAAK,CAAAE,IAAAA,EAChCC,EAAQN,EAAOG,GACfI,EAAOF,OAAHA,EAAGC,EAAMC,WAANF,EAAAA,EAAYG,KAAK,KAE9B,GAAID,IACGN,EAAOM,KACVN,EAAOM,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAM,KAG7CR,GAA0B,CAAA,IAAAS,EACtBC,EAAQX,EAAOM,GAAMK,OAAS,CAAA,EAEpCX,EAAOM,GAAMK,MAAKC,EACbD,CAAAA,EAAAA,IAAKD,EAAA,IACPG,OAAOC,KAAKH,GAAOR,QAASE,EAAMG,QAAOE,GAE9C,CAEJ,CAEA,OAAOV,CACT,CAsCqBe,CACbjB,EAAOC,QACNH,EAAQoB,2BAAsD,QAAzBpB,EAAQqB,cAGhD,MAAO,CACLvB,OAAQ,CAAA,EACRM,OAAQkB,EAAalB,EAAQJ,GAEjC,CAIA,OAFAA,EAAQoB,2BAA6BG,EAAuB,CAAE,EAAEvB,GAEzD,CACLF,OAAQD,EAAgB2B,IAAMP,OAAOQ,OAAO,CAAE,EAAE3B,GAAUI,EAAOwB,MACjEtB,OAAQ,GACR,EAtBEF,EAASN,EAAO,aAAa+B,SAAS7B,GAAQ8B,gBAC9C1B,aAAkB2B,QAAO,OAAAA,QAAAC,QACZ5B,GAAM6B,KAAA,SAAAC,GAArB9B,EAAM8B,CAAgB,EAAAH,IAAAA,OAAAA,QAAAC,QAAAF,GAAAA,EAAAG,KAAAH,EAAAG,KAAA9B,GAAAA,IAqB1B,CAAC,MAAAgC,GAAAJ,OAAAA,QAAAK,OAAAD,EACH,CAAA,CAAA"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],r):r((e||self).hookformResolversStandardSchema={},e.hookformResolvers)}(this,function(e,r){function o(){return o=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var o=arguments[r];for(var t in o)({}).hasOwnProperty.call(o,t)&&(e[t]=o[t])}return e},o.apply(null,arguments)}e.standardSchemaResolver=function(e,t){return void 0===t&&(t={}),function(n,s,i){try{var a=function(){if(l.issues){var e=function(e,r){for(var t={},n=0;n<e.length;n++){var s,i=e[n],a=null==(s=i.path)?void 0:s.join(".");if(a&&(t[a]||(t[a]={message:i.message,type:""}),r)){var l,u=t[a].types||{};t[a].types=o({},u,((l={})[Object.keys(u).length]=i.message,l))}}return t}(l.issues,!i.shouldUseNativeValidation&&"all"===i.criteriaMode);return{values:{},errors:r.toNestErrors(e,i)}}return i.shouldUseNativeValidation&&r.validateFieldsNatively({},i),{values:t.raw?Object.assign({},n):l.value,errors:{}}},l=e["~standard"].validate(n),u=function(){if(l instanceof Promise)return Promise.resolve(l).then(function(e){l=e})}();return Promise.resolve(u&&u.then?u.then(a):a())}catch(e){return Promise.reject(e)}}}});
|
|
2
|
+
//# sourceMappingURL=standard-schema.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard-schema.umd.js","sources":["../src/standard-schema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { StandardSchemaV1 } from '@standard-schema/spec';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction parseIssues(\n issues: readonly StandardSchemaV1.Issue[],\n validateAllFieldCriteria: boolean,\n) {\n const errors: Record<string, FieldError> = {};\n\n for (let i = 0; i < issues.length; i++) {\n const error = issues[i];\n const path = error.path?.join('.');\n\n if (path) {\n if (!errors[path]) {\n errors[path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[path].types || {};\n\n errors[path].types = {\n ...types,\n [Object.keys(types).length]: error.message,\n };\n }\n }\n }\n\n return errors;\n}\n\n/**\n * Creates a resolver for react-hook-form that validates data using a Standard Schema.\n *\n * @param {Schema} schema - The Standard Schema to validate against\n * @param {Object} resolverOptions - Options for the resolver\n * @param {boolean} [resolverOptions.raw=false] - Whether to return raw input values instead of parsed values\n * @returns {Resolver} A resolver function compatible with react-hook-form\n *\n * @example\n * ```ts\n * const schema = z.object({\n * name: z.string().min(2),\n * age: z.number().min(18)\n * });\n *\n * useForm({\n * resolver: standardSchemaResolver(schema)\n * });\n * ```\n */\nexport function standardSchemaResolver<\n TFieldValues extends FieldValues,\n Schema extends StandardSchemaV1<TFieldValues, any>,\n>(\n schema: Schema,\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<NonNullable<(typeof schema)['~standard']['types']>['output']> {\n return async (values: TFieldValues, _, options) => {\n let result = schema['~standard'].validate(values);\n if (result instanceof Promise) {\n result = await result;\n }\n\n if (result.issues) {\n const errors = parseIssues(\n result.issues,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n );\n\n return {\n values: {},\n errors: toNestErrors(errors, options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result.value,\n errors: {},\n };\n };\n}\n"],"names":["schema","resolverOptions","values","_","options","_temp2","result","issues","errors","validateAllFieldCriteria","i","length","_error$path","error","path","join","message","type","_extends2","types","_extends","Object","keys","parseIssues","shouldUseNativeValidation","criteriaMode","toNestErrors","validateFieldsNatively","raw","assign","value","validate","_temp","Promise","resolve","then","_result","e","reject"],"mappings":"8jBAyDEA,EACAC,GAIA,YAJAA,IAAAA,IAAAA,EAEI,CAAE,GAEN,SAAcC,EAAsBC,EAAGC,GAAW,IAAA,IAAAC,EAAAA,WAMhD,GAAIC,EAAOC,OAAQ,CACjB,IAAMC,EAjEZ,SACED,EACAE,GAIA,IAFA,IAAMD,EAAqC,CAAA,EAElCE,EAAI,EAAGA,EAAIH,EAAOI,OAAQD,IAAK,CAAAE,IAAAA,EAChCC,EAAQN,EAAOG,GACfI,EAAOF,OAAHA,EAAGC,EAAMC,WAANF,EAAAA,EAAYG,KAAK,KAE9B,GAAID,IACGN,EAAOM,KACVN,EAAOM,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAM,KAG7CR,GAA0B,CAAA,IAAAS,EACtBC,EAAQX,EAAOM,GAAMK,OAAS,CAAA,EAEpCX,EAAOM,GAAMK,MAAKC,EACbD,CAAAA,EAAAA,IAAKD,EAAA,IACPG,OAAOC,KAAKH,GAAOR,QAASE,EAAMG,QAAOE,GAE9C,CAEJ,CAEA,OAAOV,CACT,CAsCqBe,CACbjB,EAAOC,QACNH,EAAQoB,2BAAsD,QAAzBpB,EAAQqB,cAGhD,MAAO,CACLvB,OAAQ,CAAA,EACRM,OAAQkB,EAAYA,aAAClB,EAAQJ,GAEjC,CAIA,OAFAA,EAAQoB,2BAA6BG,EAAAA,uBAAuB,CAAE,EAAEvB,GAEzD,CACLF,OAAQD,EAAgB2B,IAAMP,OAAOQ,OAAO,CAAE,EAAE3B,GAAUI,EAAOwB,MACjEtB,OAAQ,GACR,EAtBEF,EAASN,EAAO,aAAa+B,SAAS7B,GAAQ8B,gBAC9C1B,aAAkB2B,QAAO,OAAAA,QAAAC,QACZ5B,GAAM6B,KAAA,SAAAC,GAArB9B,EAAM8B,CAAgB,EAAAH,IAAAA,OAAAA,QAAAC,QAAAF,GAAAA,EAAAG,KAAAH,EAAAG,KAAA9B,GAAAA,IAqB1B,CAAC,MAAAgC,GAAAJ,OAAAA,QAAAK,OAAAD,EACH,CAAA,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hookform/resolvers/standard-schema",
|
|
3
|
+
"amdName": "hookformResolversStandardSchema",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"private": true,
|
|
6
|
+
"description": "React Hook Form validation resolver: standard-schema",
|
|
7
|
+
"main": "dist/standard-schema.js",
|
|
8
|
+
"module": "dist/standard-schema.module.js",
|
|
9
|
+
"umd:main": "dist/standard-schema.umd.js",
|
|
10
|
+
"source": "src/index.ts",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"react-hook-form": "^7.0.0",
|
|
15
|
+
"@standard-schema/spec": "^1.0.0",
|
|
16
|
+
"@hookform/resolvers": "^2.0.0"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import user from '@testing-library/user-event';
|
|
3
|
+
import { type } from 'arktype';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { useForm } from 'react-hook-form';
|
|
6
|
+
import { standardSchemaResolver } from '..';
|
|
7
|
+
|
|
8
|
+
const schema = type({
|
|
9
|
+
username: 'string>1',
|
|
10
|
+
password: 'string>1',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
type FormData = typeof schema.infer;
|
|
14
|
+
|
|
15
|
+
interface Props {
|
|
16
|
+
onSubmit: (data: FormData) => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function TestComponent({ onSubmit }: Props) {
|
|
20
|
+
const { register, handleSubmit } = useForm<FormData>({
|
|
21
|
+
resolver: standardSchemaResolver(schema),
|
|
22
|
+
shouldUseNativeValidation: true,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
27
|
+
<input {...register('username')} placeholder="username" />
|
|
28
|
+
|
|
29
|
+
<input {...register('password')} placeholder="password" />
|
|
30
|
+
|
|
31
|
+
<button type="submit">submit</button>
|
|
32
|
+
</form>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
test("form's native validation with arkType", async () => {
|
|
37
|
+
const handleSubmit = vi.fn();
|
|
38
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
39
|
+
|
|
40
|
+
// username
|
|
41
|
+
let usernameField = screen.getByPlaceholderText(
|
|
42
|
+
/username/i,
|
|
43
|
+
) as HTMLInputElement;
|
|
44
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
45
|
+
expect(usernameField.validationMessage).toBe('');
|
|
46
|
+
|
|
47
|
+
// password
|
|
48
|
+
let passwordField = screen.getByPlaceholderText(
|
|
49
|
+
/password/i,
|
|
50
|
+
) as HTMLInputElement;
|
|
51
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
52
|
+
expect(passwordField.validationMessage).toBe('');
|
|
53
|
+
|
|
54
|
+
await user.click(screen.getByText(/submit/i));
|
|
55
|
+
|
|
56
|
+
// username
|
|
57
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
58
|
+
expect(usernameField.validity.valid).toBe(false);
|
|
59
|
+
expect(usernameField.validationMessage).toBe(
|
|
60
|
+
'username must be at least length 2',
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
// password
|
|
64
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
65
|
+
expect(passwordField.validity.valid).toBe(false);
|
|
66
|
+
expect(passwordField.validationMessage).toBe(
|
|
67
|
+
'password must be at least length 2',
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
await user.type(screen.getByPlaceholderText(/username/i), 'joe');
|
|
71
|
+
await user.type(screen.getByPlaceholderText(/password/i), 'password');
|
|
72
|
+
|
|
73
|
+
// username
|
|
74
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
75
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
76
|
+
expect(usernameField.validationMessage).toBe('');
|
|
77
|
+
|
|
78
|
+
// password
|
|
79
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
80
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
81
|
+
expect(passwordField.validationMessage).toBe('');
|
|
82
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import user from '@testing-library/user-event';
|
|
3
|
+
import { type } from 'arktype';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { useForm } from 'react-hook-form';
|
|
6
|
+
import { standardSchemaResolver } from '..';
|
|
7
|
+
|
|
8
|
+
const schema = type({
|
|
9
|
+
username: 'string>1',
|
|
10
|
+
password: 'string>1',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
type FormData = typeof schema.infer & { unusedProperty: string };
|
|
14
|
+
|
|
15
|
+
function TestComponent({
|
|
16
|
+
onSubmit,
|
|
17
|
+
}: {
|
|
18
|
+
onSubmit: (data: typeof schema.infer) => void;
|
|
19
|
+
}) {
|
|
20
|
+
const {
|
|
21
|
+
register,
|
|
22
|
+
handleSubmit,
|
|
23
|
+
formState: { errors },
|
|
24
|
+
} = useForm({
|
|
25
|
+
resolver: standardSchemaResolver(schema), // Useful to check TypeScript regressions
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
30
|
+
<input {...register('username')} />
|
|
31
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
32
|
+
|
|
33
|
+
<input {...register('password')} />
|
|
34
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
35
|
+
|
|
36
|
+
<button type="submit">submit</button>
|
|
37
|
+
</form>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
test("form's validation with arkType and TypeScript's integration", async () => {
|
|
42
|
+
const handleSubmit = vi.fn();
|
|
43
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
44
|
+
|
|
45
|
+
expect(screen.queryAllByRole('alert')).toHaveLength(0);
|
|
46
|
+
|
|
47
|
+
await user.click(screen.getByText(/submit/i));
|
|
48
|
+
|
|
49
|
+
expect(
|
|
50
|
+
screen.getByText('username must be at least length 2'),
|
|
51
|
+
).toBeInTheDocument();
|
|
52
|
+
expect(
|
|
53
|
+
screen.getByText('password must be at least length 2'),
|
|
54
|
+
).toBeInTheDocument();
|
|
55
|
+
expect(handleSubmit).not.toHaveBeenCalled();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export function TestComponentManualType({
|
|
59
|
+
onSubmit,
|
|
60
|
+
}: {
|
|
61
|
+
onSubmit: (data: FormData) => void;
|
|
62
|
+
}) {
|
|
63
|
+
const {
|
|
64
|
+
register,
|
|
65
|
+
handleSubmit,
|
|
66
|
+
formState: { errors },
|
|
67
|
+
} = useForm<typeof schema.infer, undefined, FormData>({
|
|
68
|
+
resolver: standardSchemaResolver(schema), // Useful to check TypeScript regressions
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
73
|
+
<input {...register('username')} />
|
|
74
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
75
|
+
|
|
76
|
+
<input {...register('password')} />
|
|
77
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
78
|
+
|
|
79
|
+
<button type="submit">submit</button>
|
|
80
|
+
</form>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
export const schema = z
|
|
5
|
+
.object({
|
|
6
|
+
username: z.string().regex(/^\w+$/).min(3).max(30),
|
|
7
|
+
password: z
|
|
8
|
+
.string()
|
|
9
|
+
.regex(new RegExp('.*[A-Z].*'), 'One uppercase character')
|
|
10
|
+
.regex(new RegExp('.*[a-z].*'), 'One lowercase character')
|
|
11
|
+
.regex(new RegExp('.*\\d.*'), 'One number')
|
|
12
|
+
.regex(
|
|
13
|
+
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
|
|
14
|
+
'One special character',
|
|
15
|
+
)
|
|
16
|
+
.min(8, 'Must be at least 8 characters in length'),
|
|
17
|
+
repeatPassword: z.string(),
|
|
18
|
+
accessToken: z.union([z.string(), z.number()]),
|
|
19
|
+
birthYear: z.number().min(1900).max(2013).optional(),
|
|
20
|
+
email: z.string().email().optional(),
|
|
21
|
+
tags: z.array(z.string()),
|
|
22
|
+
enabled: z.boolean(),
|
|
23
|
+
url: z.string().url('Custom error url').or(z.literal('')),
|
|
24
|
+
like: z
|
|
25
|
+
.array(
|
|
26
|
+
z.object({
|
|
27
|
+
id: z.number(),
|
|
28
|
+
name: z.string().length(4),
|
|
29
|
+
}),
|
|
30
|
+
)
|
|
31
|
+
.optional(),
|
|
32
|
+
dateStr: z
|
|
33
|
+
.string()
|
|
34
|
+
.transform((value) => new Date(value))
|
|
35
|
+
.refine((value) => !isNaN(value.getTime()), {
|
|
36
|
+
message: 'Invalid date',
|
|
37
|
+
}),
|
|
38
|
+
})
|
|
39
|
+
.refine((obj) => obj.password === obj.repeatPassword, {
|
|
40
|
+
message: 'Passwords do not match',
|
|
41
|
+
path: ['confirm'],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export const validData = {
|
|
45
|
+
username: 'Doe',
|
|
46
|
+
password: 'Password123_',
|
|
47
|
+
repeatPassword: 'Password123_',
|
|
48
|
+
birthYear: 2000,
|
|
49
|
+
email: 'john@doe.com',
|
|
50
|
+
tags: ['tag1', 'tag2'],
|
|
51
|
+
enabled: true,
|
|
52
|
+
accessToken: 'accessToken',
|
|
53
|
+
url: 'https://react-hook-form.com/',
|
|
54
|
+
like: [
|
|
55
|
+
{
|
|
56
|
+
id: 1,
|
|
57
|
+
name: 'name',
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
dateStr: '2020-01-01T00:00:00.000Z',
|
|
61
|
+
} as any as z.infer<typeof schema>;
|
|
62
|
+
|
|
63
|
+
export const invalidData = {
|
|
64
|
+
password: '___',
|
|
65
|
+
email: '',
|
|
66
|
+
birthYear: 'birthYear',
|
|
67
|
+
like: [{ id: 'z' }],
|
|
68
|
+
url: 'abc',
|
|
69
|
+
} as any as z.infer<typeof schema>;
|
|
70
|
+
|
|
71
|
+
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
72
|
+
username: {
|
|
73
|
+
ref: { name: 'username' },
|
|
74
|
+
name: 'username',
|
|
75
|
+
},
|
|
76
|
+
password: {
|
|
77
|
+
ref: { name: 'password' },
|
|
78
|
+
name: 'password',
|
|
79
|
+
},
|
|
80
|
+
email: {
|
|
81
|
+
ref: { name: 'email' },
|
|
82
|
+
name: 'email',
|
|
83
|
+
},
|
|
84
|
+
birthday: {
|
|
85
|
+
ref: { name: 'birthday' },
|
|
86
|
+
name: 'birthday',
|
|
87
|
+
},
|
|
88
|
+
};
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`standardSchemaResolver > should return a single error from standardSchemaResolver when validation fails 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"errors": {
|
|
6
|
+
"accessToken": {
|
|
7
|
+
"message": "Invalid input",
|
|
8
|
+
"ref": undefined,
|
|
9
|
+
"type": "",
|
|
10
|
+
},
|
|
11
|
+
"birthYear": {
|
|
12
|
+
"message": "Expected number, received string",
|
|
13
|
+
"ref": undefined,
|
|
14
|
+
"type": "",
|
|
15
|
+
},
|
|
16
|
+
"dateStr": {
|
|
17
|
+
"message": "Required",
|
|
18
|
+
"ref": undefined,
|
|
19
|
+
"type": "",
|
|
20
|
+
},
|
|
21
|
+
"email": {
|
|
22
|
+
"message": "Invalid email",
|
|
23
|
+
"ref": {
|
|
24
|
+
"name": "email",
|
|
25
|
+
},
|
|
26
|
+
"type": "",
|
|
27
|
+
},
|
|
28
|
+
"enabled": {
|
|
29
|
+
"message": "Required",
|
|
30
|
+
"ref": undefined,
|
|
31
|
+
"type": "",
|
|
32
|
+
},
|
|
33
|
+
"like": [
|
|
34
|
+
{
|
|
35
|
+
"id": {
|
|
36
|
+
"message": "Expected number, received string",
|
|
37
|
+
"ref": undefined,
|
|
38
|
+
"type": "",
|
|
39
|
+
},
|
|
40
|
+
"name": {
|
|
41
|
+
"message": "Required",
|
|
42
|
+
"ref": undefined,
|
|
43
|
+
"type": "",
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
"password": {
|
|
48
|
+
"message": "One uppercase character",
|
|
49
|
+
"ref": {
|
|
50
|
+
"name": "password",
|
|
51
|
+
},
|
|
52
|
+
"type": "",
|
|
53
|
+
},
|
|
54
|
+
"repeatPassword": {
|
|
55
|
+
"message": "Required",
|
|
56
|
+
"ref": undefined,
|
|
57
|
+
"type": "",
|
|
58
|
+
},
|
|
59
|
+
"tags": {
|
|
60
|
+
"message": "Required",
|
|
61
|
+
"ref": undefined,
|
|
62
|
+
"type": "",
|
|
63
|
+
},
|
|
64
|
+
"url": {
|
|
65
|
+
"message": "Custom error url",
|
|
66
|
+
"ref": undefined,
|
|
67
|
+
"type": "",
|
|
68
|
+
},
|
|
69
|
+
"username": {
|
|
70
|
+
"message": "Required",
|
|
71
|
+
"ref": {
|
|
72
|
+
"name": "username",
|
|
73
|
+
},
|
|
74
|
+
"type": "",
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
"values": {},
|
|
78
|
+
}
|
|
79
|
+
`;
|
|
80
|
+
|
|
81
|
+
exports[`standardSchemaResolver > should return all the errors from standardSchemaResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
|
82
|
+
{
|
|
83
|
+
"errors": {
|
|
84
|
+
"accessToken": {
|
|
85
|
+
"message": "Invalid input",
|
|
86
|
+
"ref": undefined,
|
|
87
|
+
"type": "",
|
|
88
|
+
"types": {
|
|
89
|
+
"0": "Invalid input",
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
"birthYear": {
|
|
93
|
+
"message": "Expected number, received string",
|
|
94
|
+
"ref": undefined,
|
|
95
|
+
"type": "",
|
|
96
|
+
"types": {
|
|
97
|
+
"0": "Expected number, received string",
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
"dateStr": {
|
|
101
|
+
"message": "Required",
|
|
102
|
+
"ref": undefined,
|
|
103
|
+
"type": "",
|
|
104
|
+
"types": {
|
|
105
|
+
"0": "Required",
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
"email": {
|
|
109
|
+
"message": "Invalid email",
|
|
110
|
+
"ref": {
|
|
111
|
+
"name": "email",
|
|
112
|
+
},
|
|
113
|
+
"type": "",
|
|
114
|
+
"types": {
|
|
115
|
+
"0": "Invalid email",
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
"enabled": {
|
|
119
|
+
"message": "Required",
|
|
120
|
+
"ref": undefined,
|
|
121
|
+
"type": "",
|
|
122
|
+
"types": {
|
|
123
|
+
"0": "Required",
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
"like": [
|
|
127
|
+
{
|
|
128
|
+
"id": {
|
|
129
|
+
"message": "Expected number, received string",
|
|
130
|
+
"ref": undefined,
|
|
131
|
+
"type": "",
|
|
132
|
+
"types": {
|
|
133
|
+
"0": "Expected number, received string",
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
"name": {
|
|
137
|
+
"message": "Required",
|
|
138
|
+
"ref": undefined,
|
|
139
|
+
"type": "",
|
|
140
|
+
"types": {
|
|
141
|
+
"0": "Required",
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
"password": {
|
|
147
|
+
"message": "One uppercase character",
|
|
148
|
+
"ref": {
|
|
149
|
+
"name": "password",
|
|
150
|
+
},
|
|
151
|
+
"type": "",
|
|
152
|
+
"types": {
|
|
153
|
+
"0": "One uppercase character",
|
|
154
|
+
"1": "One lowercase character",
|
|
155
|
+
"2": "One number",
|
|
156
|
+
"3": "Must be at least 8 characters in length",
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
"repeatPassword": {
|
|
160
|
+
"message": "Required",
|
|
161
|
+
"ref": undefined,
|
|
162
|
+
"type": "",
|
|
163
|
+
"types": {
|
|
164
|
+
"0": "Required",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
"tags": {
|
|
168
|
+
"message": "Required",
|
|
169
|
+
"ref": undefined,
|
|
170
|
+
"type": "",
|
|
171
|
+
"types": {
|
|
172
|
+
"0": "Required",
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
"url": {
|
|
176
|
+
"message": "Custom error url",
|
|
177
|
+
"ref": undefined,
|
|
178
|
+
"type": "",
|
|
179
|
+
"types": {
|
|
180
|
+
"0": "Custom error url",
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
"username": {
|
|
184
|
+
"message": "Required",
|
|
185
|
+
"ref": {
|
|
186
|
+
"name": "username",
|
|
187
|
+
},
|
|
188
|
+
"type": "",
|
|
189
|
+
"types": {
|
|
190
|
+
"0": "Required",
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
"values": {},
|
|
195
|
+
}
|
|
196
|
+
`;
|
|
197
|
+
|
|
198
|
+
exports[`standardSchemaResolver > should return values from standardSchemaResolver when validation pass & raw=true 1`] = `
|
|
199
|
+
{
|
|
200
|
+
"errors": {},
|
|
201
|
+
"values": {
|
|
202
|
+
"accessToken": "accessToken",
|
|
203
|
+
"birthYear": 2000,
|
|
204
|
+
"dateStr": "2020-01-01T00:00:00.000Z",
|
|
205
|
+
"email": "john@doe.com",
|
|
206
|
+
"enabled": true,
|
|
207
|
+
"like": [
|
|
208
|
+
{
|
|
209
|
+
"id": 1,
|
|
210
|
+
"name": "name",
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
"password": "Password123_",
|
|
214
|
+
"repeatPassword": "Password123_",
|
|
215
|
+
"tags": [
|
|
216
|
+
"tag1",
|
|
217
|
+
"tag2",
|
|
218
|
+
],
|
|
219
|
+
"url": "https://react-hook-form.com/",
|
|
220
|
+
"username": "Doe",
|
|
221
|
+
},
|
|
222
|
+
}
|
|
223
|
+
`;
|
|
224
|
+
|
|
225
|
+
exports[`standardSchemaResolver > should return values from standardSchemaResolver when validation pass 1`] = `
|
|
226
|
+
{
|
|
227
|
+
"errors": {},
|
|
228
|
+
"values": {
|
|
229
|
+
"accessToken": "accessToken",
|
|
230
|
+
"birthYear": 2000,
|
|
231
|
+
"dateStr": 2020-01-01T00:00:00.000Z,
|
|
232
|
+
"email": "john@doe.com",
|
|
233
|
+
"enabled": true,
|
|
234
|
+
"like": [
|
|
235
|
+
{
|
|
236
|
+
"id": 1,
|
|
237
|
+
"name": "name",
|
|
238
|
+
},
|
|
239
|
+
],
|
|
240
|
+
"password": "Password123_",
|
|
241
|
+
"repeatPassword": "Password123_",
|
|
242
|
+
"tags": [
|
|
243
|
+
"tag1",
|
|
244
|
+
"tag2",
|
|
245
|
+
],
|
|
246
|
+
"url": "https://react-hook-form.com/",
|
|
247
|
+
"username": "Doe",
|
|
248
|
+
},
|
|
249
|
+
}
|
|
250
|
+
`;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { standardSchemaResolver } from '..';
|
|
2
|
+
import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
|
3
|
+
|
|
4
|
+
const shouldUseNativeValidation = false;
|
|
5
|
+
|
|
6
|
+
describe('standardSchemaResolver', () => {
|
|
7
|
+
it('should return values from standardSchemaResolver when validation pass', async () => {
|
|
8
|
+
const result = await standardSchemaResolver(schema)(validData, undefined, {
|
|
9
|
+
fields,
|
|
10
|
+
shouldUseNativeValidation,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
expect(result).toMatchSnapshot();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should return a single error from standardSchemaResolver when validation fails', async () => {
|
|
17
|
+
const result = await standardSchemaResolver(schema)(
|
|
18
|
+
invalidData,
|
|
19
|
+
undefined,
|
|
20
|
+
{
|
|
21
|
+
fields,
|
|
22
|
+
shouldUseNativeValidation,
|
|
23
|
+
},
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
expect(result).toMatchSnapshot();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should return all the errors from standardSchemaResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
30
|
+
const result = await standardSchemaResolver(schema)(
|
|
31
|
+
invalidData,
|
|
32
|
+
undefined,
|
|
33
|
+
{
|
|
34
|
+
fields,
|
|
35
|
+
criteriaMode: 'all',
|
|
36
|
+
shouldUseNativeValidation,
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(result).toMatchSnapshot();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should return values from standardSchemaResolver when validation pass & raw=true', async () => {
|
|
44
|
+
const validateSpy = vi.spyOn(schema['~standard'], 'validate');
|
|
45
|
+
|
|
46
|
+
const result = await standardSchemaResolver(schema, {
|
|
47
|
+
raw: true,
|
|
48
|
+
})(validData, undefined, {
|
|
49
|
+
fields,
|
|
50
|
+
shouldUseNativeValidation,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
expect(validateSpy).toHaveBeenCalledTimes(1);
|
|
54
|
+
expect(result).toMatchSnapshot();
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './standard-schema';
|