A simple, TypeScript-first schema validation tool.
Project details
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.
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.
ts-node, making it perfect for rapid development without the hassle of compiling beforehand.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
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 }),
});
IError interface, enhancing clarity and traceability.Date constructor as a validator to automatically convert various date formats to a Date object.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.
Comments
0Start the conversation
Share the first comment.