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

@voidhaus/monoschema-transformer

v0.0.2

Published

A data transformation plugin for [monoschema](https://www.npmjs.com/package/@voidhaus/monoschema) that allows you to transform data before validation. Transform strings to numbers, dates, booleans and more with type-safe transformers.

Readme

@voidhaus/monoschema-transformer

A data transformation plugin for monoschema that allows you to transform data before validation. Transform strings to numbers, dates, booleans and more with type-safe transformers.

Features

  • Pre-validation data transformation - Transform data before schema validation
  • Built-in transformers for common type conversions
  • Custom transformer support - Create your own transformers
  • Type-safe - Full TypeScript support with proper type inference
  • Composable - Chain multiple transformers together
  • Error handling - Clear error messages with path information

Installation

npm install @voidhaus/monoschema-transformer
# or
yarn add @voidhaus/monoschema-transformer
# or
pnpm add @voidhaus/monoschema-transformer

Note: This package requires @voidhaus/monoschema as a peer dependency.

Quick Start

import { createSchema } from '@voidhaus/monoschema';
import { transformerPlugin, stringToNumber, stringToBoolean } from '@voidhaus/monoschema-transformer';

// Configure monoschema with the transformer plugin
const schema = createSchema({
  plugins: [transformerPlugin]
});

// Define a schema with transformers
const userSchema = schema.object({
  name: schema.string(),
  age: schema.number({
    $transformers: [stringToNumber()]
  }),
  isActive: schema.boolean({
    $transformers: [stringToBoolean()]
  })
});

// Transform and validate data
const result = userSchema.validate({
  name: "John Doe",
  age: "25",        // Will be transformed to number 25
  isActive: "true"  // Will be transformed to boolean true
});

console.log(result); 
// { name: "John Doe", age: 25, isActive: true }

Built-in Transformers

String Transformers

stringToNumber()

Converts string values to numbers.

const schema = createSchema({ plugins: [transformerPlugin] });
const numberSchema = schema.number({
  $transformers: [stringToNumber()]
});

numberSchema.validate("123");    // → 123
numberSchema.validate("123.45"); // → 123.45
numberSchema.validate("1e3");    // → 1000
// numberSchema.validate("abc");    // → throws error

stringToBoolean()

Converts string values to booleans (case-insensitive).

const boolSchema = schema.boolean({
  $transformers: [stringToBoolean()]
});

boolSchema.validate("true");  // → true
boolSchema.validate("TRUE");  // → true
boolSchema.validate("false"); // → false
boolSchema.validate("FALSE"); // → false
// boolSchema.validate("yes");   // → throws error

stringToDate()

Converts string values to Date objects.

const dateSchema = schema.custom<Date>({
  $transformers: [stringToDate()]
});

dateSchema.validate("2023-12-25T10:30:00.000Z"); // → Date object
dateSchema.validate("2023-12-25");               // → Date object
// dateSchema.validate("invalid-date");            // → throws error

Type to String Transformers

numberToString()

Converts numbers to strings.

const stringSchema = schema.string({
  $transformers: [numberToString()]
});

stringSchema.validate(123);   // → "123"
stringSchema.validate(123.45); // → "123.45"

booleanToString()

Converts booleans to strings.

const stringSchema = schema.string({
  $transformers: [booleanToString()]
});

stringSchema.validate(true);  // → "true"
stringSchema.validate(false); // → "false"

dateToString()

Converts Date objects to ISO strings.

const stringSchema = schema.string({
  $transformers: [dateToString()]
});

stringSchema.validate(new Date("2023-12-25")); // → "2023-12-25T00:00:00.000Z"

Chaining Transformers

You can chain multiple transformers together by providing them in an array:

const customSchema = schema.string({
  $transformers: [
    stringToNumber(),    // First: string → number
    numberToString()     // Then: number → string
  ]
});

customSchema.validate("123"); // → "123" (string → number → string)

Custom Transformers

Create your own transformers by implementing the Transformer interface:

import type { Transformer } from '@voidhaus/monoschema-transformer';

const trimString = (): Transformer => ({
  input: String,
  output: String,
  transform: (value: string): string => {
    return value.trim();
  }
});

const upperCaseString = (): Transformer => ({
  input: String,
  output: String,
  transform: (value: string): string => {
    return value.toUpperCase();
  }
});

// Use custom transformers
const nameSchema = schema.string({
  $transformers: [trimString(), upperCaseString()]
});

nameSchema.validate("  john doe  "); // → "JOHN DOE"

Error Handling

Transformers provide clear error messages with path information:

const userSchema = schema.object({
  profile: schema.object({
    age: schema.number({
      $transformers: [stringToNumber()]
    })
  })
});

try {
  userSchema.validate({
    profile: {
      age: "not-a-number"
    }
  });
} catch (error) {
  console.log(error.message); 
  // "profile.age: Cannot convert "not-a-number" to a number"
}

API Reference

Plugin

transformerPlugin

The main plugin to register with monoschema. Add this to your schema configuration to enable transformer support.

Types

Transformer

type TransformerObject = {
  input: any;
  output: any;
  transform: (value: any) => any;
};

type Transformer = () => TransformerObject;

$transformers Schema Property

Add transformers to any schema using the $transformers property:

const schema = {
  // ... other schema properties
  $transformers: [transformer1(), transformer2(), ...]
}

Integration with monoschema

This package is designed to work seamlessly with the monoschema ecosystem:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License. See LICENSE file for details.