Zod 3.x Error Formatter

Formats zod 3.x error in human readable format

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/565798/1752557/Zod%203x%20Error%20Formatter.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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
}();