2026-10-05 18:10:18 -04:00
|
|
|
import { isObject, isString } from '@/lib/objects';
|
|
|
|
|
|
|
|
|
|
// Field validators produce strings; standard-schema adapters produce `{ message }` objects.
|
|
|
|
|
export function firstError<T>(errors: T[]): string | undefined {
|
|
|
|
|
if (!errors || errors.length === 0) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const error = errors.find((e) => e !== undefined && e !== null && e !== '');
|
2026-10-08 16:53:20 -04:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if (error === undefined) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isString(error)) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isObject(error) && 'message' in error) {
|
|
|
|
|
return String(error.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String(error);
|
|
|
|
|
}
|