npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hutechwebsite/alias-rerum-dicta-ad

v1.0.0

Published

Wrap zod validation errors in user-friendly readable messages.

Downloads

39

Readme

@hutechwebsite/alias-rerum-dicta-ad

Wrap zod validation errors in user-friendly readable messages.

Build Status npm version

Features

  • User-friendly readable messages, configurable via options;
  • Maintain original issues under error.details;
  • Extensive tests.

Installation

npm install @hutechwebsite/alias-rerum-dicta-ad

Requirements

  • Node.js v.18+
  • TypeScript v.4.5+

Quick start

import { z as zod } from 'zod';
import { fromError } from '@hutechwebsite/alias-rerum-dicta-ad';

// create zod schema
const zodSchema = zod.object({
  id: zod.number().int().positive(),
  email: zod.string().email(),
});

// parse some invalid value
try {
  zodSchema.parse({
    id: 1,
    email: 'foobar', // note: invalid email
  });
} catch (err) {
  const validationError = fromError(err);
  // the error is now readable by the user
  // you may print it to console
  console.log(validationError.toString());
  // or return it as an actual error
  return validationError;
}

Motivation

Zod errors are difficult to consume for the end-user. This library wraps Zod validation errors in user-friendly readable messages that can be exposed to the outer world, while maintaining the original errors in an array for dev use.

Example

Input (from Zod)

[
  {
    "code": "too_small",
    "inclusive": false,
    "message": "Number must be greater than 0",
    "minimum": 0,
    "path": ["id"],
    "type": "number"
  },
  {
    "code": "invalid_string",
    "message": "Invalid email",
    "path": ["email"],
    "validation": "email"
  }
]

Output

Validation error: Number must be greater than 0 at "id"; Invalid email at "email"

API

ValidationError

Main ValidationError class, extending native JavaScript Error.

Arguments

  • message - string; error message (required)
  • options - ErrorOptions; error options as per JavaScript definition (optional)
    • options.cause - any; can be used to hold the original zod error (optional)

Example 1: construct new ValidationError with message

const { ValidationError } = require('@hutechwebsite/alias-rerum-dicta-ad');

const error = new ValidationError('foobar');
console.log(error instanceof Error); // prints true

Example 2: construct new ValidationError with message and options.cause

import { z as zod } from 'zod';
const { ValidationError } = require('@hutechwebsite/alias-rerum-dicta-ad');

const error = new ValidationError('foobar', {
  cause: new zod.ZodError([
    {
      code: 'invalid_string',
      message: 'Invalid email',
      path: ['email'],
      validation: 'email',
    },
  ]),
});

console.log(error.details); // prints issues from zod error

errorMap

A custom error map to use with zod's setErrorMap method and get user-friendly messages automatically.

Example

import { z as zod } from 'zod';
import { errorMap } from '@hutechwebsite/alias-rerum-dicta-ad';

zod.setErrorMap(errorMap);

isValidationError

A type guard utility function, based on instanceof comparison.

Arguments

  • error - error instance (required)

Example

import { z as zod } from 'zod';
import { ValidationError, isValidationError } from '@hutechwebsite/alias-rerum-dicta-ad';

const err = new ValidationError('foobar');
isValidationError(err); // returns true

const invalidErr = new Error('foobar');
isValidationError(err); // returns false

isValidationErrorLike

A type guard utility function, based on heuristics comparison.

Why do we need heuristics if we have instanceof comparison? Because of multi-version inconsistencies. For instance, it's possible that a dependency is using an older @hutechwebsite/alias-rerum-dicta-ad version internally. In such case, the instanceof comparison will yield invalid results because module deduplication does not apply at npm/yarn level and the prototype is different.

In most cases, it is safer to use isValidationErrorLike than isValidationError.

Arguments

  • error - error instance (required)

Example

import { ValidationError, isValidationErrorLike } from '@hutechwebsite/alias-rerum-dicta-ad';

const err = new ValidationError('foobar');
isValidationErrorLike(err); // returns true

const invalidErr = new Error('foobar');
isValidationErrorLike(err); // returns false

fromError

Converts an error to ValidationError.

Why is the difference between fromError and fromZodError? The fromError is a less strict version of fromZodError that can accept an unknown error and attempt to convert it to a ValidationError.

Arguments

  • error - unknown; an error (required)
  • options - Object; formatting options (optional)
    • maxIssuesInMessage - number; max issues to include in user-friendly message (optional, defaults to 99)
    • issueSeparator - string; used to concatenate issues in user-friendly message (optional, defaults to ";")
    • unionSeparator - string; used to concatenate union-issues in user-friendly message (optional, defaults to ", or")
    • prefix - string or null; prefix to use in user-friendly message (optional, defaults to "Validation error"). Pass null to disable prefix completely.
    • prefixSeparator - string; used to concatenate prefix with rest of the user-friendly message (optional, defaults to ": "). Not used when prefix is null.
    • includePath - boolean; used to provide control on whether to include the erroneous property name suffix or not (optional, defaults to true).

fromZodIssue

Converts a single zod issue to ValidationError.

Arguments

  • zodIssue - zod.ZodIssue; a ZodIssue instance (required)
  • options - Object; formatting options (optional)
    • issueSeparator - string; used to concatenate issues in user-friendly message (optional, defaults to ";")
    • unionSeparator - string; used to concatenate union-issues in user-friendly message (optional, defaults to ", or")
    • prefix - string or null; prefix to use in user-friendly message (optional, defaults to "Validation error"). Pass null to disable prefix completely.
    • prefixSeparator - string; used to concatenate prefix with rest of the user-friendly message (optional, defaults to ": "). Not used when prefix is null.
    • includePath - boolean; used to provide control on whether to include the erroneous property name suffix or not (optional, defaults to true).

