@gravionlabs/helix-zod 22.1.28 → 22.1.30
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,8 +1,8 @@
|
|
|
1
1
|
# @gravionlabs/helix-zod
|
|
2
2
|
|
|
3
|
-
Zod v4 adapter for `@gravionlabs/helix-
|
|
3
|
+
Zod v4 adapter for Helix forms. The validators and their keys come from `@gravionlabs/helix-ui/validators`. Two independent features:
|
|
4
4
|
|
|
5
|
-
1. **Reactive-forms validator bridge** — `HelixZodValidators.fromZod()` converts a Zod field schema into an Angular `ValidatorFn` that emits `
|
|
5
|
+
1. **Reactive-forms validator bridge** — `HelixZodValidators.fromZod()` converts a Zod field schema into an Angular `ValidatorFn` that emits `ValidatorKey`-keyed `ValidationErrors` — compatible with `HelixFormField` of `@gravionlabs/helix-shell` and anything else that reads `control.errors`.
|
|
6
6
|
2. **Dynamic forms** — `HelixDynamicForm` generates a complete, validated form from a single annotated Zod object schema, built on Angular's experimental signal forms (`@angular/forms/signals`, requires Angular **≥ 21.2**).
|
|
7
7
|
|
|
8
8
|
---
|
|
@@ -12,11 +12,17 @@ Zod v4 adapter for `@gravionlabs/helix-core` forms. Two independent features:
|
|
|
12
12
|
| Package | Version |
|
|
13
13
|
|---|---|
|
|
14
14
|
| `zod` | `^4.0.0` |
|
|
15
|
-
| `@gravionlabs/helix-
|
|
15
|
+
| `@gravionlabs/helix-ui` | `>=0.1.0` (the `validators` entry point) |
|
|
16
16
|
| `@angular/core` / `@angular/common` / `@angular/forms` | `>=21` (dynamic forms: `>=21.2`) |
|
|
17
17
|
|
|
18
18
|
---
|
|
19
19
|
|
|
20
|
+
## Styles
|
|
21
|
+
|
|
22
|
+
The built-in widgets draw their fields with `@gravionlabs/helix-ui` (`input[hx-input]`, `hx-input-number`, `hx-select`, `input[hx-checkbox]`, `button[hx-button]`) and the field frame (label, hint, error) with `@gravionlabs/helix-shell`. They have no styles of their own for the controls: the app must load the helix-ui styles and tokens (`@gravionlabs/helix-ui/styles.css` and `tokens.css`, see the helix-ui README) or the fields render unstyled.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
20
26
|
## Dynamic Forms from Zod Schemas
|
|
21
27
|
|
|
22
28
|
> Built on `@angular/forms/signals`, which is **experimental** — minor Angular releases may introduce breaking API changes.
|
|
@@ -78,7 +84,7 @@ Keys: `label`, `placeholder`, `hint`, `widget`, `options`, `order`, `addLabel`/`
|
|
|
78
84
|
provideHelixDynamicForms({
|
|
79
85
|
widgets: [{ widget: 'rating', component: RatingWidget }], // or override built-ins
|
|
80
86
|
errorMessageResolver: (error, helixKey) =>
|
|
81
|
-
helixKey ===
|
|
87
|
+
helixKey === ValidatorKey.Required ? 'Pflichtfeld' : null, // null → default message
|
|
82
88
|
})
|
|
83
89
|
```
|
|
84
90
|
|
|
@@ -151,19 +157,19 @@ fromZod(schema: ZodSchema, options?: ZodHelixOptions): ValidatorFn
|
|
|
151
157
|
|
|
152
158
|
```ts
|
|
153
159
|
export interface ZodHelixOptions {
|
|
154
|
-
fallbackKey?:
|
|
160
|
+
fallbackKey?: ValidatorKey; // required when schema uses .refine() / .superRefine()
|
|
155
161
|
allowEmpty?: boolean; // default: true
|
|
156
162
|
}
|
|
157
163
|
```
|
|
158
164
|
|
|
159
165
|
---
|
|
160
166
|
|
|
161
|
-
## Zod v4 → `
|
|
167
|
+
## Zod v4 → `ValidatorKey` Mapping
|
|
162
168
|
|
|
163
169
|
> This library targets **Zod v4**. Zod v4 introduced breaking changes from v3:
|
|
164
170
|
> `invalid_string` → `invalid_format` (with a `format` property), `too_small`/`too_big` use `origin` instead of `type`, and `not_integer` was replaced by `invalid_type` with `expected: 'int'`.
|
|
165
171
|
|
|
166
|
-
| Zod v4 issue code | Condition | `
|
|
172
|
+
| Zod v4 issue code | Condition | `ValidatorKey` |
|
|
167
173
|
|---|---|---|
|
|
168
174
|
| `invalid_type` | value is `''`, `null`, or `undefined` | `Required` |
|
|
169
175
|
| `invalid_type` | `expected === 'int'` | `Integer` |
|
|
@@ -178,26 +184,26 @@ export interface ZodHelixOptions {
|
|
|
178
184
|
| `custom` | — | requires `fallbackKey` (see below) |
|
|
179
185
|
| anything else | — | requires `fallbackKey` (see below) |
|
|
180
186
|
|
|
181
|
-
All issues from a single `safeParse` are processed simultaneously, producing one error key per issue — equivalent to stacking multiple `
|
|
187
|
+
All issues from a single `safeParse` are processed simultaneously, producing one error key per issue — equivalent to stacking multiple `Validators` calls.
|
|
182
188
|
|
|
183
189
|
### Known gaps — no automatic mapping
|
|
184
190
|
|
|
185
191
|
| Scenario | Recommendation |
|
|
186
192
|
|---|---|
|
|
187
|
-
| `z.enum()` → `invalid_value` | Use `fallbackKey` or keep using `
|
|
188
|
-
| `z.array()` item-level errors | Use `fallbackKey` or `
|
|
193
|
+
| `z.enum()` → `invalid_value` | Use `fallbackKey` or keep using `Validators.oneOf` |
|
|
194
|
+
| `z.array()` item-level errors | Use `fallbackKey` or `Validators.allOf` |
|
|
189
195
|
| `invalid_type` for `boolean` | Helix has no `Boolean` key — use `fallbackKey` |
|
|
190
196
|
|
|
191
197
|
---
|
|
192
198
|
|
|
193
199
|
## `allowEmpty` Behaviour
|
|
194
200
|
|
|
195
|
-
`
|
|
201
|
+
`Validators` defaults to `allowEmpty = true`: validation passes silently on empty values. `fromZod` mirrors this:
|
|
196
202
|
|
|
197
203
|
| Scenario | Recommended pattern |
|
|
198
204
|
|---|---|
|
|
199
205
|
| Optional field with format check | Default `allowEmpty: true` — empty passes, invalid format shows error |
|
|
200
|
-
| Mandatory field (required + format) | Stack `
|
|
206
|
+
| Mandatory field (required + format) | Stack `Validators.required('msg')` alongside `fromZod(schema)` |
|
|
201
207
|
| Required via Zod only | `allowEmpty: false` — empty triggers `Required` (null/undefined) or `MinLength` (empty string) |
|
|
202
208
|
|
|
203
209
|
```ts
|
|
@@ -209,18 +215,18 @@ HelixZodValidators.fromZod(z.string().min(1, 'Name is required'), { allowEmpty:
|
|
|
209
215
|
|
|
210
216
|
// Stacked — explicit required message + Zod format check
|
|
211
217
|
[
|
|
212
|
-
|
|
218
|
+
Validators.required('Email is required'),
|
|
213
219
|
HelixZodValidators.fromZod(z.string().email('Invalid email')),
|
|
214
220
|
]
|
|
215
221
|
```
|
|
216
222
|
|
|
217
|
-
**Note:** when `allowEmpty: false` and the value is `null` or `undefined`, Zod emits `invalid_type` with its default mismatch message, not the message from `.min()` or `.email()`. Stack `
|
|
223
|
+
**Note:** when `allowEmpty: false` and the value is `null` or `undefined`, Zod emits `invalid_type` with its default mismatch message, not the message from `.min()` or `.email()`. Stack `Validators.required('...')` for a custom required message.
|
|
218
224
|
|
|
219
225
|
---
|
|
220
226
|
|
|
221
227
|
## `.refine()` and `fallbackKey`
|
|
222
228
|
|
|
223
|
-
`.refine()` and `.superRefine()` produce `ZodIssueCode.custom`, which has no automatic `
|
|
229
|
+
`.refine()` and `.superRefine()` produce `ZodIssueCode.custom`, which has no automatic `ValidatorKey` mapping. Provide a `fallbackKey` to capture these errors:
|
|
224
230
|
|
|
225
231
|
```ts
|
|
226
232
|
const bannedUsernames = ['admin', 'root', 'system'];
|
|
@@ -229,7 +235,7 @@ HelixZodValidators.fromZod(
|
|
|
229
235
|
z.string()
|
|
230
236
|
.min(3, 'At least 3 characters')
|
|
231
237
|
.refine((v) => !bannedUsernames.includes(v), 'Username is not allowed'),
|
|
232
|
-
{ fallbackKey:
|
|
238
|
+
{ fallbackKey: ValidatorKey.Pattern },
|
|
233
239
|
)
|
|
234
240
|
```
|
|
235
241
|
|
|
@@ -246,7 +252,8 @@ HelixZodValidators.fromZod(
|
|
|
246
252
|
import { Component, inject, ChangeDetectionStrategy } from '@angular/core';
|
|
247
253
|
import { ReactiveFormsModule, FormBuilder } from '@angular/forms';
|
|
248
254
|
import { z } from 'zod';
|
|
249
|
-
import { HelixFormField
|
|
255
|
+
import { HelixFormField } from '@gravionlabs/helix-shell';
|
|
256
|
+
import { Validators, ValidatorKey } from '@gravionlabs/helix-ui/validators';
|
|
250
257
|
import { HelixZodValidators } from '@gravionlabs/helix-zod';
|
|
251
258
|
|
|
252
259
|
// Define your schema once — reuse it for both API parsing and form validation
|
|
@@ -288,7 +295,7 @@ export class RegisterComponent {
|
|
|
288
295
|
email: [
|
|
289
296
|
'',
|
|
290
297
|
[
|
|
291
|
-
|
|
298
|
+
Validators.required('Email is required'),
|
|
292
299
|
HelixZodValidators.fromZod(UserSchema.shape.email),
|
|
293
300
|
],
|
|
294
301
|
],
|
|
@@ -306,7 +313,7 @@ export class RegisterComponent {
|
|
|
306
313
|
z.string()
|
|
307
314
|
.min(3, 'At least 3 characters')
|
|
308
315
|
.refine((v) => !bannedUsernames.includes(v), 'Username is not allowed'),
|
|
309
|
-
{ fallbackKey:
|
|
316
|
+
{ fallbackKey: ValidatorKey.Pattern },
|
|
310
317
|
),
|
|
311
318
|
],
|
|
312
319
|
});
|
|
@@ -327,12 +334,8 @@ export class RegisterComponent {
|
|
|
327
334
|
|
|
328
335
|
`HelixFormField` reads `control.errors` and takes the first string value. Since `fromZod` stores the Zod error message as the value (e.g. `{ Email: 'Invalid email' }`), `activeError` picks it up with no adapter layer.
|
|
329
336
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
import { helixFormErrorMap } from '@gravionlabs/helix-core';
|
|
333
|
-
const errors = helixFormErrorMap(this.form);
|
|
334
|
-
// → { email: 'Invalid email', name: 'Name is required' }
|
|
335
|
-
```
|
|
337
|
+
The same errors are on `form.controls.email.errors`, so any code that reads `control.errors` (for example a summary of all field errors) works the same way.
|
|
338
|
+
|
|
336
339
|
|
|
337
340
|
---
|
|
338
341
|
|
|
@@ -343,7 +346,7 @@ Using `UserSchema.shape.<field>` directly in `fromZod` eliminates duplicated val
|
|
|
343
346
|
```ts
|
|
344
347
|
// Without shape — rules written twice
|
|
345
348
|
z.string().email() // API parsing
|
|
346
|
-
|
|
349
|
+
Validators.email('...') // form (same rule, again)
|
|
347
350
|
|
|
348
351
|
// With shape — one definition drives both
|
|
349
352
|
HelixZodValidators.fromZod(UserSchema.shape.email)
|
|
@@ -375,7 +378,7 @@ This library is the `@gravionlabs/helix-zod` portion of the broader Zod integrat
|
|
|
375
378
|
- REST endpoint validation with `HttpClient` and `httpResource`
|
|
376
379
|
- Luxon `IsoDateTime` transform schema
|
|
377
380
|
- Environment config validation at startup
|
|
378
|
-
- Generic `zodFieldValidator` (framework-agnostic, no `
|
|
381
|
+
- Generic `zodFieldValidator` (framework-agnostic, no `ValidatorKey` dependency)
|
|
379
382
|
|
|
380
383
|
Those patterns live in your application (`src/app/schemas/`, `src/app/api/`, etc.) — this library provides only the Angular `ValidatorFn` bridge.
|
|
381
384
|
|
|
@@ -387,8 +390,8 @@ The library fully implements the `helix-zod` scope defined in `ZOD_ARCHITECTURE_
|
|
|
387
390
|
|---|---|
|
|
388
391
|
| `ZodHelixOptions` interface (`fallbackKey`, `allowEmpty`) | Implemented |
|
|
389
392
|
| `HelixZodValidators.fromZod()` factory | Implemented |
|
|
390
|
-
| Full Zod v4 issue → `
|
|
391
|
-
| `allowEmpty = true` default mirroring `
|
|
393
|
+
| Full Zod v4 issue → `ValidatorKey` mapping | Implemented |
|
|
394
|
+
| `allowEmpty = true` default mirroring `Validators` | Implemented |
|
|
392
395
|
| `ngDevMode` throw on unmapped issue without `fallbackKey` | Implemented |
|
|
393
396
|
| Silent skip in production builds | Implemented |
|
|
394
397
|
| `z.array().min()` → `MinLength` | Implemented |
|