@quobix/vacuum 0.19.4 → 0.20.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.
Files changed (2) hide show
  1. package/README.md +116 -1
  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?
@@ -616,4 +626,109 @@ You can configure global vacuum flags using environmental variables in the form
616
626
 
617
627
  If a flag, has a `-` in it, replace with `_`
618
628
 
629
+
630
+ ## Auto-fixing rule violations
631
+
632
+ 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.
633
+
634
+ ### Set up
635
+
636
+ 1. Define a rule that has an `autoFixFunction`, e.g.:
637
+ ```yaml
638
+ rules:
639
+ use-compatible-extensions:
640
+ autoFixFunction: useExtensibleEnum
641
+ description: Prefer compatible extensions
642
+ id: use-compatible-extensions
643
+ given: "$.components.schemas[?@.enum]"
644
+ severity: warn
645
+ message: Use x-extensible-enum instead of enum for better compatibility
646
+ then:
647
+ field: enum
648
+ function: falsy
649
+ ```
650
+
651
+ This rule flags any usage of `enum` and recommends they are updated to `x-extensible-enum`.
652
+ A simple change which can be easily auto fixed!
653
+
654
+ 2. Create a function which performs the auto-fix.
655
+ ```go
656
+ func useExtensibleEnum(
657
+ node *yaml.Node,
658
+ document *yaml.Node,
659
+ context *model.RuleFunctionContext,
660
+ ) (*yaml.Node, error) {
661
+ if node.Kind != yaml.MappingNode {
662
+ return node, nil
663
+ }
664
+
665
+ for i := 0; i < len(node.Content); i += 2 {
666
+ if i+1 >= len(node.Content) {
667
+ break
668
+ }
669
+
670
+ keyNode := node.Content[i]
671
+
672
+ if keyNode.Value == "enum" {
673
+ keyNode.Value = "x-extensible-enum"
674
+
675
+ return node, nil
676
+ }
677
+ }
678
+
679
+ return node, nil
680
+ }
681
+ ```
682
+
683
+ > [!NOTE]
684
+ > The auto fix function must satisfy the `AutoFixFunction` type.
685
+ > It should take in the `*yaml.Node` of the violation, the root `*yaml.Node` of the document and the `RuleFunctionContext`.
686
+ > It should return the fixed `*yaml.Node` and an error.
687
+
688
+ 3. Configure your `RuleSetExecution` to use the auto fix function.
689
+ ```go
690
+ func Lint(rulesFile string, specFile string) error {
691
+ rules, err := rulesets.LoadLocalRuleSet(ctx, rulesFile)
692
+ if err != nil {
693
+ return fmt.Errorf("error loading ruleset: %w", err)
694
+ }
695
+
696
+ rs := rulesets.BuildDefaultRuleSetsWithLogger(slog.Logger).
697
+ GenerateRuleSetFromSuppliedRuleSet(rules)
698
+
699
+ // NOTE: only showing the fields on the RuleSetExecution relevant to auto-fixing.
700
+ results := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{
701
+ AutoFixFunctions: map[string]model.AutoFixFunction{
702
+ "useExtensibleEnum": useExtensibleEnum,
703
+ },
704
+ ApplyAutoFixes: true,
705
+ RuleSet: rs,
706
+ })
707
+
708
+ // Write back to file if fixes were applied
709
+ if len(lintResults.FixedResults) > 0 && autoFix {
710
+ fileInfo, _ := os.Stat(specFile)
711
+
712
+ err = os.WriteFile(specFile, result.ModifiedSpec, fileInfo.Mode())
713
+ if err != nil {
714
+ return fmt.Errorf("failed to write file %s: %w", c.file, err)
715
+ }
716
+ }
717
+
718
+ return nil
719
+ }
720
+ ```
721
+
722
+ 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.
723
+
724
+ 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.
725
+
726
+ > [!TIP]
727
+ > When using `vacuum` as a library You can access the fixed yaml content in the `RuleSetExecutionResult.ModifiedSpec`, and choose where to write the file.
728
+ >
729
+ > 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.
730
+
731
+ ### Usage
732
+
733
+
619
734
  > 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.4",
3
+ "version": "0.20.0",
4
4
  "description": "The world's fastest, most scalable and complete OpenAPI parser",
5
5
  "type": "module",
6
6
  "author": "Dave Shanley",