-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
ruleset_functions.go
1206 lines (1140 loc) · 38.1 KB
/
ruleset_functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020-2022 Dave Shanley / Quobix
// SPDX-License-Identifier: MIT
package rulesets
import (
"github.com/daveshanley/vacuum/model"
"github.com/daveshanley/vacuum/parser"
"regexp"
)
// GetContactPropertiesRule will return a rule configured to look at contact properties of a spec.
// it uses the in-built 'truthy' function
func GetContactPropertiesRule() *model.Rule {
return &model.Rule{
Name: "Check contact properties: name, URL, email",
Id: ContactProperties,
Formats: model.AllFormats,
Description: "Contact details are incomplete",
Given: "$.info.contact",
Resolved: true,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Recommended: false,
Type: Validation,
Severity: model.SeverityInfo,
Then: []model.RuleAction{
{
Field: "name",
Function: "truthy",
},
{
Field: "url",
Function: "truthy",
},
{
Field: "email",
Function: "truthy",
},
},
HowToFix: contactPropertiesFix,
}
}
// GetInfoContactRule Will return a rule that uses the truthy function to check if the
// info object contains a contact object
func GetInfoContactRule() *model.Rule {
return &model.Rule{
Name: "Check for specification contact details",
Id: InfoContact,
Formats: model.AllFormats,
Description: "Info section is missing contact details",
Given: "$.info",
Resolved: true,
Recommended: false,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Field: "contact",
Function: "truthy",
},
HowToFix: contactFix,
}
}
// GetInfoDescriptionRule Will return a rule that uses the truthy function to check if the
// info object contains a description
func GetInfoDescriptionRule() *model.Rule {
return &model.Rule{
Name: "Check for a specification description",
Id: InfoDescription,
Formats: model.AllFormats,
Description: "Info section is missing a description",
Given: "$.info",
Resolved: true,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Recommended: true,
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Field: "description",
Function: "truthy",
},
HowToFix: infoDescriptionFix,
}
}
// GetInfoLicenseRule will return a rule that uses the truthy function to check if the
// info object contains a license
func GetInfoLicenseRule() *model.Rule {
return &model.Rule{
Name: "Check for a license definition",
Id: InfoLicense,
Formats: model.AllFormats,
Description: "Info section should contain a license",
Given: "$.info",
Resolved: true,
Recommended: false,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Type: Style,
Severity: model.SeverityInfo,
Then: model.RuleAction{
Field: "license",
Function: "truthy",
},
HowToFix: infoLicenseFix,
}
}
// GetInfoLicenseUrlRule will return a rule that uses the truthy function to check if the
// info object contains a license with a URL that is set.
func GetInfoLicenseUrlRule() *model.Rule {
return &model.Rule{
Name: "Check if license is missing a URL",
Id: LicenseUrl,
Formats: model.AllFormats,
Description: "License should contain a URL",
Given: "$.info.license",
Resolved: true,
Recommended: false,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Type: Style,
Severity: model.SeverityInfo,
Then: model.RuleAction{
Field: "url",
Function: "truthy",
},
HowToFix: infoLicenseUrlFix,
}
}
// GetNoEvalInMarkdownRule will return a rule that uses the pattern function to check if
// there is no eval statements markdown used in descriptions
func GetNoEvalInMarkdownRule() *model.Rule {
fo := make(map[string]string)
fo["pattern"] = "eval\\("
comp, _ := regexp.Compile(fo["pattern"])
return &model.Rule{
Name: "Check descriptions for 'eval()' statements",
Id: NoEvalInMarkdown,
Formats: model.AllFormats,
Description: "Markdown descriptions must not have `eval()` statements'",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryValidation],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "noEvalDescription",
FunctionOptions: fo,
},
PrecompiledPattern: comp,
HowToFix: noEvalInMarkDownFix,
}
}
// GetNoScriptTagsInMarkdownRule will return a rule that uses the pattern function to check if
// there is no script tags used in descriptions and the title.
func GetNoScriptTagsInMarkdownRule() *model.Rule {
fo := make(map[string]string)
fo["pattern"] = "<script"
comp, _ := regexp.Compile(fo["pattern"])
return &model.Rule{
Name: "Check descriptions for '<script>' tags",
Id: NoScriptTagsInMarkdown,
Formats: model.AllFormats,
Description: "Markdown descriptions must not have `<script>` tags'",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryValidation],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "noEvalDescription",
FunctionOptions: fo,
},
PrecompiledPattern: comp,
HowToFix: noScriptTagsInMarkdownFix,
}
}
// GetOpenApiTagsAlphabeticalRule will return a rule that uses the alphabetical function to check if
// tags are in alphabetical order
func GetOpenApiTagsAlphabeticalRule() *model.Rule {
fo := make(map[string]string)
fo["keyedBy"] = "name"
return &model.Rule{
Name: "Check tags are ordered alphabetically",
Id: OpenAPITagsAlphabetical,
Formats: model.AllFormats,
Description: "Tags must be in alphabetical order",
Given: "$.tags",
Resolved: true,
Recommended: false,
RuleCategory: model.RuleCategories[model.CategoryTags],
Type: Style,
Severity: model.SeverityInfo,
Then: model.RuleAction{
Function: "alphabetical",
FunctionOptions: fo,
},
HowToFix: openAPITagsAlphabeticalFix,
}
}
// GetOpenApiTagsRule uses the schema function to check if there tags exist and that
// it's an array with at least one item.
func GetOpenApiTagsRule() *model.Rule {
// create a schema to match against.
opts := make(map[string]interface{})
yml := `type: array
items:
type: object
minItems: 1
uniqueItems: true`
jsonSchema, _ := parser.ConvertYAMLIntoJSONSchema(yml, nil)
opts["schema"] = jsonSchema
opts["forceValidation"] = true // this will be picked up by the schema function to force validation.
//opts["unpack"] = true // unpack will correctly unpack this data so the schema method can use it.
return &model.Rule{
Name: "Check global tags are defined",
Id: OpenAPITags,
Formats: model.AllFormats,
Description: "Top level spec `tags` must not be empty, and must be an array",
Given: "$",
Resolved: true,
RuleCategory: model.RuleCategories[model.CategoryTags],
Recommended: false,
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Field: "tags",
Function: "schema",
FunctionOptions: opts,
},
HowToFix: openAPITagsFix,
}
}
// GetOAS2APISchemesRule uses the schema function to check if swagger has schemes and that
// it's an array with at least one item.
func GetOAS2APISchemesRule() *model.Rule {
// create a schema to match against.
opts := make(map[string]interface{})
yml := `type: array
items:
type: string
minItems: 1
uniqueItems: true`
jsonSchema, _ := parser.ConvertYAMLIntoJSONSchema(yml, nil)
opts["schema"] = jsonSchema
opts["forceValidation"] = true // this will be picked up by the schema function to force validation.
return &model.Rule{
Name: "Check host schemes are defined",
Id: Oas2APISchemes,
Formats: model.OAS2Format,
Description: "OpenAPI host `schemes` must be present and non-empty array",
Given: "$",
Resolved: false,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Recommended: true,
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Field: "schemes",
Function: "schema",
FunctionOptions: opts,
},
HowToFix: oas2APISchemesFix,
}
}
// GetOAS2HostNotExampleRule checks to make sure that example.com is not being used as a host.
// TODO: how common is this? should we keep it? change it?
func GetOAS2HostNotExampleRule() *model.Rule {
opts := make(map[string]interface{})
opts["notMatch"] = "example\\.com"
comp, _ := regexp.Compile(opts["notMatch"].(string))
return &model.Rule{
Name: "Check server URLs for example.com",
Id: Oas2HostNotExample,
Formats: model.OAS2Format,
Description: "Host URL should not point at example.com",
Given: "$.host",
Resolved: false,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Recommended: true,
Type: Style,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "pattern",
FunctionOptions: opts,
},
PrecompiledPattern: comp,
HowToFix: oas2HostNotExampleFix,
}
}
// GetOAS3HostNotExampleRule checks to make sure that example.com is not being used as a host.
// TODO: how common is this? should we keep it? change it?
func GetOAS3HostNotExampleRule() *model.Rule {
opts := make(map[string]interface{})
opts["notMatch"] = "example\\.com"
comp, _ := regexp.Compile(opts["notMatch"].(string))
return &model.Rule{
Name: "Check server URLs for example.com",
Id: Oas3HostNotExample,
Formats: model.OAS3AllFormat,
Description: "Server URL should not point at example.com",
Given: "$.servers[*].url",
Resolved: false,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Recommended: false,
Type: Style,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "pattern",
FunctionOptions: opts,
},
PrecompiledPattern: comp,
HowToFix: oas3HostNotExampleFix,
}
}
// GetOAS2HostTrailingSlashRule checks to make sure there is no trailing slash on the host
func GetOAS2HostTrailingSlashRule() *model.Rule {
opts := make(map[string]interface{})
opts["notMatch"] = "/$"
comp, _ := regexp.Compile(opts["notMatch"].(string))
return &model.Rule{
Name: "Check host for trailing slash",
Id: Oas2HostTrailingSlash,
Formats: model.OAS2Format,
Description: "Host URL should not contain a trailing slash",
Given: "$.host",
Resolved: false,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Recommended: true,
Type: Style,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "pattern",
FunctionOptions: opts,
},
PrecompiledPattern: comp,
HowToFix: oas2HostTrailingSlashFix,
}
}
// GetOAS3HostTrailingSlashRule checks to make sure there is no trailing slash on the host
func GetOAS3HostTrailingSlashRule() *model.Rule {
opts := make(map[string]interface{})
opts["notMatch"] = "/$"
comp, _ := regexp.Compile(opts["notMatch"].(string))
return &model.Rule{
Name: "Check server url for trailing slash",
Id: Oas3HostTrailingSlash,
Formats: model.OAS3Format,
Description: "server URL should not contain a trailing slash",
Given: "$.servers[*]",
Resolved: false,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Recommended: false,
Type: Style,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Field: "url",
Function: "pattern",
FunctionOptions: opts,
},
PrecompiledPattern: comp,
HowToFix: oas3HostTrailingSlashFix,
}
}
// GetOperationDescriptionRule will return a rule that uses the truthy function to check if an operation
// has defined a description or not, or does not meet the required length
func GetOperationDescriptionRule() *model.Rule {
opts := make(map[string]interface{})
opts["minWords"] = "1" // there must be at least a single word.
return &model.Rule{
Name: "Check operation description",
Id: OperationDescription,
Formats: model.AllFormats,
Description: "Operation description checks",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryDescriptions],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "oasDescriptions",
FunctionOptions: opts,
},
HowToFix: operationDescriptionFix,
}
}
// GetOAS2ParameterDescriptionRule will check specs to make sure parameters have a description.
func GetOAS2ParameterDescriptionRule() *model.Rule {
return &model.Rule{
Name: "Check parameter description",
Id: Oas2ParameterDescription,
Formats: model.OAS2Format,
Description: "Parameter description checks",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryDescriptions],
Type: Style,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "oasParamDescriptions",
},
HowToFix: oasParameterDescriptionFix,
}
}
// GetOAS3ParameterDescriptionRule will check specs to make sure parameters have a description.
func GetOAS3ParameterDescriptionRule() *model.Rule {
return &model.Rule{
Name: "Check parameter description",
Id: Oas3ParameterDescription,
Formats: model.OAS3AllFormat,
Description: "Parameter description checks",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryDescriptions],
Type: Style,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "oasParamDescriptions",
},
HowToFix: oasParameterDescriptionFix,
}
}
// GetDescriptionDuplicationRule will check if any descriptions have been copy/pasted or duplicated.
// all descriptions should be unique, otherwise what is the point?
func GetDescriptionDuplicationRule() *model.Rule {
return &model.Rule{
Name: "Check descriptions for duplicates",
Id: DescriptionDuplication,
Formats: model.AllFormats,
Description: "Description duplication check",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryDescriptions],
Type: Validation,
Severity: model.SeverityInfo,
Then: model.RuleAction{
Function: "oasDescriptionDuplication",
},
HowToFix: descriptionDuplicationFix,
}
}
// GetComponentDescriptionsRule will check all components for description problems.
func GetComponentDescriptionsRule() *model.Rule {
return &model.Rule{
Name: "Check component description",
Formats: model.OAS3AllFormat,
Id: ComponentDescription,
Description: "Component description check",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryDescriptions],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "oasComponentDescriptions",
},
HowToFix: componentDescriptionFix,
}
}
// GetAPIServersRule checks to make sure there is a valid 'servers' definition in the document.
func GetAPIServersRule() *model.Rule {
return &model.Rule{
Name: "Validate API server definitions",
Id: Oas3APIServers,
Formats: model.OAS3Format,
Description: "Check for valid API servers definition",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryValidation],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "oasAPIServers",
},
HowToFix: oasServersFix,
}
}
// GetOperationIdValidInUrlRule will check id an operationId will be valid when used in a URL.
func GetOperationIdValidInUrlRule() *model.Rule {
// TODO: re-build this the path is useless.
opts := make(map[string]interface{})
opts["match"] = "^[A-Za-z0-9-._~:/?#\\[\\]@!\\$&'()*+,;=]*$"
comp, _ := regexp.Compile(opts["match"].(string))
return &model.Rule{
Name: "Check operationId is URL friendly",
Id: OperationOperationIdValidInUrl,
Formats: model.AllFormats,
Description: "OperationId must use URL friendly characters",
Given: "$.paths[*][*]",
Resolved: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Recommended: true,
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Field: "operationId",
Function: "pattern",
FunctionOptions: opts,
},
PrecompiledPattern: comp,
HowToFix: operationIdValidInUrlFix,
}
}
// GetOperationTagsRule uses the schema function to check if there tags exist and that
// it's an array with at least one item.
func GetOperationTagsRule() *model.Rule {
return &model.Rule{
Name: "Check operation tags are used",
Id: OperationTags,
Formats: model.AllFormats,
Description: "Operation `tags` are missing/empty",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryTags],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "oasOperationTags",
},
HowToFix: operationTagsFix,
}
}
// GetPathDeclarationsMustExistRule will check to make sure there are no empty path variables
func GetPathDeclarationsMustExistRule() *model.Rule {
opts := make(map[string]interface{})
opts["notMatch"] = "{}"
comp, _ := regexp.Compile(opts["notMatch"].(string))
return &model.Rule{
Name: "Check path parameter declarations",
Id: PathDeclarationsMustExist,
Formats: model.AllFormats,
Description: "Path parameter declarations must not be empty ex. `/api/{}` is invalid",
Given: "$.paths",
Resolved: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Recommended: true,
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "pattern",
FunctionOptions: opts,
},
PrecompiledPattern: comp,
HowToFix: pathDeclarationsMustExistFix,
}
}
// GetPathNoTrailingSlashRule will make sure that paths don't have trailing slashes
func GetPathNoTrailingSlashRule() *model.Rule {
opts := make(map[string]interface{})
opts["notMatch"] = ".+\\/$"
comp, _ := regexp.Compile(opts["notMatch"].(string))
return &model.Rule{
Name: "Check path for any trailing slashes",
Id: PathKeysNoTrailingSlash,
Formats: model.AllFormats,
Description: "Path must not end with a slash",
Given: "$.paths",
Resolved: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Recommended: true,
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "pattern",
FunctionOptions: opts,
},
PrecompiledPattern: comp,
HowToFix: pathNoTrailingSlashFix,
}
}
// GetPathNotIncludeQueryRule checks to ensure paths are not including any query parameters.
func GetPathNotIncludeQueryRule() *model.Rule {
opts := make(map[string]interface{})
opts["notMatch"] = "\\?"
comp, _ := regexp.Compile(opts["notMatch"].(string))
return &model.Rule{
Name: "Check path excludes query string",
Id: PathNotIncludeQuery,
Formats: model.AllFormats,
Description: "Path must not include query string",
Given: "$.paths",
Resolved: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Recommended: true,
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "pattern",
FunctionOptions: opts,
},
PrecompiledPattern: comp,
HowToFix: pathNotIncludeQueryFix,
}
}
// GetTagDescriptionRequiredRule checks to ensure tags defined have been given a description
func GetTagDescriptionRequiredRule() *model.Rule {
return &model.Rule{
Name: "Check tag description",
Id: TagDescription,
Formats: model.AllFormats,
Description: "Tag must have a description defined",
Given: "$.tags",
Resolved: true,
Recommended: false,
RuleCategory: model.RuleCategories[model.CategoryTags],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Field: "description",
Function: "truthy",
},
HowToFix: tagDescriptionRequiredFix,
}
}
// GetTypedEnumRule checks to ensure enums are of the specified type
func GetTypedEnumRule() *model.Rule {
return &model.Rule{
Name: "Check enum types",
Id: TypedEnum,
Formats: model.AllFormats,
Description: "Enum values must respect the specified type",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategorySchemas],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "typedEnum",
},
HowToFix: typedEnumFix,
}
}
// GetPathParamsRule checks if path params are valid and defined.
func GetPathParamsRule() *model.Rule {
// add operation tag defined rule
return &model.Rule{
Name: "Check path validity and definition",
Id: PathParamsRule,
Formats: model.AllFormats,
Description: "Path parameters must be defined and valid.",
Given: "$",
Resolved: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Recommended: true,
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "oasPathParam",
},
HowToFix: pathParamsFix,
}
}
// GetGlobalOperationTagsRule will check that an operation tag exists in top level tags
// This rule was dropped to a warning from an error after discussion here:
// - https://github.com/daveshanley/vacuum/issues/215
func GetGlobalOperationTagsRule() *model.Rule {
return &model.Rule{
Name: "Check operation tags exist globally",
Id: OperationTagDefined,
Formats: model.AllFormats,
Description: "Operation tags must be defined in global tags.",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryTags],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "oasTagDefined",
},
HowToFix: globalOperationTagsFix,
}
}
// GetOperationParametersRule will check that an operation has valid parameters defined
func GetOperationParametersRule() *model.Rule {
return &model.Rule{
Name: "Check operation parameters",
Id: OperationParameters,
Formats: model.AllFormats,
Description: "Operation parameters are unique and non-repeating.",
Given: "$.paths",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "oasOpParams",
},
HowToFix: operationParametersFix,
}
}
// GetOAS2FormDataConsumesRule will check that an "application/x-www-form-urlencoded" or "multipart/form-data"
// is defined in the 'consumes' node for in any parameters that use in formData.
func GetOAS2FormDataConsumesRule() *model.Rule {
return &model.Rule{
Name: "Check operation parameter 'formData' definition",
Id: Oas2OperationFormDataConsumeCheck,
Formats: model.OAS2Format,
Description: "Operations with `in: formData` parameter must include `application/x-www-form-urlencoded` or" +
" `multipart/form-data` in their `consumes` property.",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "oasOpFormDataConsumeCheck",
},
HowToFix: formDataConsumesFix,
}
}
// GetOAS2PolymorphicAnyOfRule will check that 'anyOf' has not been used in a swagger spec (introduced in 3.0)
func GetOAS2PolymorphicAnyOfRule() *model.Rule {
return &model.Rule{
Name: "Check for invalid use of 'anyOf'",
Id: Oas2AnyOf,
Formats: model.OAS2Format,
Description: "`anyOf` was introduced in OpenAPI 3.0, cannot be used un OpenAPI 2 specs",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategorySchemas],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "oasPolymorphicAnyOf",
},
HowToFix: oas2AnyOfFix,
}
}
// GetOAS2PolymorphicOneOfRule will check that 'oneOf' has not been used in a swagger spec (introduced in 3.0)
func GetOAS2PolymorphicOneOfRule() *model.Rule {
return &model.Rule{
Name: "Check for invalid use of 'oneOf'",
Id: Oas2OneOf,
Formats: model.OAS2Format,
Description: "`oneOf` was introduced in OpenAPI 3.0, cannot be used un OpenAPI 2 specs",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategorySchemas],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "oasPolymorphicOneOf",
},
HowToFix: oas2OneOfFix,
}
}
// GetOAS2SchemaRule will check that the schema is valid for swagger docs.
func GetOAS2SchemaRule() *model.Rule {
return &model.Rule{
Name: "Check schema is valid OpenAPI 2",
Id: Oas2Schema,
Formats: model.OAS2Format,
Description: "OpenAPI 2 specification is invalid",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryValidation],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "oasDocumentSchema",
},
HowToFix: oas2SchemaCheckFix,
}
}
// GetOAS3SchemaRule will check that the schema is valid for openapi 3+ docs.
func GetOAS3SchemaRule() *model.Rule {
return &model.Rule{
Name: "Check spec is valid OpenAPI 3",
Id: Oas3Schema,
Formats: model.OAS3Format,
Description: "OpenAPI 3 specification is invalid",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategorySchemas],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "oasDocumentSchema",
},
HowToFix: oas3SchemaCheckFix,
}
}
// GetOperationIdUniqueRule will check to make sure that operationIds are all unique and non-repeating
func GetOperationIdUniqueRule() *model.Rule {
return &model.Rule{
Name: "Check operations for unique operationId",
Id: OperationOperationIdUnique,
Formats: model.AllFormats,
Description: "Every operation must have unique `operationId`.",
Given: "$.paths",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "oasOpIdUnique",
},
HowToFix: operationIdUniqueFix,
}
}
// GetOperationSingleTagRule will check to see if an operation has more than a single tag
func GetOperationSingleTagRule() *model.Rule {
return &model.Rule{
Name: "Check operations for multiple tags",
Id: OperationSingularTag,
Formats: model.AllFormats,
Description: "Operation cannot have more than a single tag defined",
Given: "$",
Resolved: false,
Recommended: false,
RuleCategory: model.RuleCategories[model.CategoryTags],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "oasOpSingleTag",
},
HowToFix: operationSingleTagFix,
}
}
// GetOAS2APIHostRule will check swagger specs for the host property being set.
func GetOAS2APIHostRule() *model.Rule {
return &model.Rule{
Name: "Check spec for 'host' value",
Id: Oas2APIHost,
Formats: model.OAS2Format,
Description: "OpenAPI `host` must be present and a non-empty string",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryInfo],
Type: Style,
Severity: model.SeverityInfo,
Then: model.RuleAction{
Field: "host",
Function: "truthy",
},
HowToFix: oas2APIHostFix,
}
}
// GetOperationIdRule will check to make sure that operationIds exist on all operations
func GetOperationIdRule() *model.Rule {
return &model.Rule{
Name: "Check operations for an operationId",
Id: OperationOperationId,
Formats: model.AllFormats,
Description: "Every operation must contain an `operationId`.",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "oasOpId",
},
HowToFix: operationIdExistsFix,
}
}
// GetOperationSuccessResponseRule will check that every operation has a success response defined.
func GetOperationSuccessResponseRule() *model.Rule {
return &model.Rule{
Name: "Check operations for success response",
Id: OperationSuccessResponse,
Formats: model.AllFormats,
Description: "Operation must have at least one `2xx` or a `3xx` response.",
Given: "$",
Resolved: true,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategoryOperations],
Type: Style,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Field: "responses",
Function: "oasOpSuccessResponse",
},
HowToFix: operationSuccessResponseFix,
}
}
// GetDuplicatedEntryInEnumRule will check that enums used are not duplicates
func GetDuplicatedEntryInEnumRule() *model.Rule {
return &model.Rule{
Name: "Check for duplicate enum entries",
Id: DuplicatedEntryInEnum,
Formats: model.AllFormats,
Description: "Enum values must not have duplicate entry",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategorySchemas],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "duplicatedEnum",
},
HowToFix: duplicatedEntryInEnumFix,
}
}
// GetNoRefSiblingsRule will check that there are no sibling nodes next to a $ref (which is technically invalid)
func GetNoRefSiblingsRule() *model.Rule {
return &model.Rule{
Name: "Check for siblings to $ref values",
Id: NoRefSiblings,
Formats: model.AllFormats,
Description: "$ref values cannot be placed next to other properties (like a description)",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategorySchemas],
Type: Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "refSiblings",
},
HowToFix: noRefSiblingsFix,
}
}
// GetOAS3UnusedComponentRule will check that there aren't any components anywhere that haven't been used.
func GetOAS3UnusedComponentRule() *model.Rule {
return &model.Rule{
Name: "Check for unused components",
Id: Oas3UnusedComponent,
Formats: model.OAS3AllFormat,
Description: "Check for unused components and bad references",
Given: "$",
Resolved: false,
Recommended: true,
RuleCategory: model.RuleCategories[model.CategorySchemas],
Type: Validation,
Severity: model.SeverityWarn,
Then: model.RuleAction{
Function: "oasUnusedComponent",
},
HowToFix: unusedComponentFix,