@chaos-maker/core 0.4.0 → 0.5.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 CHANGED
@@ -49,14 +49,82 @@ chaos.stop(); // restores original fetch/XHR
49
49
 
50
50
  ### Presets
51
51
 
52
+ Presets are reusable bundles of rules. Drop them into a config by name with the `presets` field, and the engine merges them at construction time.
53
+
52
54
  ```ts
53
- import { ChaosMaker, presets } from '@chaos-maker/core';
55
+ import { ChaosMaker } from '@chaos-maker/core';
54
56
 
55
- // Available: unstableApi, slowNetwork, offlineMode, flakyConnection, degradedUi
56
- const chaos = new ChaosMaker(presets.slowNetwork);
57
+ const chaos = new ChaosMaker({
58
+ presets: ['slow-api'],
59
+ network: {
60
+ failures: [{ urlPattern: '/api/checkout', statusCode: 500, probability: 1 }],
61
+ },
62
+ });
57
63
  chaos.start();
58
64
  ```
59
65
 
66
+ **Built-in catalog**
67
+
68
+ | camelCase name | Kebab alias | Behavior |
69
+ | ----------------------- | --------------- | ----------------------------------------------------------------- |
70
+ | `slowNetwork` | `slow-api` | 2000ms latency on every request |
71
+ | `flakyConnection` | `flaky-api` | 5% aborts plus 3000ms latency on 10% of requests |
72
+ | `offlineMode` | `offline-mode` | Force CORS failure on every request |
73
+ | `unstableApi` | `high-latency` | 10% failures + 20% 1000ms latency, scoped to `/api/` |
74
+ | `degradedUi` | | 20% disable buttons, 10% hide links |
75
+ | `unreliableWebSocket` | | 10% drops, 500ms inbound delay, 5% inbound truncation |
76
+ | `unreliableEventStream` | | 5% drops, 200ms delay, 2% close after 2000ms |
77
+
78
+ Kebab-case aliases (`slow-api`, `flaky-api`, `offline-mode`, `high-latency`) are registry-only. They resolve via `presets: ['slow-api']` and `new PresetRegistry().get('slow-api')`. They are NOT keys on the legacy `presets` record export — `presets['slow-api']` is `undefined` by design. Use the camelCase key (`presets.slowNetwork`) when reading from the record.
79
+
80
+ **Custom presets**
81
+
82
+ Register your own bundle inline via `customPresets`. Names collide fail-fast against built-ins and against each other.
83
+
84
+ ```ts
85
+ new ChaosMaker({
86
+ customPresets: {
87
+ 'team-flow': {
88
+ network: {
89
+ failures: [{ urlPattern: '/checkout', statusCode: 503, probability: 1 }],
90
+ },
91
+ },
92
+ },
93
+ presets: ['team-flow'],
94
+ });
95
+ ```
96
+
97
+ Custom preset values may carry only rule arrays plus the optional `groups` field — `presets`, `customPresets`, `seed`, and `debug` are rejected at validation. Dependency chains are out of scope.
98
+
99
+ **Builder helper**
100
+
101
+ ```ts
102
+ import { ChaosConfigBuilder } from '@chaos-maker/core';
103
+
104
+ const config = new ChaosConfigBuilder()
105
+ .usePreset('slow-api')
106
+ .failRequests('/api/checkout', 500, 1)
107
+ .build();
108
+ ```
109
+
110
+ **Validation**
111
+
112
+ Unknown preset names, chain attempts, forbidden subfields, duplicate registrations, and group-name collisions across preset+user all surface as `ChaosConfigError` at construction time, never at runtime.
113
+
114
+ **Mutability**
115
+
116
+ Built-in preset configs are deep-frozen — `presets.slowNetwork.network!.latencies![0].delayMs = 1` throws. Your own custom presets passed via `customPresets` are NOT frozen — keep treating them as your data. The engine takes a deep clone at expansion, so any tweaks you make after construction are not observed.
117
+
118
+ **Legacy spread**
119
+
120
+ ```ts
121
+ import { presets } from '@chaos-maker/core';
122
+
123
+ new ChaosMaker({ ...presets.slowNetwork, network: { failures: [{ urlPattern: '/api', statusCode: 500, probability: 1 }] } });
124
+ ```
125
+
126
+ Still supported for migration. Prefer the declarative `presets:` field for new code.
127
+
60
128
  ### Config Builder
61
129
 
62
130
  ```ts
@@ -191,22 +259,36 @@ Event types: `network:failure`, `network:latency`, `network:abort`, `network:cor
191
259
 
192
260
  ## Config Validation
193
261
 
194
- All configs are validated with Zod in strict mode. Unknown keys are rejected. Invalid values throw `ChaosConfigError` with descriptive messages.
262
+ All configs are validated with Zod in strict mode. Unknown keys are rejected by default. Invalid values throw `ChaosConfigError` whose `issues` is a `ValidationIssue[]` with structured `path` / `code` / `ruleType` / `message` / `expected` / `received`.
195
263
 
196
264
  ```ts
197
- import { validateConfig, ChaosConfigError } from '@chaos-maker/core';
265
+ import { validateChaosConfig, ChaosConfigError } from '@chaos-maker/core';
198
266
 
199
267
  try {
200
- validateConfig({ network: { failures: [{ urlPattern: '', statusCode: 999 }] } });
268
+ validateChaosConfig({
269
+ network: { failures: [{ urlPattern: '', statusCode: 999, probability: 2 }] },
270
+ });
201
271
  } catch (e) {
202
272
  if (e instanceof ChaosConfigError) {
203
- console.log(e.issues);
204
- // ['network.failures.0.urlPattern: urlPattern must not be empty',
205
- // 'network.failures.0.statusCode: Number must be less than or equal to 599']
273
+ for (const issue of e.issues) {
274
+ console.log(issue.path, issue.code, issue.message);
275
+ }
276
+ // legacy v0.4.x string array still available:
277
+ console.log(e.messages);
206
278
  }
207
279
  }
208
280
  ```
209
281
 
282
+ `validateChaosConfig(input, opts?)` accepts:
283
+
284
+ - `unknownFields: 'reject' | 'warn' | 'ignore'` — strict by default. `'warn'` and `'ignore'` strip unknowns from the returned config; `'warn'` emits exactly one aggregated `console.warn` per call.
285
+ - `customValidators: Partial<Record<RuleType, (rule, ctx) => ValidationIssue[] | void>>` — run extra checks per rule type.
286
+ - `onDeprecation: (issue) => void` — receive `ValidationIssue` events for deprecated fields (rails only in v0.5.0).
287
+
288
+ A JSON Schema artifact ships at `node_modules/@chaos-maker/core/dist/chaos-config.schema.json` for IDE / `"$schema"` autocomplete plus a sidecar `chaos-config.schema.notes.md` listing parity caveats. The artifact is a tooling approximation — runtime canonical validation is always Zod via `validateChaosConfig`.
289
+
290
+ See the [Rule Validation concept page](https://chaos-maker-dev.github.io/chaos-maker/concepts/validation/) for the full pipeline, brand semantics, and migration notes.
291
+
210
292
  ## Service Worker chaos
211
293
 
212
294
  Chaos applies to SW-originated fetches via the `@chaos-maker/core/sw` subpath. Zod + UI + builder are excluded from this bundle so it stays small enough for production SW deploys.