Zod 3.x Error Formatter

Formats zod 3.x error in human readable format

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greasyfork.org/scripts/565798/1752557/Zod%203x%20Error%20Formatter.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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