1.1.3 • Published 7 months ago
@caeljs/tsh v1.1.3
# Type-Safe Validation Library
A robust, type-safe validation library for TypeScript that provides runtime validation while maintaining full type safety.
## Features
- 🛠️ **Type-safe validators** with TypeScript inference
- 🧩 **Composable API** for building complex validation schemas
- 🔍 **Runtime validation** with detailed error reporting
- 🏗️ **Object shape validation** with nested structures
- 🔄 **Coercion support** for common type conversions
- ✨ **Utility functions** for common validation patterns
## Installation
```bash
npm install @caeljs/tsh
# or
yarn add @caeljs/tshBasic Usage
import { t } from '@caeljs/tsh';
// Define a schema
const userSchema = object({
name: t.string().required(),
age: t.number().positive().int(),
email: t.string().email(),
tags: t.array(t.string()).minLength(1),
});
// Validate data
const result = t.validate({
name: "Drylian",
age: 23,
email: "drylian@example.com",
tags: ["developer", "typescript"]
}, userSchema);
if (result.success) {
console.log("Valid data:", result.data);
} else {
console.error("Validation error:", result.error);
}Core Validators
Primitive Validators
t.string()- Validates string valuest.number()- Validates number valuest.boolean()- Validates boolean valuest.any()- Accepts any valuet.enum()- Validates against enum values
Complex Validators
t.object(shape)- Validates object structurest.array(shape)- Validates arrayst.record(keyShape, valueShape)- Validates dictionary/map structurest.union(shapes)- Validates against multiple possible shapes
Modifiers
.optional()- Makes a field optional.nullable()- Allows null values.required()- Requires non-null, non-undefined values.partial()- Makes all object properties optional.refine(predicate, message)- Adds custom validation
Utility Functions
Validation
t.validate(value, shape)- Validates a value against a shapet.isValid(value, shape)- Checks if a value is valid
Object Manipulation
t.pick(shape, keys)- Creates a shape with only specified keyst.omit(shape, keys)- Creates a shape without specified keyst.merge(shape1, shape2)- Merges two object shapest.extend(shape, extensions)- Extends an object shape
Array Utilities
t.nonEmptyArray(shape)- Requires non-empty arrayst.uniqueArray(shape)- Requires unique array elementst.minLength(shape, min)- Sets minimum array lengtht.maxLength(shape, max)- Sets maximum array length
Common Validators
String Validators
t.email()- Validates email formatt.uuid()- Validates UUID formatt.url()- Validates URL formatt.ip()- Validates IP addresst.regex(pattern)- Validates against regex
Number Validators
t.int()- Validates integerst.positive()- Validates positive numberst.negative()- Validates negative numberst.port()- Validates port numberst.percentage()- Validates percentages
Advanced Features
Logical Operators
t.and(shape1, shape2)- Requires both shapes to match (intersection)t.or(shape1, shape2)- Requires either shape to match (union)t.not(shape)- Requires value to NOT match shape
Coercion
import { t } from '@caeljs/tsh';
// Coerce values to specific types
const coercedString = t.coerce.string(); // Converts value to string
const coercedNumber = t.coerce.number(); // Converts value to number
const coercedBoolean = t.coerce.boolean(); // Converts value to booleanCustom Validators
import { t } from '@caeljs/tsh';
// Create custom validator
const evenNumber = t.custom(
(value): value is number => typeof value === 'number' && value % 2 === 0,
'Value must be an even number'
);Error Handling
All validation errors include detailed information about what failed:
try {
shape.parse(invalidValue);
} catch (error) {
if (error instanceof TshShapeError) {
console.error(error.message);
console.error(error.path); // Path to invalid field
console.error(error.value); // Invalid value
}
}Type Safety
The library provides full TypeScript type inference:
const userSchema = t.object({
name: t.string(),
age: t.number(),
});
type User = t.infer<typeof userSchema>;
// Equivalent to:
// type User = {
// name: string;
// age: number;
// }Random Value Generation
import { random, randomInt, randomUuid } from '@caeljs/tsh';
t.random(); // Random string
t.randomInt(1, 100); // Random integer between 1-100
t.randomUuid(); // Random UUID v4Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
MIT