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

jointz

v7.0.4

Published

✅ Zero dependency universal TypeScript validation library

Downloads

132

Readme

jointz

Tests codecov npm npm bundle size (minified + gzip) code style: prettier

Zero dependency universal TypeScript validation library. Similar interface to Joi but without all the bloat, and built for browsers and node with zero dependencies.

Unlike Joi, the goal is to write validation code only once, in the code where it's used, and from that can export all the necessary information (TypeScript types, JSON schema).

Features

  • Written in TypeScript
  • Zero dependencies, tiny, and well tested
  • Pretty fast
  • Infer TypeScript types from validators
  • Produce JSON schema from validators
  • Supports any, string, number, array, tuple, constant, boolean, or and object validation
  • Implement your own validator and use it with any of the other validators
  • Fluid immutable interface
  • Targets both browsers and node

Installation

You can install it via npm

npm i --save jointz

or yarn

yarn add jointz

Usage

Import the default export from jointz

import jointz from 'jointz';

Then use it to construct validators

const ThingValidator = jointz.object({
  id: jointz.string().uuid(),
  name: jointz.string().minLength(3).maxLength(100)
}).requiredKeys(['id', 'name']);

The validator can now be used to check for errors. Validator#validate returns an array of validation errors, which is empty if the value passes validation.

const myObject = { id: 'abc', name: 'hello world!' };

const errors = ThingValidator.validate(myObject); // expect an error because id is not a uuid

if (errors.length) {
  // Fail
} else {
  // Continue
}

You can also generate TypeScript types from your validators, using Infer, or get the resulting type from validation via checkValid.

type Thing = Infer<typeof ThingValidator>;

const myObject: unknown = { id: 'abc', name: 'hello world!' };

try {
  const thing: Thing = ThingValidator.checkValid(myObject);
} catch (validationError) {
  console.log(validationError.errors);
}

jointz validators also expose the type guard function isValid on every validator.

const myObject: unknown = { id: 'abc', name: 'hello world!' };

if (ThingValidator.isValid(myObject)) {
    // This works because myObject is a valid Thing
    const id: string = myObject.id;
}

JSON Schema

All validators expose a method #toJsonSchema(): JSONSchema7 which return the JSON schema corresponding to the given type.

Errors

Errors match the following interface:

interface ValidationError {
  // Array of keys indicating where the validation failed. This is empty if top level validation failed.
  path: Array<string | number>;
  // The error message describing the failed validation.
  message: string;
  // The value that failed validation. This value may not be defined, e.g. in the case of missing required keys.
  value?: unknown;
}

A single validator can produce many errors. However, validators will only produce relevant errors, e.g. a number validator that checks a number is a multiple of 2 will not produce an additional error about the multiple when validating a string.

| Validator | Example | Error.path | Error.message | Error.value | |-----------------------------------------------------------------------|-------------------|-------------|------------------------------------|-------------| | jointz.number() | 'abc' | [] | 'must be a number' | 'abc' | | jointz.string() | 3 | [] | 'must be a string' | 3 | | jointz.object({ abc: jointz.string() }) | {abc:3} | ['abc'] | 'must be a string' | 3 | | jointz.object({ arr: jointz.array(jointz.number().multipleOf(2)) }) | {arr:[2,'5',8]} | ['arr',1] | 'must be a number' | '5' | | jointz.object({ arr: jointz.array(jointz.number().multipleOf(2)) }) | {arr:[2,5,8]} | ['arr',1] | 'number was not a multiple of 2' | 5 |