UNPKG

@chubbyts/chubbyts-api

Version:

[![CI](https://github.com/chubbyts/chubbyts-api/workflows/CI/badge.svg?branch=master)](https://github.com/chubbyts/chubbyts-api/actions?query=workflow%3ACI) [![Coverage Status](https://coveralls.io/repos/github/chubbyts/chubbyts-api/badge.svg?branch=maste

38 lines (37 loc) 1.14 kB
import { isArray, isBoolean, isNull, isNumber, isObject, isString } from '@chubbyts/chubbyts-decode-encode/dist'; import { ZodError } from 'zod'; const resolveName = (path) => { return path .map((pathPart, i) => { return i > 0 ? `[${pathPart}]` : pathPart; }) .join(''); }; const filterContext = (rest) => { if (isObject(rest)) { return Object.fromEntries(Object.entries(rest).map(([key, value]) => [key, filterContext(value)])); } if (isArray(rest)) { return rest.map(filterContext); } if (isNull(rest) || isBoolean(rest) || isNumber(rest) || isString(rest)) { return rest; } if (rest instanceof ZodError) { return zodToInvalidParameters(rest); } if (rest instanceof Date) { return rest.toJSON(); } return '**filtered**'; }; export const zodToInvalidParameters = (zodError) => { return zodError.errors.map((error) => { const { path, message, ...context } = error; return { name: resolveName(path), reason: message, context: filterContext(context), }; }); };