PitchHut logo
Jet-Schema
A simple, TypeScript-first schema validation tool.
Pitch

Jet-Schema is a zero-dependency validation tool designed for TypeScript. It empowers you to define custom validator functions effortlessly, simplifying object property validation without relying on heavy libraries. Enhance your TypeScript projects with streamlined schema validation.

Description

jet-schema ✈️

jet-schema is a simple, zero-dependency, TypeScript-first schema validation tool designed to streamline the process of validating schema properties in TypeScript applications. With a focus on flexibility, you can leverage your own custom validator functions without cumbersome setups. This allows you to create robust and readable validation logic while minimizing overhead.

Key Features

  • Custom Validator Functions: Easily create your own list of validator functions for your application, eliminating the need for a specific library and providing greater reusability.
  • Lightweight: The library has a small footprint, exporting only four functions and types, totaling 4.7kB when minified.
  • No Compilation Step: Works seamlessly with ts-node, making it perfect for rapid development without the hassle of compiling beforehand.
  • Bidirectional Type Safety: Infer types from schemas or enforce properties within a schema using generics.
  • Versatile Use: Compatible with both client-side and server-side implementations, ensuring broad applicability.
  • High Performance: Proven performance, outperforming many other schema validation libraries in benchmarks.

Quick Glance

Here’s a simple example to get started:

import schema from 'utils/schema';
import { isString, isNumber } from 'utils/validators';

interface IUser {
  id: number;
  name: string;
  address: {
    street: string;
    zip: number;
  }
}

const User = schema<IUser>({
  id: isNumber,
  name: isString,
  address: schema({
    street: isString,
    zip: isNumber,
  })
});

User.new({ id: 5, name: 'joe' }) // => { id: 5, name: 'joe' }
User.test('asdf') // => false
User.pick('name').test('john') // => true
User.pick('address').pick('zip').test(234) // => true
User.parse('something') // => Error

How It Compares to Other Libraries

Unlike many existing libraries, jet-schema allows you to directly utilize your validator functions, making it easier to validate properties without diving deep into documentation each time. This helps maintain clean, compact code as demonstrated below:

const User = schema<IUser>({
  id: isRelationalKey,
  name: isString,
  email: { vf: isEmail, default: 'x@x.x' },
  age: { vf: isNumber, transform: Number },
  created: Date,
  address: schema({
    street: isString,
    zip: isNumber,
    country: isOptionalString,
  }, { optional: true }),
});

Additional Properties

  • Easy Schema Creation: Define schemas directly or use configuration objects to manage individual validation rules.
  • Error Customization: You can package error messages using the built-in IError interface, enhancing clarity and traceability.
  • Support for Nested Objects: Easily define schemas for nested properties and provide type-safe access to schema functions.
  • Flexible Defaults and Transformations: Set default values and transformation logic for properties seamlessly within your schemas.

Bonus Features

  • Use the Date constructor as a validator to automatically convert various date formats to a Date object.
  • Support for enums as validators, enriching your type safety without additional complexity.

With jet-schema, you have a powerful, flexible, and efficient schema validation tool at your disposal. Whether you're building client-side applications or server-side logic, its TypeScript-first design makes it an excellent choice for any developer looking to enhance code quality and maintainability.

0 comments

No comments yet.

Sign in to be the first to comment.