@loydjs/runtime 0.0.0 → 1.0.0
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 +171 -0
- package/package.json +4 -3
package/README.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<h1>Loyd</h1>
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
**High-performance, tree-shakable schema validation for TypeScript.**
|
|
7
|
+
|
|
8
|
+
[](https://github.com/b3nito404/loyd/actions/workflows/ci.yml)
|
|
9
|
+
[](https://opensource.org/licenses/MIT)
|
|
10
|
+
[](https://bundlephobia.com/package/@loydjs/schema)
|
|
11
|
+
[](https://www.typescriptlang.org)
|
|
12
|
+
[](https://github.com/b3nito404/loyd/releases)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Key features
|
|
19
|
+
|
|
20
|
+
- **0.8 kb minimal bundle** : functional `pipe()` composition enables full tree-shaking; import only what you use
|
|
21
|
+
- **JIT compiler** : `compile(schema)` generates a pure JavaScript function via `new Function()`; subsequent calls are **2× faster** than `safeParse` on hot paths
|
|
22
|
+
- **Structured i18n** : validators emit `{ code, path, meta }`, never locale strings; swap locales at runtime without touching your schemas
|
|
23
|
+
- **Two-pass async pipeline** : sync rules execute first; async rules only if sync passes; multiple async rules run in parallel via `Promise.all`
|
|
24
|
+
- **Field dependency graph** : `buildDag(schema, deps)` enables incremental revalidation; change one field -> revalidate only it and its dependents
|
|
25
|
+
- **Native React integration** : `useForm`, `useField`, `useFieldArray` with zero external dependencies, backed by the DAG
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
# Core start here
|
|
33
|
+
npm install @loydjs/schema @loydjs/core @loydjs/types
|
|
34
|
+
|
|
35
|
+
# Optional packages
|
|
36
|
+
npm install @loydjs/async # two-pass async pipeline
|
|
37
|
+
npm install @loydjs/compiler # JIT compilation
|
|
38
|
+
npm install @loydjs/error-engine # structured i18n
|
|
39
|
+
npm install @loydjs/react # React hooks (requires @loydjs/graph)
|
|
40
|
+
npm install @loydjs/graph # field dependency DAG
|
|
41
|
+
npm install @loydjs/zod-compat # Zod migration utilities
|
|
42
|
+
npm install @loydjs/openapi # OpenAPI 3.1 / JSON Schema export
|
|
43
|
+
npm install @loydjs/vite # Vite / Rollup AOT plugin
|
|
44
|
+
```
|
|
45
|
+
> **Requires** Node.js ≥ 20, TypeScript ≥ 5.4, and `"strict": true` in your `tsconfig.json`.
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Quick start
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { object, pipe, string, number, email, minLength } from "@loydjs/schema";
|
|
53
|
+
import { parse, safeParse } from "@loydjs/core";
|
|
54
|
+
import type { Infer } from "@loydjs/types";
|
|
55
|
+
|
|
56
|
+
// 1. Define your schema
|
|
57
|
+
const UserSchema = object({
|
|
58
|
+
name: pipe(string(), minLength(2)),
|
|
59
|
+
email: pipe(string(), email()),
|
|
60
|
+
age: number().int().min(0),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// 2. Infer the TypeScript type - zero runtime cost
|
|
64
|
+
type User = Infer<typeof UserSchema>;
|
|
65
|
+
// -> { name: string; email: string; age: number }
|
|
66
|
+
|
|
67
|
+
// 3a. parse() throws LoydError on failure
|
|
68
|
+
const user = parse(UserSchema, req.body);
|
|
69
|
+
|
|
70
|
+
// 3b. safeParse() never throws
|
|
71
|
+
const result = safeParse(UserSchema, formData);
|
|
72
|
+
if (result.success) {
|
|
73
|
+
console.log(result.data.name); // typed as User
|
|
74
|
+
} else {
|
|
75
|
+
result.issues.forEach(issue => {
|
|
76
|
+
console.log(issue.code); // "ERR_STRING_TOO_SHORT"
|
|
77
|
+
console.log(issue.path); // ["name"]
|
|
78
|
+
console.log(issue.meta); // { min: 2, actual: 1 }
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### React forms
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import { useForm } from "@loydjs/react";
|
|
87
|
+
|
|
88
|
+
function LoginForm() {
|
|
89
|
+
const { register, handleSubmit, state } = useForm({
|
|
90
|
+
schema: LoginSchema,
|
|
91
|
+
defaultValues: { email: "", password: "" },
|
|
92
|
+
mode: "onChange",
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<form onSubmit={handleSubmit(onValid, onInvalid)}>
|
|
97
|
+
<input {...register("email")} type="email" />
|
|
98
|
+
<input {...register("password")} type="password" />
|
|
99
|
+
<button type="submit" disabled={state.isSubmitting}>Sign in</button>
|
|
100
|
+
</form>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### JIT compilation
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { compile } from "@loydjs/compiler";
|
|
109
|
+
|
|
110
|
+
const validate = compile(UserSchema);
|
|
111
|
+
|
|
112
|
+
// 2× faster on hot paths - compiled once, cached per schema instance
|
|
113
|
+
const result = validate(input); // LoydResult<User>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### i18n error formatting
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { configureFormatter, fr } from "@loydjs/error-engine";
|
|
120
|
+
|
|
121
|
+
// Call once at app startup
|
|
122
|
+
configureFormatter("fr", fr);
|
|
123
|
+
|
|
124
|
+
// Issues are now formatted in French
|
|
125
|
+
const result = safeParse(UserSchema, badInput);
|
|
126
|
+
// result.issues[0].message -> "Minimum 2 caractères (reçu : 1)"
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Migrate from Zod
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { fromZod, runCodemod } from "@loydjs/zod-compat";
|
|
133
|
+
|
|
134
|
+
// Convert a single schema
|
|
135
|
+
const LoydUser = fromZod(zodUserSchema);
|
|
136
|
+
|
|
137
|
+
// Or run the automated codemod across your entire codebase
|
|
138
|
+
await runCodemod("./src", { write: true, verbose: true });
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## Packages
|
|
144
|
+
|
|
145
|
+
| Package | Description | Size |
|
|
146
|
+
|---|---|---|
|
|
147
|
+
| `@loydjs/core` | `parse`, `safeParse`, `LoydError`, `BaseSchema` | 3.9 kb |
|
|
148
|
+
| `@loydjs/schema` | All primitives, composites, modifiers, refinements | tree-shakeable |
|
|
149
|
+
| `@loydjs/types` | `Infer<>`, `InferInput<>`, `InferOutput<>` | 0 kb runtime |
|
|
150
|
+
| `@loydjs/async` | `parseAsync`, two-pass pipeline, `AbortSignal` | ~2 kb |
|
|
151
|
+
| `@loydjs/compiler` | `compile()`, JIT codegen, cache management | ~4 kb |
|
|
152
|
+
| `@loydjs/error-engine` | `createFormatter`, en/fr/es/ar locales | ~3 kb |
|
|
153
|
+
| `@loydjs/graph` | `buildDag`, `validateIncremental`, dirty tracking | ~3 kb |
|
|
154
|
+
| `@loydjs/react` | `useForm`, `useField`, `useFieldArray`, `FormProvider` | ~8 kb |
|
|
155
|
+
| `@loydjs/zod-compat` | `fromZod`, `toZod`, `runCodemod` | ~5 kb |
|
|
156
|
+
| `@loydjs/openapi` | `toOpenApi`, `toJsonSchema`, `toOpenApiComponents` | ~4 kb |
|
|
157
|
+
| `@loydjs/vite` | `loydPlugin()` - Vite/Rollup AOT compilation | ~2 kb |
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Documentation
|
|
162
|
+
|
|
163
|
+
For the full API reference, guides, and examples, visit the official documentation:
|
|
164
|
+
|
|
165
|
+
**[https://loyddev-psi.vercel.app](https://loyddev-psi.vercel.app)**
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loydjs/runtime",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Loyd runtime — zero-copy execution engine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"loyd",
|
|
@@ -26,13 +26,14 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
|
+
"README.md",
|
|
29
30
|
"dist",
|
|
30
31
|
"src"
|
|
31
32
|
],
|
|
32
33
|
"sideEffects": false,
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@loydjs/core": "
|
|
35
|
-
"@loydjs/compiler": "
|
|
35
|
+
"@loydjs/core": "1.0.0",
|
|
36
|
+
"@loydjs/compiler": "1.0.0"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"typescript": "^5.7.2",
|