Rocky_Mountain_Vending/.pnpm-store/v10/files/29/e0d2c2fe432f31a14c4716c79ea3540b7fbdcc65a8ace67e8152e003e76472ddd73a82f24a7cb309123963b5899711f0d0ef90e1e26337e002b154bc7bd029
DMleadgen 46d973904b
Initial commit: Rocky Mountain Vending website
Next.js website for Rocky Mountain Vending company featuring:
- Product catalog with Stripe integration
- Service areas and parts pages
- Admin dashboard with Clerk authentication
- SEO optimized pages with JSON-LD structured data

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-12 16:22:15 -07:00

81 lines
2 KiB
Text

import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import type { ValidationError } from 'joi';
import { FieldError, appendErrors } from 'react-hook-form';
import { Resolver } from './types';
const parseErrorSchema = (
error: ValidationError,
validateAllFieldCriteria: boolean,
) =>
error.details.length
? error.details.reduce<Record<string, FieldError>>((previous, error) => {
const _path = error.path.join('.');
if (!previous[_path]) {
previous[_path] = { message: error.message, type: error.type };
}
if (validateAllFieldCriteria) {
const types = previous[_path].types;
const messages = types && types[error.type!];
previous[_path] = appendErrors(
_path,
validateAllFieldCriteria,
previous,
error.type,
messages
? ([] as string[]).concat(messages as string[], error.message)
: error.message,
) as FieldError;
}
return previous;
}, {})
: {};
export const joiResolver: Resolver =
(
schema,
schemaOptions = {
abortEarly: false,
},
resolverOptions = {},
) =>
async (values, context, options) => {
const _schemaOptions = Object.assign({}, schemaOptions, {
context,
});
let result: Record<string, any> = {};
if (resolverOptions.mode === 'sync') {
result = schema.validate(values, _schemaOptions);
} else {
try {
result.value = await schema.validateAsync(values, _schemaOptions);
} catch (e) {
result.error = e;
}
}
if (result.error) {
return {
values: {},
errors: toNestErrors(
parseErrorSchema(
result.error,
!options.shouldUseNativeValidation &&
options.criteriaMode === 'all',
),
options,
),
};
}
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
return {
errors: {},
values: result.value,
};
};