inu-light 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +83 -32
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
# inu-light
|
|
2
2
|
|
|
3
|
-
A lightweight, zero-dependency schema validation library for TypeScript and
|
|
3
|
+
A lightweight, zero-dependency schema validation library for TypeScript. Designed to improve runtime data safety with schema-driven validation and strict avoidance of the any type.
|
|
4
4
|
|
|
5
5
|
## Design philosophy
|
|
6
6
|
|
|
7
|
-
inu-light
|
|
7
|
+
Most schema libraries are either heavy or rely on opaque runtime magic. inu-light is intentionally minimal and predictable: the schema is the single source of truth. Advanced TypeScript generics provide precise inference, so external data cannot silently break your types at runtime.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- **Zero Dependencies**: minimal footprint, no runtime bloat
|
|
10
10
|
|
|
11
|
-
- **
|
|
12
|
-
|
|
13
|
-
- **
|
|
14
|
-
|
|
11
|
+
- **True Type Safety**: no any in the inference engine
|
|
12
|
+
|
|
13
|
+
- **Runtime Validation**: Strict validation for primitives and nested object structures
|
|
14
|
+
|
|
15
|
+
- **ESM Native**: optimized for Vite, Webpack 5, and Next.js
|
|
15
16
|
|
|
16
17
|
### Complex Types & Modifiers
|
|
17
18
|
|
|
@@ -30,32 +31,66 @@ npm install inu-light
|
|
|
30
31
|
|
|
31
32
|
## Usage
|
|
32
33
|
|
|
33
|
-
### Basic Example
|
|
34
|
+
### 1. Basic Example
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
The library uses the .parse() method to validate data. Instead of throwing unpredictable exceptions, it returns a result object containing either the validated data or a clear error message.
|
|
36
37
|
|
|
37
38
|
```typescript
|
|
38
39
|
import { inu } from 'inu-light';
|
|
39
40
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
const UserSchema = inu.object({
|
|
42
|
+
username: inu.string(),
|
|
43
|
+
priority: inu.number(),
|
|
44
|
+
isAdmin: inu.boolean(),
|
|
44
45
|
});
|
|
45
46
|
|
|
46
|
-
const result =
|
|
47
|
-
name: 'John Doe',
|
|
48
|
-
age: 30,
|
|
49
|
-
isActive: true,
|
|
50
|
-
});
|
|
47
|
+
const result = UserSchema.parse(apiResponse);
|
|
51
48
|
|
|
52
49
|
if (result.success) {
|
|
53
|
-
|
|
50
|
+
// TypeScript automatically knows 'result.value' type
|
|
51
|
+
console.log(result.value.username);
|
|
54
52
|
} else {
|
|
55
|
-
console.error(result.error);
|
|
53
|
+
console.error('Validation failed:', result.error);
|
|
56
54
|
}
|
|
57
55
|
```
|
|
58
56
|
|
|
57
|
+
### 2. Complex Types & Modifiers
|
|
58
|
+
|
|
59
|
+
inu-light handles real-world data structures with ease:
|
|
60
|
+
|
|
61
|
+
- **Optional & Nullable**: Explicitly allow undefined or null values.
|
|
62
|
+
|
|
63
|
+
- **Unions**: Validate input against multiple possible schemas.
|
|
64
|
+
|
|
65
|
+
- **Literals**: Enforce exact values (perfect for status codes or specific flags).
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
const ProjectSchema = inu.object({
|
|
69
|
+
id: inu.string(),
|
|
70
|
+
description: inu.string().optional(), // Accepts string | undefined
|
|
71
|
+
status: inu.union([inu.literal('open'), inu.literal('closed')]),
|
|
72
|
+
tags: inu.array(inu.string()).nullable(), // Accepts string[] | null
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 3. Type Inference (The "No Any" Rule)
|
|
77
|
+
|
|
78
|
+
Avoid redundancy. You don't need to manually write interfaces; the schema generates them for you.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { inu, ShapeToType } from 'inu-light';
|
|
82
|
+
|
|
83
|
+
const ProductSchema = inu.object({
|
|
84
|
+
sku: inu.string(),
|
|
85
|
+
price: inu.number(),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Automatically extract the TypeScript type
|
|
89
|
+
type Product = ShapeToType<typeof ProductSchema>;
|
|
90
|
+
|
|
91
|
+
// Resulting type: { sku: string; price: number; }
|
|
92
|
+
```
|
|
93
|
+
|
|
59
94
|
### Type Inference
|
|
60
95
|
|
|
61
96
|
You can extract the TypeScript type directly from a schema using `ShapeToType`.
|
|
@@ -74,30 +109,46 @@ type User = ShapeToType<typeof schema>;
|
|
|
74
109
|
|
|
75
110
|
### Primitives
|
|
76
111
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
112
|
+
| Method | Description |
|
|
113
|
+
| :-------------- | :-------------------- |
|
|
114
|
+
| `inu.string()` | Validates a `string` |
|
|
115
|
+
| `inu.number()` | Validates a `number` |
|
|
116
|
+
| `inu.boolean()` | Validates a `boolean` |
|
|
80
117
|
|
|
81
|
-
###
|
|
118
|
+
### Structural
|
|
82
119
|
|
|
83
|
-
|
|
120
|
+
| Method | Description |
|
|
121
|
+
| :-------------------- | :---------------------------------------------------------- |
|
|
122
|
+
| `inu.object(shape)` | Validates an object based on the provided shape. |
|
|
123
|
+
| `inu.array(schema)` | Validates an array where every item matches the `schema`. |
|
|
124
|
+
| `inu.tuple([s1, s2])` | Validates a fixed-length array with positional types. |
|
|
125
|
+
| `inu.union([s1, s2])` | Validates if the input matches at least one of the schemas. |
|
|
126
|
+
| `inu.literal(value)` | Validates strict equality against the provided `value`. |
|
|
84
127
|
|
|
85
|
-
|
|
128
|
+
### Modifiers (Available on all schemas)
|
|
86
129
|
|
|
87
|
-
|
|
130
|
+
- `.optional()`: Transforms the type to `T | undefined`.
|
|
131
|
+
- `.nullable()`: Transforms the type to `T | null`.
|
|
88
132
|
|
|
89
|
-
|
|
133
|
+
## Development
|
|
90
134
|
|
|
91
|
-
```
|
|
135
|
+
```bash
|
|
136
|
+
# Install dependencies
|
|
137
|
+
npm install
|
|
138
|
+
|
|
139
|
+
# Build (Outputs to /dist)
|
|
92
140
|
npm run build
|
|
141
|
+
|
|
142
|
+
# Run local tests
|
|
143
|
+
npx tsx test-local.ts
|
|
93
144
|
```
|
|
94
145
|
|
|
95
|
-
###
|
|
146
|
+
### Build
|
|
96
147
|
|
|
97
|
-
To
|
|
148
|
+
To compile the library from source:
|
|
98
149
|
|
|
99
150
|
```
|
|
100
|
-
|
|
151
|
+
npm run build
|
|
101
152
|
```
|
|
102
153
|
|
|
103
154
|
## License
|