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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nerd-bible/valio

v0.0.12

Published

Encode and decode Typescript types with extensible error handling.

Downloads

18

Readme

valio

Encode and decode Typescript types with extensible error handling.

Why?

I like Zod, but its codecs don't support custom error contexts. I tried adding support to Zod but found it easier to start from scratch.

Theory

  • Data flows through "pipes" with specific input and output types.
    • Both input and output are validated
  • Pipes are bidirectional.
    • Input -> output is "decoding"
    • Output -> input is "encoding"
  • When encoding or decoding a Context is passed which holds:
    • The path to the current value
    • Error formatting function
    • Errors grouped by path

Practice

Use classes, inheritance, and result types. No errors are thrown.

Usage

For primitive types the input and output are the same.

import * as v from "@nerd-bible/valio";

const schema = v.number(); // Pipe<number, number>

expect(schema.decode(5)).toEqual({ success: true, output: 5 });
expect(schema.decodeAny("5")).toEqual({
success: false,
errors: { ".": [{ input: "5", message: "not type number" }] },
});

You can add custom checks, but be sure to provide context for error messages.

import * as v from "@nerd-bible/valio";

const schema = v.number().refine((n) => n == 5, "eq", { n: 5 });

expect(schema.decode(5)).toEqual({ success: true, output: 5 });
expect(schema.encode(3)).toEqual({
	success: false,
	errors: { ".": [{ input: 3, message: "must be 5" }] },
});

There are common builtin codecs for coercion.

import * as v from "@nerd-bible/valio";

const schema = v.codecs.number();

expect(schema.decode("13")).toEqual({ success: true, output: 13 });

Extending

To make a codec with a custom transformer, you can use v.codecs.custom.

import * as v from "@nerd-bible/valio";

function transformToNumber(any: number): number {
	if (typeof any == "number") return any;
	return NaN;
}

const schema = v.codecs.custom(
	v.union([v.string(), v.number(), v.null(), v.undefined()]),
	v.number(),
	{ decode: transformToNumber },
);

To make a pipe with any types and transformers, simply extend the class with your input and output HalfPipes.

import { Pipe, HalfPipe } from "@nerd-bible/valio";

function transformToNumber(any: number): number {
	if (typeof any == "number") return any;
	return NaN;
}

class AnyToNumber extends Pipe<any, number> {
	constructor() {
		super(
			new HalfPipe("any", (v): v is any => true, transformToNumber),
			new HalfPipe("number", (v): v is number => typeof v == "number"),
		);
	}
}

Custom error messages

Override errorFmt in Context and pass it to encode and decode.

import * as v from "@nerd-bible/valio";

const schema = v.number().refine((n) => n == 5, "eq", { n: 5 });
class MyContext extends v.Context {
	errorFmt() {
		return "You done messed up";
	}
}

expect(schema.decode(3, new MyContext())).toEqual({
	success: false,
	errors: { ".": [{ input: 3, message: "You done messed up" }] },
});