Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability for ARM report #422

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions packages/azure-openapi-validator/autorest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ const cachedFiles = new Map<string,any>()

function convertLintMsgToAutoRestMsg(message:LintResultMessage):Message {
// try to extract provider namespace and resource type
const path = message.jsonpath?.[1] === "paths" && message.jsonpath[2]
const pathComponents = typeof path === "string" && path.split("/")
const pathComponentsProviderIndex = pathComponents && pathComponents.indexOf("providers")
const pathComponentsTail =
pathComponentsProviderIndex && pathComponentsProviderIndex >= 0 && pathComponents.slice(pathComponentsProviderIndex + 1)
const pathComponentProviderNamespace = pathComponentsTail && pathComponentsTail[0]
const pathComponentResourceType = pathComponentsTail && pathComponentsTail[1]
const path = message.jsonpath?.[0] === "paths" ? message.jsonpath[1] : undefined
const pathComponents = typeof path === "string" ? path.split("/") : undefined
const pathComponentsProviderIndex = pathComponents ? pathComponents.lastIndexOf("providers") : -1
const pathComponentsTail = pathComponents && pathComponentsProviderIndex >= 0 ? pathComponents.slice(pathComponentsProviderIndex + 1) : []
const pathComponentProviderNamespace = pathComponentsTail[0] || ""

// we can infer the resource type from the path pattern like: "{scope}/providers/MyNamepace/RT1/{rt1Name}/RT2/{rt2Name}" whose pathComponentsTail is ["MyNamespace","RT1","{rt1Name}","RT2","{rt2Name}"]
const pathComponentResourceType = pathComponentsTail && pathComponentsTail.length >= 3 && pathComponentsTail.length % 2 ? pathComponentsTail[pathComponentsTail.length -2] : ""
const type = message.message.startsWith("[Verbose]this is a verbose message to indicate that this rule was passed for specific swagger schema successfully and no fix is needed") ? "verbose" : message.type
const msg = {
Channel: message.type,
Channel: type,
Text: message.message,
Key: [message.code],
Source: [
Expand Down
23 changes: 17 additions & 6 deletions packages/rulesets/generated/spectral/az-arm.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ function isSchemaEqual(a, b) {
}
return false;
}
function createRuleFunctionWithPasses(fn) {
return (input, options, ctx) => {
const messsages = fn(input, options, ctx);
if (messsages.length === 0) {
messsages.push({
message: `[Verbose]this is a verbose message to indicate that this rule was passed for specific swagger schema successfully and no fix is needed, please ignore it.`,
path: ctx.path
});
}
return messsages;
};
}

const nextLinkPropertyMustExist = (opt, _opts, ctx) => {
var _a, _b, _c;
Expand Down Expand Up @@ -1405,20 +1417,20 @@ const bodyParamRepeatedInfo = (pathItem, _opts, paths) => {
return errors;
};

function camelCase(propertyName, options, { path }) {
const camelCase = createRuleFunctionWithPasses((propertyName, options, { path }) => {
if (!propertyName) {
return [];
}
const errors = [];
const camelCaseReg = /^[a-z0-9$-]+([A-Z]{1,3}[a-z0-9$-]+)+$|^[a-z0-9$-]+$|^[a-z0-9$-]+([A-Z]{1,3}[a-z0-9$-]+)*[A-Z]{1,3}$/;
if (!camelCaseReg.test(propertyName)) {
errors.push({
message: "",
message: "Property name should be camel case.",
path
});
}
return errors;
}
});

const collectionObjectPropertiesNaming = (op, _opts, paths) => {
var _a, _b;
Expand Down Expand Up @@ -1734,15 +1746,14 @@ const pathBodyParameters = (parameters, _opts, paths) => {
return errors;
};

const pathSegmentCasing = (apiPaths, _opts, paths) => {
const pathSegmentCasing = (apiPaths, _opts, { path }) => {
if (apiPaths === null || typeof apiPaths !== 'object') {
return [];
}
if (!_opts || !_opts.segments || !Array.isArray(_opts.segments)) {
return [];
}
const segments = _opts.segments;
const path = paths.path || [];
const errors = [];
for (const apiPath of Object.keys(apiPaths)) {
segments.forEach((seg) => {
Expand Down Expand Up @@ -2343,7 +2354,7 @@ const ruleset = {
},
DefinitionsPropertiesNamesCamelCase: {
description: "Property names should be camel case.",
message: "Property name should be camel case.",
message: "{{error}}",
severity: "error",
resolved: false,
given: "$.definitions..[?(@property === 'type' && @ === 'object')]^.properties[?(@property.match(/^[^@].+$/))]~",
Expand Down
2 changes: 1 addition & 1 deletion packages/rulesets/src/spectral/az-arm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ const ruleset: any = {
// this rule covers BodyPropertiesNamesCamelCase and DefinitionsPropertiesNamesCamelCase
DefinitionsPropertiesNamesCamelCase: {
description: "Property names should be camel case.",
message: "Property name should be camel case.",
message: "{{error}}",
severity: "error",
resolved: false,
given: "$.definitions..[?(@property === 'type' && @ === 'object')]^.properties[?(@property.match(/^[^@].+$/))]~",
Expand Down
7 changes: 4 additions & 3 deletions packages/rulesets/src/spectral/functions/camel-case.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { IFunctionResult } from "@stoplight/spectral-core"
import type { JsonPath } from "@stoplight/types"
import { createRuleFunctionWithPasses } from "./utils"

// check if given propertyName is camel case
export function camelCase(propertyName: string, options: any, { path }: { path: JsonPath }): IFunctionResult[] {
export const camelCase = createRuleFunctionWithPasses((propertyName: string, options: any, { path }: { path: JsonPath }): IFunctionResult[] => {
if (!propertyName) {
return [] as IFunctionResult[]
}
Expand All @@ -12,11 +13,11 @@ export function camelCase(propertyName: string, options: any, { path }: { path:

if (!camelCaseReg.test(propertyName)) {
errors.push({
message:"",
message: "Property name should be camel case.",
path
})
}
return errors
}
})

export default camelCase
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// _opts has a property 'segments' as string[] to specify the segment names to check for
export const pathSegmentCasing = (apiPaths:any, _opts:any, paths: any) => {
export const pathSegmentCasing = (apiPaths:any, _opts:any, {path}: any) => {
if (apiPaths === null || typeof apiPaths !== 'object') {
return [];
}
Expand All @@ -8,8 +8,6 @@ export const pathSegmentCasing = (apiPaths:any, _opts:any, paths: any) => {
return []
}
const segments = _opts.segments
const path = paths.path || [];

const errors:any[] = [];
for (const apiPath of Object.keys(apiPaths)) {
segments.forEach((seg:string) => {
Expand Down
15 changes: 15 additions & 0 deletions packages/rulesets/src/spectral/functions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IFunctionResult, RulesetFunctionContext } from "@stoplight/spectral-core"

/**
* get all properties as array
*/
Expand Down Expand Up @@ -174,3 +176,16 @@ export function isSchemaEqual(a: any, b: any): boolean {
}
return false;
}

export function createRuleFunctionWithPasses<I = unknown, O = unknown>(fn:(input:I, options:O, ctx: RulesetFunctionContext)=> IFunctionResult[]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createRuleFunctionWithPasses

createRuleFunctionForEmittingSuccessMessage

return (input:I, options:O, ctx: RulesetFunctionContext):IFunctionResult[] => {
const messsages = fn(input,options,ctx)
if (messsages.length === 0) {
messsages.push({
message: `[Verbose]this is a verbose message to indicate that this rule was passed for specific swagger schema successfully and no fix is needed, please ignore it.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a verbose message to indicate that this rule was passed for specific swagger schema successfully and no fix is needed, please ignore it.

How about this text for the verbose message - "ARM RPC rule passed for path {path}" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path is another field of the error object, I think we don't need to duplicate it in the message

path: ctx.path
})
}
return messsages
}
}