-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from dtolb/DT_update-functions
Updates to Rulesets, Tests, & Functions
- Loading branch information
Showing
53 changed files
with
2,562 additions
and
632 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright 2022 Cisco Systems, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import commonAbbreviations from './commonAbbreviations.js'; | ||
import identifyCasingType from './identifyCasingType.js'; | ||
|
||
const removeCommonAbbreviations = (string) => { | ||
const regex = new RegExp(`\\b(${ commonAbbreviations.join('|') })\\b`, 'g'); | ||
|
||
return string.replace(regex, '').trim(); | ||
}; | ||
|
||
const checkConsistency = (strings) => { | ||
const cleanedStrings = strings.map(removeCommonAbbreviations).filter(Boolean); | ||
|
||
const casingCounts = cleanedStrings.reduce((acc, str) => { | ||
const type = identifyCasingType(str); | ||
|
||
acc[type] = (acc[type] || 0) + 1; | ||
|
||
return acc; | ||
}, {}); | ||
|
||
const relevantTypes = Object.keys(casingCounts).filter((type) => type !== 'lowercase' && type !== 'unknown'); | ||
|
||
const isConsistent = relevantTypes.length <= 1; | ||
|
||
if (!isConsistent) { | ||
const foundTypes = relevantTypes.length > 0 ? relevantTypes : ['lowercase', 'unknown'].filter((type) => casingCounts[type]); | ||
const typeDescriptions = foundTypes.map((type) => ({ | ||
'camelCase': 'camel', | ||
'PascalCase': 'Pascal', | ||
'kebab-case': 'kebab', | ||
'snake_case': 'snake', | ||
'unknown': 'unknown', | ||
}[type] || type)).join(', '); | ||
|
||
return { | ||
message: `Inconsistent casing types detected. Found: ${ typeDescriptions }.`, | ||
}; | ||
} | ||
|
||
return null; // Consistency found or acceptable ambiguity with 'lowercase' | ||
}; | ||
|
||
export default checkConsistency; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
/** | ||
* Copyright 2022 Cisco Systems, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
const isObject = (value) => value !== null && typeof value === 'object'; | ||
|
||
export const collectTags = (targetVal) => { | ||
if (!isObject(targetVal)) { | ||
return new Map(); | ||
} | ||
|
||
const globalTags = targetVal.tags | ||
? targetVal.tags.map((tagObj, index) => [tagObj.name, ['tags', index.toString()]]) | ||
: []; | ||
|
||
const operationTags = Object.entries(targetVal.paths || {}).flatMap(([pathKey, operations]) => Object.entries(operations).flatMap(([method, operation]) => operation.tags | ||
? operation.tags.map((tag, index) => [tag, ['paths', pathKey, method, 'tags', index.toString()]]) | ||
: [], | ||
), | ||
); | ||
|
||
const allTags = [...globalTags, ...operationTags].reduce((acc, [tag, path]) => { | ||
if (!acc.has(tag)) { | ||
acc.set(tag, new Set()); | ||
} | ||
|
||
acc.get(tag).add(path.join('.')); | ||
|
||
return acc; | ||
}, new Map()); | ||
|
||
return allTags; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright 2022 Cisco Systems, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
export default function ensureErrorConsistency(targetVal, opts, paths, otherValues) { | ||
const errors = []; | ||
const descriptionsMap = {}; | ||
|
||
// Traverse the API document to collect descriptions and their paths | ||
Object.entries(targetVal.paths || {}).forEach(([path, methods]) => { | ||
Object.entries(methods).forEach(([method, details]) => { | ||
if (details.responses) { | ||
Object.entries(details.responses).forEach(([statusCode, response]) => { | ||
if (statusCode.startsWith('4') || statusCode.startsWith('5')) { | ||
const description = response.description; | ||
|
||
if (!descriptionsMap[statusCode]) { | ||
descriptionsMap[statusCode] = {}; | ||
} | ||
|
||
if (!descriptionsMap[statusCode][description]) { | ||
descriptionsMap[statusCode][description] = []; | ||
} | ||
|
||
descriptionsMap[statusCode][description].push(`paths.${ path }.${ method }.responses.${ statusCode }`); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
// Generate errors for inconsistencies | ||
Object.entries(descriptionsMap).forEach(([statusCode, descriptions]) => { | ||
if (Object.keys(descriptions).length > 1) { // More than one unique description for this status code | ||
let allPaths = []; | ||
const allDescriptions = Object.keys(descriptions); | ||
|
||
Object.values(descriptions).forEach((paths) => { | ||
allPaths = allPaths.concat(paths); | ||
}); | ||
|
||
errors.push({ | ||
message: `Inconsistent descriptions for status code ${ statusCode }. Found '${ allDescriptions.join("', '") }'.`, | ||
// path: allPaths[0].split('.') | ||
}); | ||
} | ||
}); | ||
|
||
return errors; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright 2022 Cisco Systems, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
import ensureErrorConsistency from './ensureErrorConsistency'; | ||
|
||
describe('ensureErrorConsistency', () => { | ||
it('should import the function without error', () => { | ||
expect(ensureErrorConsistency).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.