Zod 3.x Error Formatter

Formats zod 3.x error in human readable format

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/565798/1752557/Zod%203x%20Error%20Formatter.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name Zod 3.x Error Formatter
// @namespace -
// @version 1.1.0
// @description Formats zod 3.x error in human readable format
// @author NotYou
// @grant none
// @license MIT
// ==/UserScript==

!function() {
    'use strict';

    class ZodErrorFormatter {
        static format(zodError) {
            if (
                !Array.isArray(zodError.issues) &&
                typeof zodError.isEmpty !== 'boolean'
            ) {
                throw new Error('Provided value is not a ZodError-like object')
            }

            if (zodError.isEmpty) {
                return 'No issues found'
            }

            const justDisplayMessage = issue => issue.message
            const formatPath = path => path.map(pathItem => {
                if (typeof pathItem === 'number') {
                    return `[${pathItem}]`
                }

                return pathItem.toString()
            }).join('.')

            const formatIssue = issue => {
                const issueFormatter = {
                    "invalid_type": issue => `${issue.message} ${issue.expected}, but got "${issue.received}"${issue.path.length ?` at path: "${formatPath(issue.path)}"` : ''}`,
                    "invalid_literal": issue => `${issue.message}, but got "${issue.received}"`,
                    "custom": justDisplayMessage,
                    "invalid_union": issue => {
                        const expectedValues = issue.unionErrors.map(unionError => `"${unionError.issues[0].expected}"`).join(' | ')

                        return `${issue.message} "${formatPath(issue.path)}", expected these values: ${expectedValues}`
                    },
                    "invalid_union_discriminator": justDisplayMessage,
                    "invalid_enum_value": justDisplayMessage,
                    "unrecognized_keys": justDisplayMessage,
                    "invalid_arguments": justDisplayMessage,
                    "invalid_return_type": justDisplayMessage,
                    "invalid_date": justDisplayMessage,
                    "invalid_string": issue => `Invalid string format, validation failed at "${issue.validation}"`,
                    "too_small": justDisplayMessage,
                    "too_big": justDisplayMessage,
                    "invalid_intersection_types": justDisplayMessage,
                    "not_multiple_of": justDisplayMessage,
                    "not_finite": justDisplayMessage
                }[issue.code]

                if (typeof issueFormatter === 'function') {
                    return issueFormatter(issue)
                }

                return `Got unexpected error! Code: ${issue.code}; Message: ${issue.message}`
            }

            return zodError.issues.map(formatIssue).join('\n\n')
        }
    }

    window.ZodErrorFormatter = ZodErrorFormatter
}();