@quobix/vacuum 0.19.5 → 0.20.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.
Files changed (2) hide show
  1. package/README.md +117 -113
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -144,7 +144,17 @@ come say hi!
144
144
 
145
145
  ## Documentation
146
146
 
147
- 🔥 **New in** `v0.19` 🔥: **Ignore rules with `x-lint-ignore`**
147
+ 🔥 **New in** `v0.20` 🔥: **Support for auto fixing custom rules**
148
+
149
+ Got some rules that don't really need a human to look at?
150
+
151
+ Well now you can define an `AutoFixFunction` for your rules, and when you run with the `--fix` flag, the fixes will will be applied to the file, or use `--fix-file` to write them to a different file.
152
+
153
+ See [Auto-Fixing Rule Violations](#auto-fixing-rule-violations) for more specifics.
154
+
155
+ ---
156
+
157
+ `v0.19`: **Ignore rules with `x-lint-ignore`**
148
158
 
149
159
  Got an error in your spec you know about but can't get round to fixing yet?
150
160
  Migrating from zally and wanting to keep your existing `x-zally-ignore` issues silenced?
@@ -171,8 +181,6 @@ New rules:
171
181
 
172
182
  ---
173
183
 
174
-
175
-
176
184
  `v0.17`: **Github Action**.
177
185
 
178
186
  vacuum now has an official Github Action. [Read the docs](https://quobix.com/vacuum/github-action/), or check it out
@@ -203,116 +211,7 @@ $refs being used in path items.
203
211
 
204
212
  ---
205
213
 
206
- `v0.14+`: **Engine Speedup**.
207
-
208
- **Speed!** We've made some significant improvements to how efficiently large documents are walked
209
- Which means vacuum is now faster than ever.
210
-
211
- ---
212
-
213
- `v0.12+` : Core functions support JSON Path.
214
-
215
- Now all **core** functions return the **correct and accurate JSON path for each linting result**. Previously this was not possible
216
- at all, but with some clever engineering, we have made it happen. It's a small thing, but with huge impact.
217
-
218
- This feature has been available on the OpenAPI functions for some time, however core functions were without a comparison.
219
- But no more! core functions have joined the party.
220
-
221
- A new `--no-clip` flag is available on the `lint` command. This prevents message/path truncation.
222
-
223
- ---
224
-
225
- `v0.11+`: Ignore Linting Errors/Violations.
226
-
227
- v0.11 introduces the ability to ignore specific linting errors. This is useful for when you want to implement new
228
- rules to existing production APIs. In some cases, correcting the lint errors would result in a breaking change.
229
-
230
- Having a way to ignore these errors allows you to implement the new rules for new APIs while maintaining
231
- backwards compatibility for existing ones.
232
-
233
- [Learn more about ignoring violations](https://quobix.com/vacuum/ignoring/)
234
-
235
- ---
236
-
237
- `v0.10+` : **Quality release**.
238
-
239
- v0.10 is a quality release, with a number of fixes and improvements to rule schemas, function names and more.
240
- vacuum now powers [The OpenAPI doctor](https://pb33f.io/doctor/). To enable correct ruleset management and automation
241
- a number of functions have been renamed, interfaces have been upgraded and rule functions schemas are now accurate.
242
-
243
- This is a breaking change for anyone using vacuum as a library with custom rules.
244
-
245
- ---
246
-
247
- `v0.9+` : **Built in Language Server**.
248
-
249
- A new command is available `language-server`. This starts vacuum as an LSP compatible language server. Run vacuum
250
- in your favorite IDE and get linting and validation as you type, in realtime.
251
-
252
- Will support any LSP compatible editor, like VSCode, Sublime, vim, etc.
253
-
254
- [Install the VSCode extension](https://marketplace.visualstudio.com/items?itemName=pb33f.vacuum)
255
- [Learn more about the language-server command](https://quobix.com/vacuum/commands/language-server/)
256
-
257
- ---
258
-
259
- `v0.8+` : **OpenAPI Bundler**.
260
-
261
- A new command is available `bundle` will bundle all external references for an OpenAPI file into a single file.
262
-
263
- [Learn more about the bundle command](https://quobix.com/vacuum/commands/bundle/)
264
-
265
- A new linting rule is available `oas-schema-check` will perform type checks and validation on all schemas in your
266
- OpenAPI file. It's enabled by default in the recommended ruleset.
267
-
268
- [oas-schema-check rule docs](https://quobix.com/vacuum/rules/schemas/oas-schema-check/)
269
-
270
- ---
271
-
272
- `v0.7+` : **Hard Mode**.
273
-
274
- Want to lint your spec with the most strict ruleset possible? Now you can! Use the `-z` / `--hard-mode` flag to enable
275
-
276
- ---
277
-
278
- `v0.6+` : **Sharable / distributed rulesets** now available.
279
-
280
- Want to share / extend / distribute your own rulesets? Now you can!
281
214
 
282
- [Learn more about sharable rulesets](https://quobix.com/vacuum/rulesets/sharing/)
283
-
284
- ---
285
-
286
- `v0.5+` : **Multi-file linting** now available for the `lint` command.
287
-
288
- Want to lint multiple files at once? Now you can!
289
-
290
- ```shell
291
- vacuum lint file1.json path/to/file2.yaml file3.json
292
- ```
293
-
294
- Want to suck in a ton of files? Use a **glob** pattern!
295
-
296
- ```shell
297
- vacuum lint some/path/**/*.yaml
298
- ```
299
-
300
-
301
- ---
302
- `v0.3+`: [Custom JavaScript Functions](https://quobix.com/vacuum/api/custom-javascript-functions/) are now available out of the box.
303
-
304
- Write custom functions in JavaScript and use them in any ruleset. No need
305
- to compile golang code to extend vacuum anymore!
306
-
307
- [Learn more about building custom JavaScript functions](https://quobix.com/vacuum/api/custom-javascript-functions/).
308
-
309
-
310
- ---
311
- `v0.2+`: [OWASP API rules](https://quobix.com/vacuum/rules/owasp/) are now available out of the box.
312
-
313
- [Learn more about enabling OWASP API rules](https://quobix.com/vacuum/rulesets/owasp/).
314
-
315
- ---
316
215
 
317
216
  ### [Quick Start Guide 🚀](https://quobix.com/vacuum/start)
318
217
 
@@ -365,7 +264,7 @@ See all the documentation at https://quobix.com/vacuum
365
264
  Designed to reliably lint OpenAPI specifications, **very, very quickly**. Including _very large_ ones. Spectral can be quite slow
366
265
  when used as an API and does not scale for enterprise applications.
367
266
 
368
- vacuum will tell you what is wrong with your spec, why, where and how to fix it.
267
+ vacuum will tell you what is wrong with your spec, why, where, and how to fix it.
369
268
 
370
269
  vacuum will work at scale and is designed as a CLI (with a web or console UI) and a library to be consumed in other applications.
371
270
 
@@ -616,4 +515,109 @@ You can configure global vacuum flags using environmental variables in the form
616
515
 
617
516
  If a flag, has a `-` in it, replace with `_`
618
517
 
518
+
519
+ ## Auto-fixing rule violations
520
+
521
+ If you have a rule that doesn't need a human to look at it, and the change can be reliably automated you can configure an `AutoFixFunction` on the rule. When you then run the `lint` command you can pass the `--fix` flag and the violation will be automatically fixed.
522
+
523
+ ### Set up
524
+
525
+ 1. Define a rule that has an `autoFixFunction`, e.g.:
526
+ ```yaml
527
+ rules:
528
+ use-compatible-extensions:
529
+ autoFixFunction: useExtensibleEnum
530
+ description: Prefer compatible extensions
531
+ id: use-compatible-extensions
532
+ given: "$.components.schemas[?@.enum]"
533
+ severity: warn
534
+ message: Use x-extensible-enum instead of enum for better compatibility
535
+ then:
536
+ field: enum
537
+ function: falsy
538
+ ```
539
+
540
+ This rule flags any usage of `enum` and recommends they are updated to `x-extensible-enum`.
541
+ A simple change which can be easily auto fixed!
542
+
543
+ 2. Create a function which performs the auto-fix.
544
+ ```go
545
+ func useExtensibleEnum(
546
+ node *yaml.Node,
547
+ document *yaml.Node,
548
+ context *model.RuleFunctionContext,
549
+ ) (*yaml.Node, error) {
550
+ if node.Kind != yaml.MappingNode {
551
+ return node, nil
552
+ }
553
+
554
+ for i := 0; i < len(node.Content); i += 2 {
555
+ if i+1 >= len(node.Content) {
556
+ break
557
+ }
558
+
559
+ keyNode := node.Content[i]
560
+
561
+ if keyNode.Value == "enum" {
562
+ keyNode.Value = "x-extensible-enum"
563
+
564
+ return node, nil
565
+ }
566
+ }
567
+
568
+ return node, nil
569
+ }
570
+ ```
571
+
572
+ > [!NOTE]
573
+ > The auto fix function must satisfy the `AutoFixFunction` type.
574
+ > It should take in the `*yaml.Node` of the violation, the root `*yaml.Node` of the document and the `RuleFunctionContext`.
575
+ > It should return the fixed `*yaml.Node` and an error.
576
+
577
+ 3. Configure your `RuleSetExecution` to use the auto fix function.
578
+ ```go
579
+ func Lint(rulesFile string, specFile string) error {
580
+ rules, err := rulesets.LoadLocalRuleSet(ctx, rulesFile)
581
+ if err != nil {
582
+ return fmt.Errorf("error loading ruleset: %w", err)
583
+ }
584
+
585
+ rs := rulesets.BuildDefaultRuleSetsWithLogger(slog.Logger).
586
+ GenerateRuleSetFromSuppliedRuleSet(rules)
587
+
588
+ // NOTE: only showing the fields on the RuleSetExecution relevant to auto-fixing.
589
+ results := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{
590
+ AutoFixFunctions: map[string]model.AutoFixFunction{
591
+ "useExtensibleEnum": useExtensibleEnum,
592
+ },
593
+ ApplyAutoFixes: true,
594
+ RuleSet: rs,
595
+ })
596
+
597
+ // Write back to file if fixes were applied
598
+ if len(lintResults.FixedResults) > 0 && autoFix {
599
+ fileInfo, _ := os.Stat(specFile)
600
+
601
+ err = os.WriteFile(specFile, result.ModifiedSpec, fileInfo.Mode())
602
+ if err != nil {
603
+ return fmt.Errorf("failed to write file %s: %w", c.file, err)
604
+ }
605
+ }
606
+
607
+ return nil
608
+ }
609
+ ```
610
+
611
+ When the auto fix function runs, if it returns an error the fix will not be applied, the error will be logged, and the violation will be reported in the standard results.
612
+
613
+ If the auto fix function succeeds the yaml node flagged by the violation will be replaced with the transformed version returned by the auto fix function.
614
+
615
+ > [!TIP]
616
+ > When using `vacuum` as a library You can access the fixed yaml content in the `RuleSetExecutionResult.ModifiedSpec`, and choose where to write the file.
617
+ >
618
+ > When using `vacuum` as a cli, the `--fix` flag will overwrite the spec file in place, and `--fix-file` flag lets you specify an alternative file to write the content to, if you want to compare the outputs.
619
+
620
+ ### Usage
621
+
622
+
619
623
  > Logo gopher is modified, originally from [egonelbre](https://github.com/egonelbre/gophers)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quobix/vacuum",
3
- "version": "0.19.5",
3
+ "version": "0.20.1",
4
4
  "description": "The world's fastest, most scalable and complete OpenAPI parser",
5
5
  "type": "module",
6
6
  "author": "Dave Shanley",