makearmy-app/node_modules/@hookform/resolvers/valibot/dist/valibot.modern.mjs.map

1 line
5.7 KiB
Text
Raw Normal View History

{"version":3,"file":"valibot.modern.mjs","sources":["../src/valibot.ts"],"sourcesContent":["import { toNestErrors } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\nimport { getDotPath, safeParseAsync } from 'valibot';\nimport { BaseSchema, BaseSchemaAsync, Config, InferIssue } from 'valibot';\n\nexport function valibotResolver<Input extends FieldValues, Context, Output>(\n schema: BaseSchema<Input, Output, any> | BaseSchemaAsync<Input, Output, any>,\n schemaOptions?: Partial<\n Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>\n >,\n resolverOptions?: {\n mode?: 'async' | 'sync';\n raw?: false;\n },\n): Resolver<Input, Context, Output>;\n\nexport function valibotResolver<Input extends FieldValues, Context, Output>(\n schema: BaseSchema<Input, Output, any> | BaseSchemaAsync<Input, Output, any>,\n schemaOptions:\n | Partial<\n Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>\n >\n | undefined,\n resolverOptions: {\n mode?: 'async' | 'sync';\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver for react-hook-form using Valibot schema validation\n * @param {BaseSchema<TFieldValues, TFieldValues, any> | BaseSchemaAsync<TFieldValues, TFieldValues, any>} schema - The Valibot schema to validate against\n * @param {Partial<Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>>} [schemaOptions] - Optional Valibot validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {('sync' | 'async')} [resolverOptions.mode] - Validation mode\n * @param {boolean} [resolverOptions.raw] - If true, returns raw values rather than validated results\n * @returns {Resolver<InferOutput<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = valibot.object({\n * name: valibot.string().minLength(2),\n * age: valibot.number().min(18)\n * });\n *\n * useForm({\n * resolver: valibotResolver(schema)\n * });\n */\nexport function valibotResolver<Input extends FieldValues, Context, Output>(\n schema: BaseSchema<Input, Output, any> | BaseSchemaAsync<Input, Output, any>,\n schemaOptions?: Partial<\n Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>\n >,\n resolverOptions: {\n /**\n * @default async\n */\n mode?: 'sync' | 'async';\n /**\n * Return the raw input values rather than the parsed values.\n * @default false\n */\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Output | Input> {\n return async (values: Input, _, options) => {\n // Check if we should validate all field criteria\n const validateAllFieldCriteria =\n !options.shouldUseNativeValidation && options.criteriaMode === 'all';\n\n // Parse values with Valibot schema\n const result = await safeParseAsync(\n schema,\n values,\n Object.assign({}, schemaOptions, {\n abortPipeEarly: !validateAllFieldCriteria,\n }),\n );\n\n // If there are issues, return them as errors\n if (result.issues) {\n // Create errors object\n const errors: Record<string, FieldError> = {};\n\n // Iterate over issues to add them to errors object\n for (; result.issues.length; ) {\n const issue = result.issues[0];\n // Create dot path from issue\n const path = getDotPath(issue);\n\n if (path) {\n // Add first error of path to errors object\n if (!errors[path]) {\n errors[path] = { message: issue.message, type: issue.type };\n }\n\n // If configured, add all errors of path to errors object\n if (validateAllFieldCriteria) {\n const types = errors[path].types;\n const messages = types && types[issue.type];\n errors[path] = appendErrors(\n path,\n validateAllFieldCriteria,\n errors,\n issue.type,\n messages\n