@hookform/resolvers 4.0.0 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -2
- package/ajv/dist/ajv.d.ts +20 -0
- package/ajv/dist/ajv.js.map +1 -1
- package/ajv/dist/ajv.modern.mjs.map +1 -1
- package/ajv/dist/ajv.module.js.map +1 -1
- package/ajv/dist/ajv.umd.js.map +1 -1
- package/ajv/src/ajv.ts +20 -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/src/__tests__/Form.tsx +32 -6
- package/arktype/src/__tests__/__fixtures__/data.ts +1 -1
- package/arktype/src/arktype.ts +29 -9
- 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 +21 -10
- package/class-validator/src/class-validator.ts +46 -12
- 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/effect-ts/dist/effect-ts.d.ts +19 -2
- 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.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/effect-ts.ts +30 -8
- 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.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.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 +5 -1
- package/standard-schema/dist/index.d.ts +0 -1
- package/standard-schema/dist/standard-schema.d.ts +25 -2
- package/standard-schema/dist/standard-schema.js +1 -1
- package/standard-schema/dist/standard-schema.js.map +1 -1
- package/standard-schema/dist/standard-schema.mjs +1 -1
- package/standard-schema/dist/standard-schema.modern.mjs +1 -1
- package/standard-schema/dist/standard-schema.modern.mjs.map +1 -1
- package/standard-schema/dist/standard-schema.module.js +1 -1
- package/standard-schema/dist/standard-schema.module.js.map +1 -1
- package/standard-schema/dist/standard-schema.umd.js +1 -1
- package/standard-schema/dist/standard-schema.umd.js.map +1 -1
- package/standard-schema/package.json +1 -0
- package/standard-schema/src/__tests__/Form.tsx +32 -6
- package/standard-schema/src/__tests__/__fixtures__/data.ts +67 -21
- package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +222 -11
- package/standard-schema/src/__tests__/standard-schema.ts +49 -3
- package/standard-schema/src/index.ts +0 -1
- package/standard-schema/src/standard-schema.ts +54 -11
- package/superstruct/dist/index.d.ts +0 -1
- package/superstruct/dist/superstruct.d.ts +22 -2
- 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.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 +31 -9
- 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/index.ts +0 -1
- package/typebox/src/typebox.ts +33 -7
- package/typeschema/dist/typeschema.d.ts +17 -0
- package/typeschema/dist/typeschema.js.map +1 -1
- package/typeschema/dist/typeschema.modern.mjs.map +1 -1
- package/typeschema/dist/typeschema.module.js.map +1 -1
- package/typeschema/dist/typeschema.umd.js.map +1 -1
- package/typeschema/src/typeschema.ts +17 -0
- package/valibot/dist/index.d.ts +0 -1
- package/valibot/dist/valibot.d.ts +31 -2
- 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.map +1 -1
- package/valibot/src/__tests__/Form.tsx +26 -0
- package/valibot/src/__tests__/__fixtures__/data.ts +7 -3
- package/valibot/src/index.ts +0 -1
- package/valibot/src/valibot.ts +52 -5
- 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 +37 -9
- package/yup/dist/yup.d.ts +18 -7
- package/yup/dist/yup.js.map +1 -1
- package/yup/dist/yup.modern.mjs.map +1 -1
- package/yup/dist/yup.module.js.map +1 -1
- package/yup/dist/yup.umd.js.map +1 -1
- package/yup/src/yup.ts +21 -10
- 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 +39 -8
- 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/standard-schema/dist/types.d.ts +0 -3
- package/standard-schema/src/types.ts +0 -10
- 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
package/nope/dist/nope.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";var o=function
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";var o=function(r,e,t){return void 0===e&&(e={}),void 0===t&&(t=""),Object.keys(r).reduce(function(e,n){var a=t?t+"."+n:n,i=r[n];return"string"==typeof i?e[a]={message:i}:o(i,e,a),e},e)},t=function(t,n){return void 0===n&&(n={abortEarly:!1}),function(a,i,s){var u=t.validate(a,i,n);return u?{values:{},errors:r(o(u),s)}:(s.shouldUseNativeValidation&&e({},s),{values:a,errors:{}})}};export{t as nopeResolver};
|
|
2
2
|
//# sourceMappingURL=nope.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nope.module.js","sources":["../src/nope.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ShapeErrors } from 'nope-validator/lib/cjs/types';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (\n errors: ShapeErrors,\n parsedErrors: FieldErrors = {},\n path = '',\n) => {\n return Object.keys(errors).reduce((acc, key) => {\n const _path = path ? `${path}.${key}` : key;\n const error = errors[key];\n\n if (typeof error === 'string') {\n acc[_path] = {\n message: error,\n } as FieldError;\n } else {\n parseErrors(error, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n};\n\nexport const nopeResolver: Resolver =\n (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n ) =>\n (values, context, options) => {\n const result = schema.validate(values, context, schemaOptions) as\n | ShapeErrors\n | undefined;\n\n if (result) {\n return { values: {}, errors: toNestErrors(parseErrors(result), options) };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrors","errors","parsedErrors","path","Object","keys","reduce","acc","key","_path","error","message","nopeResolver","schema","schemaOptions","abortEarly","values","context","options","result","validate","toNestErrors","shouldUseNativeValidation","validateFieldsNatively"],"mappings":"+EAKA,IAAMA,EAAc,
|
|
1
|
+
{"version":3,"file":"nope.module.js","sources":["../src/nope.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ShapeErrors } from 'nope-validator/lib/cjs/types';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (\n errors: ShapeErrors,\n parsedErrors: FieldErrors = {},\n path = '',\n) => {\n return Object.keys(errors).reduce((acc, key) => {\n const _path = path ? `${path}.${key}` : key;\n const error = errors[key];\n\n if (typeof error === 'string') {\n acc[_path] = {\n message: error,\n } as FieldError;\n } else {\n parseErrors(error, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n};\n\n/**\n * Creates a resolver for react-hook-form using Nope schema validation\n * @param {NopeSchema} schema - The Nope schema to validate against\n * @param {NopeSchemaOptions} [schemaOptions] - Optional Nope validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver<NopeSchema>} A resolver function compatible with react-hook-form\n * @example\n * const schema = nope.object({\n * name: nope.string().required(),\n * age: nope.number().required()\n * });\n *\n * useForm({\n * resolver: nopeResolver(schema)\n * });\n */\nexport const nopeResolver: Resolver =\n (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n ) =>\n (values, context, options) => {\n const result = schema.validate(values, context, schemaOptions) as\n | ShapeErrors\n | undefined;\n\n if (result) {\n return { values: {}, errors: toNestErrors(parseErrors(result), options) };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrors","errors","parsedErrors","path","Object","keys","reduce","acc","key","_path","error","message","nopeResolver","schema","schemaOptions","abortEarly","values","context","options","result","validate","toNestErrors","shouldUseNativeValidation","validateFieldsNatively"],"mappings":"+EAKA,IAAMA,EAAc,SAClBC,EACAC,EACAC,GAEA,YAHAD,IAAAA,IAAAA,EAA4B,CAAE,QAC1B,IAAJC,IAAAA,EAAO,IAEAC,OAAOC,KAAKJ,GAAQK,OAAO,SAACC,EAAKC,GACtC,IAAMC,EAAQN,EAAUA,EAAI,IAAIK,EAAQA,EAClCE,EAAQT,EAAOO,GAUrB,MARqB,iBAAVE,EACTH,EAAIE,GAAS,CACXE,QAASD,GAGXV,EAAYU,EAAOH,EAAKE,GAGnBF,CACT,EAAGL,EACL,EAmBaU,EACX,SACEC,EACAC,GAEC,YAFY,IAAbA,IAAAA,EAAgB,CACdC,YAAY,aAGfC,EAAQC,EAASC,GAChB,IAAMC,EAASN,EAAOO,SAASJ,EAAQC,EAASH,GAIhD,OAAIK,EACK,CAAEH,OAAQ,CAAE,EAAEf,OAAQoB,EAAarB,EAAYmB,GAASD,KAGjEA,EAAQI,2BAA6BC,EAAuB,CAAE,EAAEL,GAEzD,CAAEF,OAAAA,EAAQf,OAAQ,IAC3B,CAAC"}
|
package/nope/dist/nope.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],o):o((e||self).hookformResolversNope={},e.hookformResolvers)}(this,function(e,o){var r=function
|
|
1
|
+
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],o):o((e||self).hookformResolversNope={},e.hookformResolvers)}(this,function(e,o){var r=function(e,o,t){return void 0===o&&(o={}),void 0===t&&(t=""),Object.keys(e).reduce(function(o,s){var n=t?t+"."+s:s,i=e[s];return"string"==typeof i?o[n]={message:i}:r(i,o,n),o},o)};e.nopeResolver=function(e,t){return void 0===t&&(t={abortEarly:!1}),function(s,n,i){var f=e.validate(s,n,t);return f?{values:{},errors:o.toNestErrors(r(f),i)}:(i.shouldUseNativeValidation&&o.validateFieldsNatively({},i),{values:s,errors:{}})}}});
|
|
2
2
|
//# sourceMappingURL=nope.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nope.umd.js","sources":["../src/nope.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ShapeErrors } from 'nope-validator/lib/cjs/types';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (\n errors: ShapeErrors,\n parsedErrors: FieldErrors = {},\n path = '',\n) => {\n return Object.keys(errors).reduce((acc, key) => {\n const _path = path ? `${path}.${key}` : key;\n const error = errors[key];\n\n if (typeof error === 'string') {\n acc[_path] = {\n message: error,\n } as FieldError;\n } else {\n parseErrors(error, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n};\n\nexport const nopeResolver: Resolver =\n (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n ) =>\n (values, context, options) => {\n const result = schema.validate(values, context, schemaOptions) as\n | ShapeErrors\n | undefined;\n\n if (result) {\n return { values: {}, errors: toNestErrors(parseErrors(result), options) };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrors","errors","parsedErrors","path","Object","keys","reduce","acc","key","_path","error","message","schema","schemaOptions","abortEarly","values","context","options","result","validate","toNestErrors","shouldUseNativeValidation","validateFieldsNatively"],"mappings":"0TAKA,IAAMA,EAAc,
|
|
1
|
+
{"version":3,"file":"nope.umd.js","sources":["../src/nope.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ShapeErrors } from 'nope-validator/lib/cjs/types';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (\n errors: ShapeErrors,\n parsedErrors: FieldErrors = {},\n path = '',\n) => {\n return Object.keys(errors).reduce((acc, key) => {\n const _path = path ? `${path}.${key}` : key;\n const error = errors[key];\n\n if (typeof error === 'string') {\n acc[_path] = {\n message: error,\n } as FieldError;\n } else {\n parseErrors(error, acc, _path);\n }\n\n return acc;\n }, parsedErrors);\n};\n\n/**\n * Creates a resolver for react-hook-form using Nope schema validation\n * @param {NopeSchema} schema - The Nope schema to validate against\n * @param {NopeSchemaOptions} [schemaOptions] - Optional Nope validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver<NopeSchema>} A resolver function compatible with react-hook-form\n * @example\n * const schema = nope.object({\n * name: nope.string().required(),\n * age: nope.number().required()\n * });\n *\n * useForm({\n * resolver: nopeResolver(schema)\n * });\n */\nexport const nopeResolver: Resolver =\n (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n ) =>\n (values, context, options) => {\n const result = schema.validate(values, context, schemaOptions) as\n | ShapeErrors\n | undefined;\n\n if (result) {\n return { values: {}, errors: toNestErrors(parseErrors(result), options) };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrors","errors","parsedErrors","path","Object","keys","reduce","acc","key","_path","error","message","schema","schemaOptions","abortEarly","values","context","options","result","validate","toNestErrors","shouldUseNativeValidation","validateFieldsNatively"],"mappings":"0TAKA,IAAMA,EAAc,SAClBC,EACAC,EACAC,GAEA,YAHAD,IAAAA,IAAAA,EAA4B,CAAE,QAC1B,IAAJC,IAAAA,EAAO,IAEAC,OAAOC,KAAKJ,GAAQK,OAAO,SAACC,EAAKC,GACtC,IAAMC,EAAQN,EAAUA,EAAI,IAAIK,EAAQA,EAClCE,EAAQT,EAAOO,GAUrB,MARqB,iBAAVE,EACTH,EAAIE,GAAS,CACXE,QAASD,GAGXV,EAAYU,EAAOH,EAAKE,GAGnBF,CACT,EAAGL,EACL,iBAoBE,SACEU,EACAC,GAEC,YAFY,IAAbA,IAAAA,EAAgB,CACdC,YAAY,aAGfC,EAAQC,EAASC,GAChB,IAAMC,EAASN,EAAOO,SAASJ,EAAQC,EAASH,GAIhD,OAAIK,EACK,CAAEH,OAAQ,CAAE,EAAEd,OAAQmB,EAAAA,aAAapB,EAAYkB,GAASD,KAGjEA,EAAQI,2BAA6BC,EAAsBA,uBAAC,CAAE,EAAEL,GAEzD,CAAEF,OAAAA,EAAQd,OAAQ,IAC3B,CAAC"}
|
package/nope/src/nope.ts
CHANGED
|
@@ -24,6 +24,23 @@ const parseErrors = (
|
|
|
24
24
|
}, parsedErrors);
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Creates a resolver for react-hook-form using Nope schema validation
|
|
29
|
+
* @param {NopeSchema} schema - The Nope schema to validate against
|
|
30
|
+
* @param {NopeSchemaOptions} [schemaOptions] - Optional Nope validation options
|
|
31
|
+
* @param {Object} resolverOptions - Additional resolver configuration
|
|
32
|
+
* @param {string} [resolverOptions.mode='async'] - Validation mode
|
|
33
|
+
* @returns {Resolver<NopeSchema>} A resolver function compatible with react-hook-form
|
|
34
|
+
* @example
|
|
35
|
+
* const schema = nope.object({
|
|
36
|
+
* name: nope.string().required(),
|
|
37
|
+
* age: nope.number().required()
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* useForm({
|
|
41
|
+
* resolver: nopeResolver(schema)
|
|
42
|
+
* });
|
|
43
|
+
*/
|
|
27
44
|
export const nopeResolver: Resolver =
|
|
28
45
|
(
|
|
29
46
|
schema,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hookform/resolvers",
|
|
3
3
|
"amdName": "hookformResolvers",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.1.1",
|
|
5
5
|
"description": "React Hook Form validation resolvers: Yup, Joi, Superstruct, Zod, Vest, Class Validator, io-ts, Nope, computed-types, TypeBox, arktype, Typanion, Effect-TS and VineJS",
|
|
6
6
|
"main": "dist/resolvers.js",
|
|
7
7
|
"module": "dist/resolvers.module.js",
|
|
@@ -269,6 +269,7 @@
|
|
|
269
269
|
"devDependencies": {
|
|
270
270
|
"@sinclair/typebox": "^0.34.15",
|
|
271
271
|
"@standard-schema/spec": "^1.0.0",
|
|
272
|
+
"@standard-schema/utils": "^0.3.0",
|
|
272
273
|
"@testing-library/dom": "^10.4.0",
|
|
273
274
|
"@testing-library/jest-dom": "^6.6.3",
|
|
274
275
|
"@testing-library/react": "^16.2.0",
|
|
@@ -318,5 +319,8 @@
|
|
|
318
319
|
},
|
|
319
320
|
"peerDependencies": {
|
|
320
321
|
"react-hook-form": "^7.0.0"
|
|
322
|
+
},
|
|
323
|
+
"dependencies": {
|
|
324
|
+
"caniuse-lite": "^1.0.30001698"
|
|
321
325
|
}
|
|
322
326
|
}
|
|
@@ -1,2 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { FieldValues, Resolver } from 'react-hook-form';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a resolver for react-hook-form that validates data using a Standard Schema.
|
|
5
|
+
*
|
|
6
|
+
* @param {Schema} schema - The Standard Schema to validate against
|
|
7
|
+
* @param {Object} resolverOptions - Options for the resolver
|
|
8
|
+
* @param {boolean} [resolverOptions.raw=false] - Whether to return raw input values instead of parsed values
|
|
9
|
+
* @returns {Resolver} A resolver function compatible with react-hook-form
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const schema = z.object({
|
|
14
|
+
* name: z.string().min(2),
|
|
15
|
+
* age: z.number().min(18)
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* useForm({
|
|
19
|
+
* resolver: standardSchemaResolver(schema)
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function standardSchemaResolver<Schema extends StandardSchemaV1<FieldValues>>(schema: Schema, resolverOptions?: {
|
|
24
|
+
raw?: boolean;
|
|
25
|
+
}): Resolver<StandardSchemaV1.InferOutput<Schema>>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("@hookform/resolvers");exports.standardSchemaResolver=function(
|
|
1
|
+
var e=require("@hookform/resolvers"),r=require("@standard-schema/utils");function t(){return t=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var s in t)({}).hasOwnProperty.call(t,s)&&(e[s]=t[s])}return e},t.apply(null,arguments)}exports.standardSchemaResolver=function(s,a){return void 0===a&&(a={}),function(n,i,o){try{var u=function(){if(l.issues){var s=function(e,s){for(var a={},n=0;n<e.length;n++){var i=e[n],o=r.getDotPath(i);if(o&&(a[o]||(a[o]={message:i.message,type:""}),s)){var u,l=a[o].types||{};a[o].types=t({},l,((u={})[Object.keys(l).length]=i.message,u))}}return a}(l.issues,!o.shouldUseNativeValidation&&"all"===o.criteriaMode);return{values:{},errors:e.toNestErrors(s,o)}}return o.shouldUseNativeValidation&&e.validateFieldsNatively({},o),{values:a.raw?Object.assign({},n):l.value,errors:{}}},l=s["~standard"].validate(n),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)}}};
|
|
2
2
|
//# sourceMappingURL=standard-schema.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standard-schema.js","sources":["../src/standard-schema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { StandardSchemaV1 } from '@standard-schema/spec';\nimport {
|
|
1
|
+
{"version":3,"file":"standard-schema.js","sources":["../src/standard-schema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { StandardSchemaV1 } from '@standard-schema/spec';\nimport { getDotPath } from '@standard-schema/utils';\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 = getDotPath(error);\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 Schema extends StandardSchemaV1<FieldValues>,\n>(\n schema: Schema,\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<StandardSchemaV1.InferOutput<Schema>> {\n return async (values, _, 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","getDotPath","message","type","_extends2","types","_extends","Object","keys","parseIssues","shouldUseNativeValidation","criteriaMode","toNestErrors","validateFieldsNatively","raw","assign","value","validate","_temp","Promise","resolve","then","_result","e","reject"],"mappings":"yUAyDEA,EACAC,GAIA,gBAJAA,IAAAA,EAEI,aAEUC,EAAQC,EAAGC,GAAW,IAAA,IAAAC,EAAA,WAMlC,GAAIC,EAAOC,OAAQ,CACjB,IAAMC,EAhEZ,SACED,EACAE,GAIA,IAFA,IAAMD,EAAqC,GAElCE,EAAI,EAAGA,EAAIH,EAAOI,OAAQD,IAAK,CACtC,IAAME,EAAQL,EAAOG,GACfG,EAAOC,EAAAA,WAAWF,GAExB,GAAIC,IACGL,EAAOK,KACVL,EAAOK,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAM,KAG7CP,GAA0B,CAAA,IAAAQ,EACtBC,EAAQV,EAAOK,GAAMK,OAAS,CAAE,EAEtCV,EAAOK,GAAMK,MAAKC,EACbD,CAAAA,EAAAA,IAAKD,MACPG,OAAOC,KAAKH,GAAOP,QAASC,EAAMG,QAAOE,GAE9C,CAEJ,CAEA,OAAOT,CACT,CAqCqBc,CACbhB,EAAOC,QACNH,EAAQmB,2BAAsD,QAAzBnB,EAAQoB,cAGhD,MAAO,CACLtB,OAAQ,CAAE,EACVM,OAAQiB,EAAAA,aAAajB,EAAQJ,GAEjC,CAIA,OAFAA,EAAQmB,2BAA6BG,EAAsBA,uBAAC,CAAE,EAAEtB,GAEzD,CACLF,OAAQD,EAAgB0B,IAAMP,OAAOQ,OAAO,CAAA,EAAI1B,GAAUI,EAAOuB,MACjErB,OAAQ,CAAA,EACR,EAtBEF,EAASN,EAAO,aAAa8B,SAAS5B,GAAQ6B,gBAC9CzB,aAAkB0B,QAAO,OAAAA,QAAAC,QACZ3B,GAAM4B,KAAA,SAAAC,GAArB7B,EAAM6B,CAAgB,EAAAH,IAAAA,OAAAA,QAAAC,QAAAF,GAAAA,EAAAG,KAAAH,EAAAG,KAAA7B,GAAAA,IAqB1B,CAAC,MAAA+B,GAAAJ,OAAAA,QAAAK,OAAAD,EACH,CAAA,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as
|
|
1
|
+
import{toNestErrors as e,validateFieldsNatively as r}from"@hookform/resolvers";import{getDotPath as t}from"@standard-schema/utils";function n(){return n=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},n.apply(null,arguments)}function s(s,a){return void 0===a&&(a={}),function(i,o,u){try{var l=function(){if(c.issues){var s=function(e,r){for(var s={},a=0;a<e.length;a++){var i=e[a],o=t(i);if(o&&(s[o]||(s[o]={message:i.message,type:""}),r)){var u,l=s[o].types||{};s[o].types=n({},l,((u={})[Object.keys(l).length]=i.message,u))}}return s}(c.issues,!u.shouldUseNativeValidation&&"all"===u.criteriaMode);return{values:{},errors:e(s,u)}}return u.shouldUseNativeValidation&&r({},u),{values:a.raw?Object.assign({},i):c.value,errors:{}}},c=s["~standard"].validate(i),f=function(){if(c instanceof Promise)return Promise.resolve(c).then(function(e){c=e})}();return Promise.resolve(f&&f.then?f.then(l):l())}catch(e){return Promise.reject(e)}}}export{s as standardSchemaResolver};
|
|
2
2
|
//# sourceMappingURL=standard-schema.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as
|
|
1
|
+
import{toNestErrors as e,validateFieldsNatively as s}from"@hookform/resolvers";import{getDotPath as t}from"@standard-schema/utils";function r(){return r=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},r.apply(null,arguments)}function a(a,n={}){return async(o,i,l)=>{let u=a["~standard"].validate(o);if(u instanceof Promise&&(u=await u),u.issues){const s=function(e,s){const a={};for(let n=0;n<e.length;n++){const o=e[n],i=t(o);if(i&&(a[i]||(a[i]={message:o.message,type:""}),s)){const e=a[i].types||{};a[i].types=r({},e,{[Object.keys(e).length]:o.message})}}return a}(u.issues,!l.shouldUseNativeValidation&&"all"===l.criteriaMode);return{values:{},errors:e(s,l)}}return l.shouldUseNativeValidation&&s({},l),{values:n.raw?Object.assign({},o):u.value,errors:{}}}}export{a as standardSchemaResolver};
|
|
2
2
|
//# sourceMappingURL=standard-schema.modern.mjs.map
|
|
@@ -1 +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 {
|
|
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 { getDotPath } from '@standard-schema/utils';\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 = getDotPath(error);\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 Schema extends StandardSchemaV1<FieldValues>,\n>(\n schema: Schema,\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<StandardSchemaV1.InferOutput<Schema>> {\n return async (values, _, 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","async","values","_","options","result","validate","Promise","issues","errors","validateAllFieldCriteria","i","length","error","path","getDotPath","message","type","types","_extends","Object","keys","parseIssues","shouldUseNativeValidation","criteriaMode","toNestErrors","validateFieldsNatively","raw","assign","value"],"mappings":"oWAsDgBA,EAGdC,EACAC,EAEI,CAAA,GAEJ,OAAOC,MAAOC,EAAQC,EAAGC,KACvB,IAAIC,EAASN,EAAO,aAAaO,SAASJ,GAK1C,GAJIG,aAAkBE,UACpBF,QAAeA,GAGbA,EAAOG,OAAQ,CACjB,MAAMC,EAhEZ,SACED,EACAE,GAEA,MAAMD,EAAqC,CAAA,EAE3C,IAAK,IAAIE,EAAI,EAAGA,EAAIH,EAAOI,OAAQD,IAAK,CACtC,MAAME,EAAQL,EAAOG,GACfG,EAAOC,EAAWF,GAExB,GAAIC,IACGL,EAAOK,KACVL,EAAOK,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAM,KAG7CP,GAA0B,CAC5B,MAAMQ,EAAQT,EAAOK,GAAMI,OAAS,CAAE,EAEtCT,EAAOK,GAAMI,MAAKC,EACbD,CAAAA,EAAAA,EACH,CAAA,CAACE,OAAOC,KAAKH,GAAON,QAASC,EAAMG,SAEvC,CAEJ,CAEA,OAAOP,CACT,CAqCqBa,CACbjB,EAAOG,QACNJ,EAAQmB,2BAAsD,QAAzBnB,EAAQoB,cAGhD,MAAO,CACLtB,OAAQ,CAAE,EACVO,OAAQgB,EAAahB,EAAQL,GAEjC,CAIA,OAFAA,EAAQmB,2BAA6BG,EAAuB,CAAE,EAAEtB,GAEzD,CACLF,OAAQF,EAAgB2B,IAAMP,OAAOQ,OAAO,CAAA,EAAI1B,GAAUG,EAAOwB,MACjEpB,OAAQ,IAGd"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as
|
|
1
|
+
import{toNestErrors as e,validateFieldsNatively as r}from"@hookform/resolvers";import{getDotPath as t}from"@standard-schema/utils";function n(){return n=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},n.apply(null,arguments)}function s(s,a){return void 0===a&&(a={}),function(i,o,u){try{var l=function(){if(c.issues){var s=function(e,r){for(var s={},a=0;a<e.length;a++){var i=e[a],o=t(i);if(o&&(s[o]||(s[o]={message:i.message,type:""}),r)){var u,l=s[o].types||{};s[o].types=n({},l,((u={})[Object.keys(l).length]=i.message,u))}}return s}(c.issues,!u.shouldUseNativeValidation&&"all"===u.criteriaMode);return{values:{},errors:e(s,u)}}return u.shouldUseNativeValidation&&r({},u),{values:a.raw?Object.assign({},i):c.value,errors:{}}},c=s["~standard"].validate(i),f=function(){if(c instanceof Promise)return Promise.resolve(c).then(function(e){c=e})}();return Promise.resolve(f&&f.then?f.then(l):l())}catch(e){return Promise.reject(e)}}}export{s as standardSchemaResolver};
|
|
2
2
|
//# sourceMappingURL=standard-schema.module.js.map
|
|
@@ -1 +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 {
|
|
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 { getDotPath } from '@standard-schema/utils';\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 = getDotPath(error);\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 Schema extends StandardSchemaV1<FieldValues>,\n>(\n schema: Schema,\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<StandardSchemaV1.InferOutput<Schema>> {\n return async (values, _, 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","getDotPath","message","type","_extends2","types","_extends","Object","keys","parseIssues","shouldUseNativeValidation","criteriaMode","toNestErrors","validateFieldsNatively","raw","assign","value","validate","_temp","Promise","resolve","then","_result","e","reject"],"mappings":"oWAsDgBA,EAGdC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,aAEUC,EAAQC,EAAGC,GAAW,IAAA,IAAAC,EAAA,WAMlC,GAAIC,EAAOC,OAAQ,CACjB,IAAMC,EAhEZ,SACED,EACAE,GAIA,IAFA,IAAMD,EAAqC,GAElCE,EAAI,EAAGA,EAAIH,EAAOI,OAAQD,IAAK,CACtC,IAAME,EAAQL,EAAOG,GACfG,EAAOC,EAAWF,GAExB,GAAIC,IACGL,EAAOK,KACVL,EAAOK,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAM,KAG7CP,GAA0B,CAAA,IAAAQ,EACtBC,EAAQV,EAAOK,GAAMK,OAAS,CAAE,EAEtCV,EAAOK,GAAMK,MAAKC,EACbD,CAAAA,EAAAA,IAAKD,MACPG,OAAOC,KAAKH,GAAOP,QAASC,EAAMG,QAAOE,GAE9C,CAEJ,CAEA,OAAOT,CACT,CAqCqBc,CACbhB,EAAOC,QACNH,EAAQmB,2BAAsD,QAAzBnB,EAAQoB,cAGhD,MAAO,CACLtB,OAAQ,CAAE,EACVM,OAAQiB,EAAajB,EAAQJ,GAEjC,CAIA,OAFAA,EAAQmB,2BAA6BG,EAAuB,CAAE,EAAEtB,GAEzD,CACLF,OAAQD,EAAgB0B,IAAMP,OAAOQ,OAAO,CAAA,EAAI1B,GAAUI,EAAOuB,MACjErB,OAAQ,CAAA,EACR,EAtBEF,EAASN,EAAO,aAAa8B,SAAS5B,GAAQ6B,gBAC9CzB,aAAkB0B,QAAO,OAAAA,QAAAC,QACZ3B,GAAM4B,KAAA,SAAAC,GAArB7B,EAAM6B,CAAgB,EAAAH,IAAAA,OAAAA,QAAAC,QAAAF,GAAAA,EAAAG,KAAAH,EAAAG,KAAA7B,GAAAA,IAqB1B,CAAC,MAAA+B,GAAAJ,OAAAA,QAAAK,OAAAD,EACH,CAAA,CAAA"}
|
|
@@ -1,2 +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){e.standardSchemaResolver=function(e){return
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers"),require("@standard-schema/utils")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","@standard-schema/utils"],r):r((e||self).hookformResolversStandardSchema={},e.hookformResolvers,e.utils)}(this,function(e,r,t){function s(){return s=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var s in t)({}).hasOwnProperty.call(t,s)&&(e[s]=t[s])}return e},s.apply(null,arguments)}e.standardSchemaResolver=function(e,o){return void 0===o&&(o={}),function(n,a,i){try{var l=function(){if(u.issues){var e=function(e,r){for(var o={},n=0;n<e.length;n++){var a=e[n],i=t.getDotPath(a);if(i&&(o[i]||(o[i]={message:a.message,type:""}),r)){var l,u=o[i].types||{};o[i].types=s({},u,((l={})[Object.keys(u).length]=a.message,l))}}return o}(u.issues,!i.shouldUseNativeValidation&&"all"===i.criteriaMode);return{values:{},errors:r.toNestErrors(e,i)}}return i.shouldUseNativeValidation&&r.validateFieldsNatively({},i),{values:o.raw?Object.assign({},n):u.value,errors:{}}},u=e["~standard"].validate(n),f=function(){if(u instanceof Promise)return Promise.resolve(u).then(function(e){u=e})}();return Promise.resolve(f&&f.then?f.then(l):l())}catch(e){return Promise.reject(e)}}}});
|
|
2
2
|
//# sourceMappingURL=standard-schema.umd.js.map
|
|
@@ -1 +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 {
|
|
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 { getDotPath } from '@standard-schema/utils';\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 = getDotPath(error);\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 Schema extends StandardSchemaV1<FieldValues>,\n>(\n schema: Schema,\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<StandardSchemaV1.InferOutput<Schema>> {\n return async (values, _, 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","getDotPath","message","type","_extends2","types","_extends","Object","keys","parseIssues","shouldUseNativeValidation","criteriaMode","toNestErrors","validateFieldsNatively","raw","assign","value","validate","_temp","Promise","resolve","then","_result","e","reject"],"mappings":"moBAyDEA,EACAC,GAIA,gBAJAA,IAAAA,EAEI,aAEUC,EAAQC,EAAGC,GAAW,IAAA,IAAAC,EAAA,WAMlC,GAAIC,EAAOC,OAAQ,CACjB,IAAMC,EAhEZ,SACED,EACAE,GAIA,IAFA,IAAMD,EAAqC,GAElCE,EAAI,EAAGA,EAAIH,EAAOI,OAAQD,IAAK,CACtC,IAAME,EAAQL,EAAOG,GACfG,EAAOC,EAAAA,WAAWF,GAExB,GAAIC,IACGL,EAAOK,KACVL,EAAOK,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAM,KAG7CP,GAA0B,CAAA,IAAAQ,EACtBC,EAAQV,EAAOK,GAAMK,OAAS,CAAE,EAEtCV,EAAOK,GAAMK,MAAKC,EACbD,CAAAA,EAAAA,IAAKD,MACPG,OAAOC,KAAKH,GAAOP,QAASC,EAAMG,QAAOE,GAE9C,CAEJ,CAEA,OAAOT,CACT,CAqCqBc,CACbhB,EAAOC,QACNH,EAAQmB,2BAAsD,QAAzBnB,EAAQoB,cAGhD,MAAO,CACLtB,OAAQ,CAAE,EACVM,OAAQiB,EAAAA,aAAajB,EAAQJ,GAEjC,CAIA,OAFAA,EAAQmB,2BAA6BG,EAAsBA,uBAAC,CAAE,EAAEtB,GAEzD,CACLF,OAAQD,EAAgB0B,IAAMP,OAAOQ,OAAO,CAAA,EAAI1B,GAAUI,EAAOuB,MACjErB,OAAQ,CAAA,EACR,EAtBEF,EAASN,EAAO,aAAa8B,SAAS5B,GAAQ6B,gBAC9CzB,aAAkB0B,QAAO,OAAAA,QAAAC,QACZ3B,GAAM4B,KAAA,SAAAC,GAArB7B,EAAM6B,CAAgB,EAAAH,IAAAA,OAAAA,QAAAC,QAAAF,GAAAA,EAAAG,KAAAH,EAAAG,KAAA7B,GAAAA,IAqB1B,CAAC,MAAA+B,GAAAJ,OAAAA,QAAAK,OAAAD,EACH,CAAA,CAAA"}
|
|
@@ -12,16 +12,16 @@ const schema = type({
|
|
|
12
12
|
|
|
13
13
|
type FormData = typeof schema.infer & { unusedProperty: string };
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
onSubmit
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
function TestComponent({
|
|
16
|
+
onSubmit,
|
|
17
|
+
}: {
|
|
18
|
+
onSubmit: (data: typeof schema.infer) => void;
|
|
19
|
+
}) {
|
|
20
20
|
const {
|
|
21
21
|
register,
|
|
22
22
|
handleSubmit,
|
|
23
23
|
formState: { errors },
|
|
24
|
-
} = useForm
|
|
24
|
+
} = useForm({
|
|
25
25
|
resolver: standardSchemaResolver(schema), // Useful to check TypeScript regressions
|
|
26
26
|
});
|
|
27
27
|
|
|
@@ -54,3 +54,29 @@ test("form's validation with arkType and TypeScript's integration", async () =>
|
|
|
54
54
|
).toBeInTheDocument();
|
|
55
55
|
expect(handleSubmit).not.toHaveBeenCalled();
|
|
56
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
|
+
}
|
|
@@ -1,24 +1,48 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
2
|
import { Field, InternalFieldName } from 'react-hook-form';
|
|
3
|
+
import { z } from 'zod';
|
|
3
4
|
|
|
4
|
-
export const schema =
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
5
|
+
export const schema = z
|
|
6
|
+
.object({
|
|
7
|
+
username: z.string().regex(/^\w+$/).min(3).max(30),
|
|
8
|
+
password: z
|
|
9
|
+
.string()
|
|
10
|
+
.regex(new RegExp('.*[A-Z].*'), 'One uppercase character')
|
|
11
|
+
.regex(new RegExp('.*[a-z].*'), 'One lowercase character')
|
|
12
|
+
.regex(new RegExp('.*\\d.*'), 'One number')
|
|
13
|
+
.regex(
|
|
14
|
+
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
|
|
15
|
+
'One special character',
|
|
16
|
+
)
|
|
17
|
+
.min(8, 'Must be at least 8 characters in length'),
|
|
18
|
+
repeatPassword: z.string(),
|
|
19
|
+
accessToken: z.union([z.string(), z.number()]),
|
|
20
|
+
birthYear: z.number().min(1900).max(2013).optional(),
|
|
21
|
+
email: z.string().email().optional(),
|
|
22
|
+
tags: z.array(z.string()),
|
|
23
|
+
enabled: z.boolean(),
|
|
24
|
+
url: z.string().url('Custom error url').or(z.literal('')),
|
|
25
|
+
like: z
|
|
26
|
+
.array(
|
|
27
|
+
z.object({
|
|
28
|
+
id: z.number(),
|
|
29
|
+
name: z.string().length(4),
|
|
30
|
+
}),
|
|
31
|
+
)
|
|
32
|
+
.optional(),
|
|
33
|
+
dateStr: z
|
|
34
|
+
.string()
|
|
35
|
+
.transform((value) => new Date(value))
|
|
36
|
+
.refine((value) => !isNaN(value.getTime()), {
|
|
37
|
+
message: 'Invalid date',
|
|
38
|
+
}),
|
|
39
|
+
})
|
|
40
|
+
.refine((obj) => obj.password === obj.repeatPassword, {
|
|
41
|
+
message: 'Passwords do not match',
|
|
42
|
+
path: ['confirm'],
|
|
43
|
+
});
|
|
20
44
|
|
|
21
|
-
export const validData
|
|
45
|
+
export const validData = {
|
|
22
46
|
username: 'Doe',
|
|
23
47
|
password: 'Password123_',
|
|
24
48
|
repeatPassword: 'Password123_',
|
|
@@ -34,8 +58,8 @@ export const validData: typeof schema.infer = {
|
|
|
34
58
|
name: 'name',
|
|
35
59
|
},
|
|
36
60
|
],
|
|
37
|
-
dateStr:
|
|
38
|
-
}
|
|
61
|
+
dateStr: '2020-01-01T00:00:00.000Z',
|
|
62
|
+
} as any as z.infer<typeof schema>;
|
|
39
63
|
|
|
40
64
|
export const invalidData = {
|
|
41
65
|
password: '___',
|
|
@@ -43,7 +67,7 @@ export const invalidData = {
|
|
|
43
67
|
birthYear: 'birthYear',
|
|
44
68
|
like: [{ id: 'z' }],
|
|
45
69
|
url: 'abc',
|
|
46
|
-
}
|
|
70
|
+
} as any as z.infer<typeof schema>;
|
|
47
71
|
|
|
48
72
|
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
49
73
|
username: {
|
|
@@ -63,3 +87,25 @@ export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
|
63
87
|
name: 'birthday',
|
|
64
88
|
},
|
|
65
89
|
};
|
|
90
|
+
|
|
91
|
+
export const customSchema: StandardSchemaV1<
|
|
92
|
+
StandardSchemaV1.InferInput<typeof schema>,
|
|
93
|
+
StandardSchemaV1.InferOutput<typeof schema>
|
|
94
|
+
> = {
|
|
95
|
+
'~standard': {
|
|
96
|
+
version: 1,
|
|
97
|
+
vendor: 'custom',
|
|
98
|
+
validate: () => ({
|
|
99
|
+
issues: [
|
|
100
|
+
{
|
|
101
|
+
path: [{ key: 'username' }],
|
|
102
|
+
message: 'Custom error',
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
path: [{ key: 'like' }, { key: 0 }, { key: 'id' }],
|
|
106
|
+
message: 'Custom error',
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
}),
|
|
110
|
+
},
|
|
111
|
+
};
|