UNPKG

@schema-hub/zod-error-formatter

Version:

Simple and easy-to-understand zod error messages

43 lines 1.35 kB
import { isNonEmptyArray } from '../tuple/non-empty-array.js'; function joinList(values, separator, lastItemSeparator) { const initialList = Array.from(values); const lastItem = initialList.pop(); if (initialList.length === 0) { return lastItem; } const joinedInitialList = initialList.join(separator); return `${joinedInitialList}${lastItemSeparator}${lastItem}`; } export function isParsedType(value) { return typeof value === 'object' && value !== null && Object.hasOwn(value, 'type'); } function stringify(value) { if (isParsedType(value)) { return value.type; } if (value === undefined) { return 'undefined'; } if (typeof value === 'symbol') { return value.toString(); } if (typeof value === 'bigint') { return value.toString(); } return JSON.stringify(value); } export function formatList(values, lastItemSeparator) { const escapedValues = values.map(stringify); if (!isNonEmptyArray(escapedValues)) { return 'unknown'; } return joinList(escapedValues, ', ', lastItemSeparator); } export function formatOneOfList(values) { const formattedList = formatList(values, ' or '); if (values.length > 1) { return `one of ${formattedList}`; } return formattedList; } //# sourceMappingURL=list.js.map