inu-light 1.0.0 → 1.0.2

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 CHANGED
@@ -1,17 +1,18 @@
1
1
  # inu-light
2
2
 
3
- A lightweight, zero-dependency schema validation library for TypeScript and JavaScript.
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 focuses on predictable schemas and strict typing, avoiding magic behavior and unnecessary runtime dependencies.
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
- ## Features
9
+ - **Zero Dependencies**: minimal footprint, no runtime bloat
10
10
 
11
- - **Zero Dependencies**: Minimum footprint for your project.
12
- - **Type Safe**: Automatically infers TypeScript types from your schemas without using explicit any.
13
- - **ESM Native**: Built for modern environments.
14
- - **Lightweight**: Focused on core validation primitives.
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
- Define a schema and validate data against it. The `parse` method returns a result object containing either the validated data or an error message.
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 userSchema = inu.object({
41
- name: inu.string(),
42
- age: inu.number(),
43
- isActive: inu.boolean(),
41
+ const UserSchema = inu.object({
42
+ username: inu.string(),
43
+ priority: inu.number(),
44
+ isAdmin: inu.boolean(),
44
45
  });
45
46
 
46
- const result = userSchema.parse({
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
- console.log(result.value); // Type-safe data
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
- - `inu.string()`: Validates that the input is a string.
78
- - `inu.number()`: Validates that the input is a number.
79
- - `inu.boolean()`: Validates that the input is a boolean.
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
- ### Complex Types
118
+ ### Structural
82
119
 
83
- - `inu.object(shape)`: Validates an object based on the provided shape.
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
- ## Development
128
+ ### Modifiers (Available on all schemas)
86
129
 
87
- ### Build
130
+ - `.optional()`: Transforms the type to `T | undefined`.
131
+ - `.nullable()`: Transforms the type to `T | null`.
88
132
 
89
- To compile the library from source:
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
- ### Testing
146
+ ### Build
96
147
 
97
- To run local tests during development:
148
+ To compile the library from source:
98
149
 
99
150
  ```
100
- npx tsx test-local.ts
151
+ npm run build
101
152
  ```
102
153
 
103
154
  ## License
@@ -1,7 +1,7 @@
1
1
  import { Schema, ParseResult } from '../types/schema.js';
2
2
  export declare class InuLiteral<T> extends Schema<T> {
3
- private readonly expectedValue;
3
+ readonly expectedValue: T;
4
4
  constructor(expectedValue: T);
5
5
  parse(value: unknown): ParseResult<T>;
6
6
  }
7
- export declare function literal<T>(value: T): Schema<T>;
7
+ export declare function literal<T extends string | number | boolean>(value: T): Schema<T>;
@@ -8,7 +8,7 @@ export class InuLiteral extends Schema {
8
8
  if (value === this.expectedValue) {
9
9
  return {
10
10
  success: true,
11
- value: value,
11
+ value: this.expectedValue,
12
12
  };
13
13
  }
14
14
  return {
@@ -1,9 +1,7 @@
1
1
  import { Schema, ParseResult } from '../types/schema.js';
2
2
  export declare class InuUnion<T> extends Schema<T> {
3
- private schemas;
4
- constructor(schemas: Schema<T>[]);
3
+ private readonly schemas;
4
+ constructor(schemas: ReadonlyArray<Schema<T>>);
5
5
  parse(value: unknown): ParseResult<T>;
6
6
  }
7
- export declare function union<T extends unknown[]>(schemas: [...{
8
- [K in keyof T]: Schema<T[K]>;
9
- }]): Schema<T[number]>;
7
+ export declare function union<U>(schemas: ReadonlyArray<Schema<U>>): Schema<U>;
@@ -15,8 +15,7 @@ export class InuUnion extends Schema {
15
15
  }
16
16
  return {
17
17
  success: false,
18
- error: `Union error: Data does not match any of the allowed types.
19
- Sub-errors: [${errors.join(' | ')}]`,
18
+ error: `Union error: Data does not match any allowed types.`,
20
19
  };
21
20
  }
22
21
  }
package/package.json CHANGED
@@ -1,24 +1,24 @@
1
- {
2
- "name": "inu-light",
3
- "version": "1.0.0",
4
- "description": "A ultra-lightweight TypeScript validation library with zero dependencies.",
5
- "main": "./dist/index.js",
6
- "types": "./dist/index.d.ts",
7
- "type": "module",
8
- "files": [
9
- "dist"
10
- ],
11
- "scripts": {
12
- "build": "tsc",
13
- "prepublishOnly": "npm run build"
14
- },
15
- "keywords": [
16
- "validation",
17
- "typescript",
18
- "schema",
19
- "lightweight",
20
- "zod-like"
21
- ],
22
- "author": "Seu Nome",
23
- "license": "MIT"
24
- }
1
+ {
2
+ "name": "inu-light",
3
+ "version": "1.0.2",
4
+ "description": "A ultra-lightweight TypeScript validation library with zero dependencies.",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "type": "module",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "keywords": [
16
+ "validation",
17
+ "typescript",
18
+ "schema",
19
+ "lightweight",
20
+ "zod-like"
21
+ ],
22
+ "author": "Seu Nome",
23
+ "license": "MIT"
24
+ }