Socket
Socket
Sign inDemoInstall

@hookform/resolvers

Package Overview
Dependencies
Maintainers
3
Versions
118
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hookform/resolvers - npm Package Compare versions

Comparing version 3.5.0 to 3.6.0

4

package.json
{
"name": "@hookform/resolvers",
"amdName": "hookformResolvers",
"version": "3.5.0",
"version": "3.6.0",
"description": "React Hook Form validation resolvers: Yup, Joi, Superstruct, Zod, Vest, Class Validator, io-ts, Nope, computed-types, TypeBox, arktype, Typanion and Effect-TS",

@@ -265,3 +265,3 @@ "main": "dist/resolvers.js",

"typescript": "^5.1.6",
"valibot": "^0.24.1",
"valibot": "0.31.0-rc.12",
"vest": "^4.6.11",

@@ -268,0 +268,0 @@ "vite": "^4.4.9",

@@ -555,10 +555,11 @@ <div align="center">

import { valibotResolver } from '@hookform/resolvers/valibot';
import { object, string, minLength, endsWith } from 'valibot';
import * as v from 'valibot';
const schema = object({
username: string('username is required', [
minLength(3, 'Needs to be at least 3 characters'),
endsWith('cool', 'Needs to end with `cool`'),
]),
password: string('password is required'),
const schema = v.object({
username: v.pipe(
v.string('username is required'),
v.minLength(3, 'Needs to be at least 3 characters'),
v.endsWith('cool', 'Needs to end with `cool`'),
),
password: v.string('password is required'),
});

@@ -585,3 +586,3 @@

[![npm](https://img.shields.io/bundlephobia/minzip/valibot?style=for-the-badge)](https://bundlephobia.com/result?p=effect)
[![npm](https://img.shields.io/bundlephobia/minzip/@effect/schema?style=for-the-badge)](https://bundlephobia.com/result?p=effect)

@@ -588,0 +589,0 @@ ```typescript jsx

import { FieldValues, ResolverResult, ResolverOptions } from 'react-hook-form';
import { BaseSchema, BaseSchemaAsync, ParseInfo } from 'valibot';
export type Resolver = <T extends BaseSchema | BaseSchemaAsync>(schema: T, schemaOptions?: Partial<Pick<ParseInfo, 'abortEarly' | 'abortPipeEarly'>>, resolverOptions?: {
import { BaseIssue, BaseSchema, BaseSchemaAsync, Config, InferIssue } from 'valibot';
export type Resolver = <T extends BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>>(schema: T, schemaOptions?: Partial<Omit<Config<InferIssue<T>>, 'abortPipeEarly' | 'skipPipe'>>, resolverOptions?: {
/**

@@ -5,0 +5,0 @@ * @default async

@@ -1,2 +0,2 @@

var r=require("@hookform/resolvers"),e=require("valibot"),t=require("react-hook-form");function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}var o=function(r,e){for(var o,a={},i=function(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return n(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(r,e):void 0}}(r))){t&&(r=t);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(r.issues);!(o=i()).done;){var s=o.value;if(s.path){var u=s.path.map(function(r){return r.key}).join(".");if(a[u]||(a[u]={message:s.message,type:s.validation}),e){var l=a[u].types,c=l&&l[s.validation];a[u]=t.appendErrors(u,e,a,s.validation,c?[].concat(c,s.message):s.message)}}}return a};exports.valibotResolver=function(t,n,a){return void 0===a&&(a={}),function(i,s,u){try{return Promise.resolve(function(r,o){try{var s=function(){function r(r){return{values:a.raw?i:r,errors:{}}}var o=Object.assign({},{abortEarly:!1,abortPipeEarly:!1},n);return"sync"===a.mode?r(e.parse(t,i,o)):Promise.resolve(e.parseAsync(t,i,o)).then(r)}()}catch(r){return o(r)}return s&&s.then?s.then(void 0,o):s}(0,function(t){if(t instanceof e.ValiError)return{values:{},errors:r.toNestErrors(o(t,!u.shouldUseNativeValidation&&"all"===u.criteriaMode),u)};throw t}))}catch(r){return Promise.reject(r)}}};
var r=require("@hookform/resolvers"),e=require("react-hook-form"),t=require("valibot");function o(){return o=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(r[o]=t[o])}return r},o.apply(this,arguments)}function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,o=new Array(e);t<e;t++)o[t]=r[t];return o}exports.valibotResolver=function(a,i,s){return void 0===s&&(s={}),function(u,l,c){try{var f=!c.shouldUseNativeValidation&&"all"===c.criteriaMode;return Promise.resolve(t.safeParseAsync(a,u,o({},i,{abortPipeEarly:!f}))).then(function(o){if(o.issues){for(var a,i={},l=function(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return n(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(r,e):void 0}}(r))){t&&(r=t);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(o.issues);!(a=l()).done;){var v=a.value,y=t.getDotPath(v);if(y&&(i[y]||(i[y]={message:v.message,type:v.type}),f)){var p=i[y].types,m=p&&p[v.type];i[y]=e.appendErrors(y,f,i,v.type,m?[].concat(m,v.message):v.message)}}return{values:{},errors:r.toNestErrors(i,c)}}return{values:s.raw?u:o.output,errors:{}}})}catch(r){return Promise.reject(r)}}};
//# sourceMappingURL=valibot.js.map

@@ -1,2 +0,2 @@

import{toNestErrors as r}from"@hookform/resolvers";import{parse as t,parseAsync as e,ValiError as n}from"valibot";import{appendErrors as o}from"react-hook-form";function a(r,t){(null==t||t>r.length)&&(t=r.length);for(var e=0,n=new Array(t);e<t;e++)n[e]=r[e];return n}var i=function(r,t){for(var e,n={},i=function(r,t){var e="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(e)return(e=e.call(r)).next.bind(e);if(Array.isArray(r)||(e=function(r,t){if(r){if("string"==typeof r)return a(r,t);var e=Object.prototype.toString.call(r).slice(8,-1);return"Object"===e&&r.constructor&&(e=r.constructor.name),"Map"===e||"Set"===e?Array.from(r):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?a(r,t):void 0}}(r))){e&&(r=e);var n=0;return function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(r.issues);!(e=i()).done;){var u=e.value;if(u.path){var s=u.path.map(function(r){return r.key}).join(".");if(n[s]||(n[s]={message:u.message,type:u.validation}),t){var c=n[s].types,l=c&&c[u.validation];n[s]=o(s,t,n,u.validation,l?[].concat(l,u.message):u.message)}}}return n},u=function(o,a,u){return void 0===u&&(u={}),function(s,c,l){try{return Promise.resolve(function(r,n){try{var i=function(){function r(r){return{values:u.raw?s:r,errors:{}}}var n=Object.assign({},{abortEarly:!1,abortPipeEarly:!1},a);return"sync"===u.mode?r(t(o,s,n)):Promise.resolve(e(o,s,n)).then(r)}()}catch(r){return n(r)}return i&&i.then?i.then(void 0,n):i}(0,function(t){if(t instanceof n)return{values:{},errors:r(i(t,!l.shouldUseNativeValidation&&"all"===l.criteriaMode),l)};throw t}))}catch(r){return Promise.reject(r)}}};export{u as valibotResolver};
import{toNestErrors as r}from"@hookform/resolvers";import{appendErrors as e}from"react-hook-form";import{safeParseAsync as t,getDotPath as o}from"valibot";function n(){return n=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(r[o]=t[o])}return r},n.apply(this,arguments)}function a(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,o=new Array(e);t<e;t++)o[t]=r[t];return o}var i=function(i,s,u){return void 0===u&&(u={}),function(l,c,f){try{var m=!f.shouldUseNativeValidation&&"all"===f.criteriaMode;return Promise.resolve(t(i,l,n({},s,{abortPipeEarly:!m}))).then(function(t){if(t.issues){for(var n,i={},s=function(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return a(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?a(r,e):void 0}}(r))){t&&(r=t);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(t.issues);!(n=s()).done;){var c=n.value,p=o(c);if(p&&(i[p]||(i[p]={message:c.message,type:c.type}),m)){var v=i[p].types,y=v&&v[c.type];i[p]=e(p,m,i,c.type,y?[].concat(y,c.message):c.message)}}return{values:{},errors:r(i,f)}}return{values:u.raw?l:t.output,errors:{}}})}catch(r){return Promise.reject(r)}}};export{i as valibotResolver};
//# sourceMappingURL=valibot.module.js.map

@@ -1,2 +0,2 @@

!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@hookform/resolvers"),require("valibot"),require("react-hook-form")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","valibot","react-hook-form"],e):e((r||self).hookformResolversValibot={},r.hookformResolvers,r.valibot,r.ReactHookForm)}(this,function(r,e,t,o){function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,o=new Array(e);t<e;t++)o[t]=r[t];return o}var a=function(r,e){for(var t,a={},i=function(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return n(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(r,e):void 0}}(r))){t&&(r=t);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(r.issues);!(t=i()).done;){var s=t.value;if(s.path){var l=s.path.map(function(r){return r.key}).join(".");if(a[l]||(a[l]={message:s.message,type:s.validation}),e){var u=a[l].types,f=u&&u[s.validation];a[l]=o.appendErrors(l,e,a,s.validation,f?[].concat(f,s.message):s.message)}}}return a};r.valibotResolver=function(r,o,n){return void 0===n&&(n={}),function(i,s,l){try{return Promise.resolve(function(e,a){try{var s=function(){function e(r){return{values:n.raw?i:r,errors:{}}}var a=Object.assign({},{abortEarly:!1,abortPipeEarly:!1},o);return"sync"===n.mode?e(t.parse(r,i,a)):Promise.resolve(t.parseAsync(r,i,a)).then(e)}()}catch(r){return a(r)}return s&&s.then?s.then(void 0,a):s}(0,function(r){if(r instanceof t.ValiError)return{values:{},errors:e.toNestErrors(a(r,!l.shouldUseNativeValidation&&"all"===l.criteriaMode),l)};throw r}))}catch(r){return Promise.reject(r)}}}});
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers"),require("react-hook-form"),require("valibot")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","react-hook-form","valibot"],r):r((e||self).hookformResolversValibot={},e.hookformResolvers,e.ReactHookForm,e.valibot)}(this,function(e,r,t,o){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 o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},n.apply(this,arguments)}function a(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,o=new Array(r);t<r;t++)o[t]=e[t];return o}e.valibotResolver=function(e,i,s){return void 0===s&&(s={}),function(l,u,f){try{var c=!f.shouldUseNativeValidation&&"all"===f.criteriaMode;return Promise.resolve(o.safeParseAsync(e,l,n({},i,{abortPipeEarly:!c}))).then(function(e){if(e.issues){for(var n,i={},u=function(e,r){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,r){if(e){if("string"==typeof e)return a(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?a(e,r):void 0}}(e))){t&&(e=t);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(e.issues);!(n=u()).done;){var v=n.value,p=o.getDotPath(v);if(p&&(i[p]||(i[p]={message:v.message,type:v.type}),c)){var y=i[p].types,d=y&&y[v.type];i[p]=t.appendErrors(p,c,i,v.type,d?[].concat(d,v.message):v.message)}}return{values:{},errors:r.toNestErrors(i,f)}}return{values:s.raw?l:e.output,errors:{}}})}catch(e){return Promise.reject(e)}}}});
//# sourceMappingURL=valibot.umd.js.map

@@ -14,6 +14,6 @@ {

"peerDependencies": {
"@hookform/resolvers": "^2.0.0",
"react-hook-form": "^7.0.0",
"@hookform/resolvers": "^2.0.0",
"valibot": ">=0.8"
"valibot": ">=0.31.0 <1"
}
}
import { Field, InternalFieldName } from 'react-hook-form';
import {
object,
string,
minLength,
maxLength,
regex,
number,
minValue,
maxValue,
email,
array,
boolean,
required,
union,
variant,
literal,
} from 'valibot';
import * as v from 'valibot';
export const schema = required(
object({
username: string([minLength(2), maxLength(30), regex(/^\w+$/)]),
password: string('New Password is required', [
regex(new RegExp('.*[A-Z].*'), 'One uppercase character'),
regex(new RegExp('.*[a-z].*'), 'One lowercase character'),
regex(new RegExp('.*\\d.*'), 'One number'),
regex(
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
'One special character',
),
minLength(8, 'Must be at least 8 characters in length'),
]),
repeatPassword: string('Repeat Password is required'),
accessToken: union(
[
string('Access token should be a string'),
number('Access token should be a number'),
],
'access token is required',
export const schema = v.object({
username: v.pipe(
v.string(),
v.minLength(2),
v.maxLength(30),
v.regex(/^\w+$/),
),
password: v.pipe(
v.string('New Password is required'),
v.regex(new RegExp('.*[A-Z].*'), 'One uppercase character'),
v.regex(new RegExp('.*[a-z].*'), 'One lowercase character'),
v.regex(new RegExp('.*\\d.*'), 'One number'),
v.regex(
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
'One special character',
),
birthYear: number('Please enter your birth year', [
minValue(1900),
maxValue(2013),
]),
email: string([email('Invalid email address')]),
tags: array(string('Tags should be strings')),
enabled: boolean(),
like: required(
object({
id: number('Like id is required'),
name: string('Like name is required', [minLength(4, 'Too short')]),
}),
v.minLength(8, 'Must be at least 8 characters in length'),
),
repeatPassword: v.string('Repeat Password is required'),
accessToken: v.union(
[
v.string('Access token should be a string'),
v.number('Access token should be a number'),
],
'access token is required',
),
birthYear: v.pipe(
v.number('Please enter your birth year'),
v.minValue(1900),
v.maxValue(2013),
),
email: v.pipe(v.string(), v.email('Invalid email address')),
tags: v.array(v.string('Tags should be strings')),
enabled: v.boolean(),
like: v.object({
id: v.number('Like id is required'),
name: v.pipe(
v.string('Like name is required'),
v.minLength(4, 'Too short'),
),
}),
);
});
export const schemaError = variant('type', [
object({ type: literal('a') }),
object({ type: literal('b') }),
export const schemaError = v.variant('type', [
v.object({ type: v.literal('a') }),
v.object({ type: v.literal('b') }),
]);

@@ -61,0 +51,0 @@

@@ -24,4 +24,3 @@ /* eslint-disable no-console, @typescript-eslint/ban-ts-comment */

});
const parseSpy = vi.spyOn(valibot, 'parse');
const parseAsyncSpy = vi.spyOn(valibot, 'parseAsync');
const funcSpy = vi.spyOn(valibot, 'safeParseAsync');

@@ -32,4 +31,3 @@ const result = await valibotResolver(schema, undefined, {

expect(parseSpy).toHaveBeenCalledTimes(1);
expect(parseAsyncSpy).not.toHaveBeenCalled();
expect(funcSpy).toHaveBeenCalledTimes(1);
expect(result.errors).toEqual({});

@@ -47,4 +45,3 @@ expect(result).toMatchSnapshot();

});
const parseSpy = vi.spyOn(valibot, 'parse');
const parseAsyncSpy = vi.spyOn(valibot, 'parseAsync');
const funcSpy = vi.spyOn(valibot, 'safeParseAsync');

@@ -55,4 +52,3 @@ const result = await valibotResolver(schema, undefined, {

expect(parseSpy).toHaveBeenCalledTimes(1);
expect(parseAsyncSpy).not.toHaveBeenCalled();
expect(funcSpy).toHaveBeenCalledTimes(1);
expect(result).toMatchSnapshot();

@@ -114,3 +110,3 @@ });

it('should be able to validate variants', async () => {
it('should be able to validate variants without errors', async () => {
const result = await valibotResolver(schemaError, undefined, {

@@ -131,3 +127,3 @@ mode: 'sync',

it('should exit issue resolution if no path is set', async () => {
it('should be able to validate variants with errors', async () => {
const result = await valibotResolver(schemaError, undefined, {

@@ -141,3 +137,9 @@ mode: 'sync',

expect(result).toEqual({
errors: {},
errors: {
type: {
message: 'Invalid type: Expected "a" | "b" but received "c"',
ref: undefined,
type: 'variant',
},
},
values: {},

@@ -144,0 +146,0 @@ });

import { FieldValues, ResolverResult, ResolverOptions } from 'react-hook-form';
import { BaseSchema, BaseSchemaAsync, ParseInfo } from 'valibot';
import {
BaseIssue,
BaseSchema,
BaseSchemaAsync,
Config,
InferIssue,
} from 'valibot';
export type Resolver = <T extends BaseSchema | BaseSchemaAsync>(
export type Resolver = <
T extends
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
>(
schema: T,
schemaOptions?: Partial<Pick<ParseInfo, 'abortEarly' | 'abortPipeEarly'>>,
schemaOptions?: Partial<
Omit<Config<InferIssue<T>>, 'abortPipeEarly' | 'skipPipe'>
>,
resolverOptions?: {

@@ -8,0 +20,0 @@ /**

import { toNestErrors } from '@hookform/resolvers';
import { FieldError, appendErrors, FieldValues } from 'react-hook-form';
import { getDotPath, safeParseAsync } from 'valibot';
import type { Resolver } from './types';
import {
BaseSchema,
BaseSchemaAsync,
ValiError,
parse,
parseAsync,
} from 'valibot';
import { FieldErrors, FieldError, appendErrors } from 'react-hook-form';
const parseErrors = (
valiErrors: ValiError,
validateAllFieldCriteria: boolean,
): FieldErrors => {
const errors: Record<string, FieldError> = {};
for (const error of valiErrors.issues) {
if (!error.path) {
continue;
}
const _path = error.path.map(({ key }) => key).join('.');
export const valibotResolver: Resolver =
(schema, schemaOptions, resolverOptions = {}) =>
async (values, _, options) => {
// Check if we should validate all field criteria
const validateAllFieldCriteria =
!options.shouldUseNativeValidation && options.criteriaMode === 'all';
if (!errors[_path]) {
errors[_path] = { message: error.message, type: error.validation };
}
// Parse values with Valibot schema
const result = await safeParseAsync(schema, values, {
...schemaOptions,
abortPipeEarly: !validateAllFieldCriteria,
});
if (validateAllFieldCriteria) {
const types = errors[_path].types;
const messages = types && types[error.validation];
// If there are issues, return them as errors
if (result.issues) {
// Create errors object
const errors: Record<string, FieldError> = {};
errors[_path] = appendErrors(
_path,
validateAllFieldCriteria,
errors,
error.validation,
messages
? ([] as string[]).concat(messages as string[], error.message)
: error.message,
) as FieldError;
}
}
// Iterate over issues to add them to errors object
for (const issue of result.issues) {
// Create dot path from issue
const path = getDotPath(issue);
return errors;
};
if (path) {
// Add first error of path to errors object
if (!errors[path]) {
errors[path] = { message: issue.message, type: issue.type };
}
export const valibotResolver: Resolver =
(schema, schemaOptions, resolverOptions = {}) =>
async (values, _, options) => {
try {
const schemaOpts = Object.assign(
{},
{
abortEarly: false,
abortPipeEarly: false,
},
schemaOptions,
);
// If configured, add all errors of path to errors object
if (validateAllFieldCriteria) {
const types = errors[path].types;
const messages = types && types[issue.type];
errors[path] = appendErrors(
path,
validateAllFieldCriteria,
errors,
issue.type,
messages
? ([] as string[]).concat(
messages as string | string[],
issue.message,
)
: issue.message,
) as FieldError;
}
}
}
const parsed =
resolverOptions.mode === 'sync'
? parse(schema as BaseSchema, values, schemaOpts)
: await parseAsync(
schema as BaseSchema | BaseSchemaAsync,
values,
schemaOpts,
);
// Return resolver result with errors
return {
values: resolverOptions.raw ? values : parsed,
errors: {} as FieldErrors,
};
} catch (error) {
if (error instanceof ValiError) {
return {
values: {},
errors: toNestErrors(
parseErrors(
error,
!options.shouldUseNativeValidation &&
options.criteriaMode === 'all',
),
options,
),
};
}
values: {},
errors: toNestErrors(errors, options),
} as const;
}
throw error;
}
// Otherwise, return resolver result with values
return {
values: resolverOptions.raw ? values : (result.output as FieldValues),
errors: {},
};
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc