ts-valid8
Version:
A next-generation TypeScript validation library with advanced features
145 lines (114 loc) âĒ 4.22 kB
Markdown
[](https://www.npmjs.com/package/ts-valid8)
[](https://github.com/your-actual-username/ts-valid8/actions)
[](https://codecov.io/gh/your-actual-username/ts-valid8)
[](https://www.npmjs.com/package/ts-valid8)
[](https://bundlephobia.com/package/ts-valid8)
[](https://opensource.org/licenses/MIT)
A next-generation TypeScript validation library with advanced features including conditional validation, cross-field validation, and plugin system.
- â
**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
```bash
npm install ts-valid8
yarn add ts-valid8
pnpm add ts-valid8
```
```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);
}
```
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()
)
)
});
```
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'
)
)
});
```
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);
```
For complete API documentation, visit [our docs website](
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.