Skip to content

Commit

Permalink
Merge pull request #21 from CiscoDevNet/new-rules
Browse files Browse the repository at this point in the history
add rule for multi-version and operation-id-required-uniq-verb-noun
  • Loading branch information
coliu19 authored Feb 2, 2024
2 parents 45d316c + 68d3370 commit 133a84c
Show file tree
Hide file tree
Showing 21 changed files with 1,979 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
rules: {
'accessor-pairs': 'error',
'array-bracket-newline': [
'error',
'warn',
{
multiline: true,
},
Expand All @@ -40,7 +40,7 @@ module.exports = {
],
'array-callback-return': 'error',
'array-element-newline': [
'error',
'warn',
{
ArrayExpression: 'always',
ArrayPattern: 'consistent',
Expand Down
29 changes: 0 additions & 29 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,3 @@ jobs:
- run: npm publish --access=public
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

publish-to-github:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v3
# Setup .npmrc file to publish to GitHub Packages
- uses: actions/setup-node@v3
with:
node-version: '16.x'
registry-url: 'https://npm.pkg.github.com'
# Defaults to the user or organization that owns the workflow file
# scope: '@cisco-developer'
- name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Show tag
run: |
echo $RELEASE_VERSION
echo ${{ env.RELEASE_VERSION }}
- run: npm --no-git-tag-version version ${{ env.RELEASE_VERSION }}
- run: cat package.json
- run: npm ci
# Seems access does not matter for github.
- run: npm publish --access=restricted
# - run: npm publish --access=public
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40 changes: 40 additions & 0 deletions contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import completedSchema from './functions/completedSchema.js';
import ensureField from './functions/ensureField.js';
import includeAll from './functions/includeAll.js';
import keyMatchAnyPattern from './functions/keyMatchAnyPattern.js';
import multiVersion from './functions/multiVersion.js';
import operationIdCheck from './functions/operationIdCheck.js';
export default {
'extends': [
[
Expand Down Expand Up @@ -160,5 +162,43 @@ export default {
},
},
},
'multi-versions-server-url-missing-version': {
'description': 'No version is specified in the server object of the OpenAPI Document. Best practices recommend specifying the version in the server object only once',
'message': '{{description}}; {{error}}',
'severity': 'warn',
'given': [
'$',
],
'then': {
'function': multiVersion,
'functionOptions': {
'check': 'server-url-missing',
},
},
},
'multi-versions': {
'description': 'should only contain a single API version at a time.',
'message': '{{description}}; {{error}}',
'severity': 'error',
'given': [
'$',
],
'then': {
'function': multiVersion,
},
},
'operationId-required-and-unique': {
'description': 'operationId must be required and uniq',
'message': '{{description}}; {{error}}',
'severity': 'error',
'given': [
'$',
],
'then': [
{
'function': operationIdCheck,
},
],
},
},
};
19 changes: 18 additions & 1 deletion documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import { oas } from '@stoplight/spectral-rulesets';
import { xor } from '@stoplight/spectral-functions/dist';
import ensureExamples from './functions/ensureExamples.js';
import ensureField from './functions/ensureField.js';
export default {
Expand All @@ -30,7 +31,23 @@ export default {
'info-contact': 'warn',
'info-description': 'warn',
'info-license': 'warn',
'license-url': 'warn',
'license-url': {
'description': 'License object must include "url" or "identifier"',
'message': '{{description}}; {{error}}',
'severity': 'warn',
'given': [
'$.info.license',
],
'then': {
'function': xor,
'functionOptions': {
'properties': [
'url',
'identifier',
],
},
},
},
'description-for-every-attribute': {
'description': 'DEA - Descriptions for Every Attribute',
'message': 'For every attribute that is present in the OAS document, if a description is proposed as optional to complement that attribute, then yes it must be present; {{error}}',
Expand Down
2 changes: 1 addition & 1 deletion examples/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pets"
"$ref": "#/components/schemas/Pet"
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions functions/defaultInEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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 { isObject } from '../util/funcUtils.js';

/**
* Checks if there is only one version in server urls and paths.
* @param {string} targetVal The string to lint
* @param {object} opts checks to do
*/
export default function (targetVal) {
if (!isObject(targetVal)) {
return;
}

const results = [];

for (const key in targetVal) {
if (targetVal[key].enum && targetVal[key].default) {
if (!targetVal[key].enum.includes(targetVal[key].default)) {
results.push({
message: 'default not in enum',
});
}
}
}

return results;
}
74 changes: 74 additions & 0 deletions functions/defaultInEnum.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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 defaultInEnum from './defaultInEnum.js';

describe('defaultInEnum', () => {
const targetVal = {
'port': {
'enum': [
8443,
443,
],
'default': 8443,
},
};

test('should pass when default in enum', () => {
const res = defaultInEnum(targetVal);

expect(res).toEqual([]);
});


test('should pass if no default field in enum', () => {
const targetVal = {
'port': {
'enum': [
8443,
443,
],
},
};

const res = defaultInEnum(targetVal);

expect(res).toEqual([]);
});

test('should fail if default in not in enum', () => {
const targetVal = {
'port': {
'enum': [
8443,
443,
],
'default': 1443,
},
};

const res = defaultInEnum(targetVal);

expect(res).toEqual([
{
message: 'default not in enum',
},
]);
});

});
Loading

0 comments on commit 133a84c

Please sign in to comment.