From 3b2e265249891d0b1d89f835a1d0bea8c271c1be Mon Sep 17 00:00:00 2001 From: Fabian Hiller Date: Tue, 4 Jun 2024 22:52:08 +0200 Subject: [PATCH] Upgrade Valibot and refactor resolver with new util --- package.json | 2 +- pnpm-lock.yaml | 10 +++---- valibot/src/valibot.ts | 64 ++++++++++++++++-------------------------- 3 files changed, 30 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index cb5d897b..cb8e41bd 100644 --- a/package.json +++ b/package.json @@ -262,7 +262,7 @@ "superstruct": "^1.0.3", "typanion": "^3.14.0", "typescript": "^5.1.6", - "valibot": "0.31.0-rc.11", + "valibot": "0.31.0-rc.12", "vest": "^4.6.11", "vite": "^4.4.9", "vite-tsconfig-paths": "^4.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9eb51e2a..ff5f0ab3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -135,8 +135,8 @@ importers: specifier: ^5.1.6 version: 5.1.6 valibot: - specifier: 0.31.0-rc.11 - version: 0.31.0-rc.11 + specifier: 0.31.0-rc.12 + version: 0.31.0-rc.12 vest: specifier: ^4.6.11 version: 4.6.11 @@ -3472,8 +3472,8 @@ packages: util-deprecate@1.0.2: resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==} - valibot@0.31.0-rc.11: - resolution: {integrity: sha512-WAyZUdU0RP93vl0A8HyCUi5q4oveLjglou4X/Zn7a9T/lQLHb8J376SXjMqaMweG+XBZJ0bU2b156+d/nAlJ+A==} + valibot@0.31.0-rc.12: + resolution: {integrity: sha512-vAI18A65Og1BwuVqC3Zvr0+yNxOANLHDVo3r5VOTsVItfBl6KcykmY7fioSU5CVSlrj2JgViHjcsfAofO2h0rw==} validate-npm-package-license@3.0.4: resolution: {integrity: sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==} @@ -7239,7 +7239,7 @@ snapshots: util-deprecate@1.0.2: {} - valibot@0.31.0-rc.11: {} + valibot@0.31.0-rc.12: {} validate-npm-package-license@3.0.4: dependencies: diff --git a/valibot/src/valibot.ts b/valibot/src/valibot.ts index 51f45a20..6fb15733 100644 --- a/valibot/src/valibot.ts +++ b/valibot/src/valibot.ts @@ -1,6 +1,6 @@ import { toNestErrors } from '@hookform/resolvers'; import { FieldError, appendErrors, FieldValues } from 'react-hook-form'; -import { safeParseAsync } from 'valibot'; +import { getDotPath, safeParseAsync } from 'valibot'; import type { Resolver } from './types'; export const valibotResolver: Resolver = @@ -23,47 +23,31 @@ export const valibotResolver: Resolver = // Iterate over issues to add them to errors object for (const issue of result.issues) { - if (issue.path) { - // Create path string from issue path - let path = ''; - for (const item of issue.path) { - if ( - 'key' in item && - (typeof item.key === 'string' || typeof item.key === 'number') - ) { - if (path) { - path += `.${item.key}`; - } else { - path += item.key; - } - } else { - break; - } - } + // Create dot path from issue + const path = getDotPath(issue); - if (path) { - // Add first error of path to errors object - if (!errors[path]) { - errors[path] = { message: issue.message, type: issue.type }; - } + if (path) { + // Add first error of path to errors object + if (!errors[path]) { + errors[path] = { message: issue.message, type: issue.type }; + } - // 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; - } + // 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; } } }