PitchHut logo
Log in / Sign up
jet-schema
by sean-maxwell
Editor's pick
A simple, typescript-first schema validation tool.
Pitch

Jet-Schema offers a straightforward, TypeScript-first approach to schema validation, allowing you to implement your custom validator functions easily. With a lightweight footprint and zero dependencies, it empowers you to validate object properties without being tied to any specific library, ensuring flexibility and control.

Description

jet-schema is a straightforward and efficient TypeScript-first schema validation tool designed to improve your development workflow by allowing you to utilize your own custom validator functions. This zero-dependency library prioritizes simplicity and flexibility, making it easier to validate object properties without the need to consult extensive documentation each time you create or modify a validation function.

Key Features:

  • Use Custom Validators: Tailor the validation process by integrating your specific validator functions with object properties, ensuring a seamless validation experience tailored to your project's needs.
  • Lightweight Library: The entire package is only 2.2 KB minified and zipped, making it one of the most compact schema validation tools available.
  • Type Safety: Achieve robust type safety in both directions—you can impose a schema structure from a predefined type or infer a type directly from a schema.
  • Core Functions: Automatically gain access to the essential .new, .test, and .parse functions with every new schema you create, simplifying the coding process.
  • Flexible Configuration: Configure settings on both global and local levels, allowing you to define default behaviors and rules for multiple schemas efficiently.
  • Cross-Environment Compatibility: Use jet-schema seamlessly on both client-side and server-side applications, enhancing versatility.
  • Built-in Support for Enums and Dates: Validate against enum values and transform date representations directly into Date objects for easy manipulation.

Example Usage:

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

interface IUser {
    id: number;
    name: string;
}

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

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

Benefits of Using jet-schema:

  • Reduction in Complexity: Compared to other schema validation libraries, jet-schema offers a more streamlined approach, emphasizing simplicity and user-friendliness.
  • Integration without Compilation: Enjoy development without the overhead of a compilation step, ensuring swift deployment in environments that support ts-node.
  • Customizable Error Reporting: Implement your own error handling logic to provide detailed feedback during validation processes.

Conclusion:

If you're looking for a robust, TypeScript-friendly library that empowers you to harness your custom validation logic while keeping your application lightweight and efficient, jet-schema could be the perfect choice for you. Whether developing in TypeScript or JavaScript, integrate this tool into your project and transform the way you handle schema validations.

8 comments
thresh
Nov 30, 2024

This is cool! Impressive that it all fits into 5kb. One thing to consider: zod has kind of become the go-to in the TS ecosystem. A lot of sdks like openai work with zod objects directly, and tools like zod-to-json-schema make it easy to keep zod schemas as the source of truth.

For jet-schema to really catch on, it might help to have similar converters. At the very least, something like a jet-schema-to-zod utility (either built-in or as a separate package) could make it easier for people to migrate or integrate into existing workflows.

sean-maxwell
Dec 1, 2024

thanks for the comment, I could definitely convert z.object to schema<>().... but because jet-schema relies on having your own validator functions, I'm not sure how I'd convert z.string() to whatever function you wrote for making sure something is a string

thresh
Dec 2, 2024

I guess converting to zod schema matters more than converting from as people who want to make the switch to jet-schema can refactor manually but they'll need to autoconvert back to zod to interact with other sdks. Maybe you can pass the validators to z.object().refine() but not sure how that'll affect inference.. could work..

seymurkafkas
Dec 1, 2024

Very cool project, starred.

sean-maxwell
Dec 1, 2024

thanks, any suggestions for improvements are welcome

anar
Dec 4, 2024

Same here, starred! I like jet-schema's API. We're launching a new 'Top Projects' page in a few days. Mind if we feature jet-schema there?

Also one minor suggestion, Sean. In the Quick Glance section on Github, I'd recommend removing the IUser interface in favor of inferType. One of zod's main appeals is the ability to use z.infer<typeof schema> and first-time users might mistakenly assume that jet-schema requires types to be declared twice.

sean-maxwell
Dec 4, 2024

You can absolutely feature jet-schema there, thanks. I updated the readme as you suggested.

anar
Dec 4, 2024

Awesome!

Sign in to comment.