UNPKG

ts-valid8

Version:

A next-generation TypeScript validation library with advanced features

145 lines (114 loc) â€Ē 4.22 kB
# ts-valid8 [![npm version](https://img.shields.io/npm/v/ts-valid8.svg)](https://www.npmjs.com/package/ts-valid8) [![Build Status](https://github.com/your-actual-username/ts-valid8/workflows/CI%2FCD/badge.svg)](https://github.com/your-actual-username/ts-valid8/actions) [![codecov](https://codecov.io/gh/your-actual-username/ts-valid8/branch/main/graph/badge.svg)](https://codecov.io/gh/your-actual-username/ts-valid8) [![npm downloads](https://img.shields.io/npm/dm/ts-valid8.svg)](https://www.npmjs.com/package/ts-valid8) [![bundle size](https://img.shields.io/bundlephobia/minzip/ts-valid8)](https://bundlephobia.com/package/ts-valid8) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) A next-generation TypeScript validation library with advanced features including conditional validation, cross-field validation, and plugin system. ## Features - ✅ **Fully Typed**: Built from the ground up with TypeScript - 🔄 **Composable**: Mix and match validation rules - ðŸ§Đ **Extensible**: Plugin system for custom validators - 🔗 **Cross-field Validation**: Validate fields dependent on other fields - 🔍 **Conditional Logic**: Apply different validation rules based on conditions - 🌐 **Browser & Node.js**: Works in all environments - ðŸŠķ **Lightweight**: No dependencies, tree-shakeable - 🔍 **Detailed Errors**: Get specific error messages for each validation failure ## Installation ```bash npm install ts-valid8 # or yarn add ts-valid8 # or pnpm add ts-valid8 ``` ## Basic Usage ```typescript import { string, number, object, array } from 'ts-valid8'; // Define a schema const userSchema = object({ name: string().min(2).max(50).required(), email: string().email().required(), age: number().min(18).integer(), tags: array().of(string()).min(1).max(5) }); // Validate some data const result = userSchema.validate({ name: 'John Doe', email: 'john@example.com', age: 25, tags: ['developer', 'typescript'] }); if (result.success) { // Data is valid, use result.data (fully typed!) const user = result.data; console.log(`Welcome ${user.name}!`); } else { // Data is invalid, show errors console.error(result.errors); } ``` ## Advanced Features ### Conditional Validation Apply different validation rules based on input values: ```typescript import { string, object, conditional } from 'ts-valid8'; const schema = object({ paymentType: string().oneOf(['credit', 'bank']), creditCard: string().use( conditional( (value, obj) => obj?.paymentType === 'credit', (s) => s.pattern(/^\d{16}$/).required(), (s) => s.optional() ) ), bankAccount: string().use( conditional( (value, obj) => obj?.paymentType === 'bank', (s) => s.minLength(8).required(), (s) => s.optional() ) ) }); ``` ### Cross-field Validation Validate fields based on other fields: ```typescript import { string, object, crossfield } from 'ts-valid8'; const schema = object({ password: string().min(8).required(), confirmPassword: string().use( crossfield( 'password', (confirm, password) => confirm === password, 'Passwords must match' ) ) }); ``` ### Custom Validators Create your own validators: ```typescript import { string, Plugin } from 'ts-valid8'; // Create a custom validation plugin const isUsernameAvailable: Plugin<string> = (schema) => { return schema.refine( async (value) => { // Check username availability (e.g., API call) const response = await fetch(`/api/check-username?username=${value}`); const { available } = await response.json(); return available; }, 'Username is already taken' ); }; // Use it in your schema const schema = string().use(isUsernameAvailable); ``` ## API Documentation For complete API documentation, visit [our docs website](#). ## Contributing Contributions are welcome! Please feel free to submit a Pull Request. ## License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.