@@ -9,7 +9,7 @@ import { resolveObject } from './utils/componentUtils.js';
99
1010const RULE_NAME = 'xgen-IPA-119-no-default-for-cloud-providers' ;
1111const ERROR_MESSAGE = 'When using a provider field or param, API producers should not define a default value.' ;
12- export default ( input , { propertyNameToLookFor } , { path, documentInventory } ) => {
12+ export default ( input , { propertyNameToLookFor, cloudProviderEnumValues } , { path, documentInventory } ) => {
1313 const oas = documentInventory . resolved ;
1414 const propertyObject = resolveObject ( oas , path ) ;
1515 const fieldType = path [ path . length - 2 ] ;
@@ -19,14 +19,28 @@ export default (input, { propertyNameToLookFor }, { path, documentInventory }) =
1919 return ;
2020 }
2121
22- const errors = checkViolationsAndReturnErrors ( input , propertyObject , path , propertyNameToLookFor , fieldType ) ;
22+ const errors = checkViolationsAndReturnErrors (
23+ input ,
24+ propertyObject ,
25+ path ,
26+ propertyNameToLookFor ,
27+ fieldType ,
28+ cloudProviderEnumValues
29+ ) ;
2330 if ( errors . length !== 0 ) {
2431 return collectAndReturnViolation ( path , RULE_NAME , errors ) ;
2532 }
2633 collectAdoption ( path , RULE_NAME ) ;
2734} ;
2835
29- function checkViolationsAndReturnErrors ( propertyName , propertyObject , path , propertyNameToLookFor , fieldType ) {
36+ function checkViolationsAndReturnErrors (
37+ propertyName ,
38+ propertyObject ,
39+ path ,
40+ propertyNameToLookFor ,
41+ fieldType ,
42+ cloudProviderEnumValues
43+ ) {
3044 try {
3145 if ( fieldType === 'properties' ) {
3246 if ( propertyName === propertyNameToLookFor && propertyObject . default !== undefined ) {
@@ -37,6 +51,21 @@ function checkViolationsAndReturnErrors(propertyName, propertyObject, path, prop
3751 } ,
3852 ] ;
3953 }
54+
55+ if ( Array . isArray ( propertyObject . enum ) && propertyObject . enum . length > 0 ) {
56+ const enumValues = propertyObject . enum ;
57+ const hasCloudProviderEnumValue = cloudProviderEnumValues . every ( ( cloudProviderValue ) =>
58+ enumValues . includes ( cloudProviderValue )
59+ ) ;
60+ if ( hasCloudProviderEnumValue && propertyObject . default !== undefined ) {
61+ return [
62+ {
63+ path,
64+ message : ERROR_MESSAGE ,
65+ } ,
66+ ] ;
67+ }
68+ }
4069 } else if ( fieldType === 'parameters' ) {
4170 if ( propertyObject . name === propertyNameToLookFor && propertyObject . schema . default !== undefined ) {
4271 return [
@@ -46,6 +75,22 @@ function checkViolationsAndReturnErrors(propertyName, propertyObject, path, prop
4675 } ,
4776 ] ;
4877 }
78+
79+ if ( Array . isArray ( propertyObject . schema . enum ) && propertyObject . schema . enum . length > 0 ) {
80+ const enumValues = propertyObject . schema . enum ;
81+ const hasCloudProviderEnumValue = cloudProviderEnumValues . every ( ( cloudProviderValue ) =>
82+ enumValues . includes ( cloudProviderValue )
83+ ) ;
84+
85+ if ( hasCloudProviderEnumValue && propertyObject . schema . default !== undefined ) {
86+ return [
87+ {
88+ path,
89+ message : ERROR_MESSAGE ,
90+ } ,
91+ ] ;
92+ }
93+ }
4994 }
5095
5196 return [ ] ;
0 commit comments