fromZodError

Converts zod error to ValidationError.

Why is the difference between ZodError and ZodIssue? A ZodError is a collection of 1 or more ZodIssue instances. It's what you get when you call zodSchema.parse().

Arguments

  • zodError - zod.ZodError; a ZodError instance (required)
  • options - Object; formatting options (optional)
    • maxIssuesInMessage - number; max issues to include in user-friendly message (optional, defaults to 99)
    • issueSeparator - string; used to concatenate issues in user-friendly message (optional, defaults to ";")
    • unionSeparator - string; used to concatenate union-issues in user-friendly message (optional, defaults to ", or")
    • prefix - string or null; prefix to use in user-friendly message (optional, defaults to "Validation error"). Pass null to disable prefix completely.
    • prefixSeparator - string; used to concatenate prefix with rest of the user-friendly message (optional, defaults to ": "). Not used when prefix is null.
    • includePath - boolean; used to provide control on whether to include the erroneous property name suffix or not (optional, defaults to true).

toValidationError

A curried version of fromZodError meant to be used for FP (Functional Programming). Note it first takes the options object if needed and returns a function that converts the zodError to a ValidationError object

toValidationError(options) => (zodError) => ValidationError

Example using fp-ts

import * as Either from 'fp-ts/Either';
import { z as zod } from 'zod';
import { toValidationError, ValidationError } from '@hutechwebsite/alias-rerum-dicta-ad';

// create zod schema
const zodSchema = zod
  .object({
    id: zod.number().int().positive(),
    email: zod.string().email(),
  })
  .brand<'User'>();

export type User = zod.infer<typeof zodSchema>;

export function parse(
  value: zod.input<typeof zodSchema>
): Either.Either<ValidationError, User> {
  return Either.tryCatch(() => schema.parse(value), toValidationError());
}

FAQ

How to distinguish between errors

Use the isValidationErrorLike type guard.

Example

Scenario: Distinguish between ValidationError VS generic Error in order to respond with 400 VS 500 HTTP status code respectively.

import * as Either from 'fp-ts/Either';
import { z as zod } from 'zod';
import { isValidationErrorLike } from '@hutechwebsite/alias-rerum-dicta-ad';

try {
  func(); // throws Error - or - ValidationError
} catch (err) {
  if (isValidationErrorLike(err)) {
    return 400; // Bad Data (this is a client error)
  }

  return 500; // Server Error
}

How to use ValidationError outside zod

It's possible to implement custom validation logic outside zod and throw a ValidationError.

Example 1: passing custom message

import { ValidationError } from '@hutechwebsite/alias-rerum-dicta-ad';
import { Buffer } from 'node:buffer';

function parseBuffer(buf: unknown): Buffer {
  if (!Buffer.isBuffer(buf)) {
    throw new ValidationError('Invalid argument; expected buffer');
  }

  return buf;
}

Example 2: passing custom message and original error as cause

import { ValidationError } from '@hutechwebsite/alias-rerum-dicta-ad';

try {
  // do something that throws an error
} catch (err) {
  throw new ValidationError('Something went deeply wrong', { cause: err });
}

How to use ValidationError with custom "error map"

Zod supports customizing error messages by providing a custom "error map". You may combine this with @hutechwebsite/alias-rerum-dicta-ad to produce user-friendly messages.

Example 1: produce user-friendly error messages using the errorMap property

If all you need is to produce user-friendly error messages you may use the errorMap property.

import { z as zod } from 'zod';
import { errorMap } from '@hutechwebsite/alias-rerum-dicta-ad';

zod.setErrorMap(errorMap);

Example 2: extra customization using fromZodIssue

If you need to customize some error code, you may use the fromZodIssue function.

import { z as zod } from 'zod';
import { fromZodIssue } from '@hutechwebsite/alias-rerum-dicta-ad';

const customErrorMap: zod.ZodErrorMap = (issue, ctx) => {
  switch (issue.code) {
    case ZodIssueCode.invalid_type: {
      return {
        message:
          'Custom error message of your preference for invalid_type errors',
      };
    }
    default: {
      const validationError = fromZodIssue({
        ...issue,
        // fallback to the default error message
        // when issue does not have a message
        message: issue.message ?? ctx.defaultError,
      });

      return {
        message: validationError.message,
      };
    }
  }
};

zod.setErrorMap(customErrorMap);

How to use @hutechwebsite/alias-rerum-dicta-ad with react-hook-form?

import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { errorMap } from '@hutechwebsite/alias-rerum-dicta-ad';

useForm({
  resolver: zodResolver(schema, { errorMap }),
});

Does @hutechwebsite/alias-rerum-dicta-ad support CommonJS

Yes, @hutechwebsite/alias-rerum-dicta-ad supports CommonJS out-of-the-box. All you need to do is import it using require.

Example

const { ValidationError } = require('@hutechwebsite/alias-rerum-dicta-ad');

Contribute

Source code contributions are most welcome. Please open a PR, ensure the linter is satisfied and all tests pass.

We are hiring

Causaly is building the world's largest biomedical knowledge platform, using technologies such as TypeScript, React and Node.js. Find out more about our openings at https://apply.workable.com/causaly/.

License

MIT