Created
March 14, 2023 19:00
-
-
Save mikekistler/0276a6e756325f93184aded2a8852c63 to your computer and use it in GitHub Desktop.
TypeSpec linter for api-version query parameter
This file contains hidden or 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
import { createTypeSpecLibrary } from "@typespec/compiler"; | |
import { createRule, getLinter } from "@typespec/lint"; | |
import { isQueryParam } from "@typespec/http"; | |
export const myLibrary = createTypeSpecLibrary({ | |
name: "myLibrary", | |
diagnostics: { | |
"version-policy": { | |
severity: "error", | |
messages: { | |
default: | |
"Every operation must have a required 'api-version' query parameter", | |
}, | |
}, | |
}, | |
}); | |
const isApiVersion = (p) => { | |
return p.name === "apiVersion" && !p.optional && p.type.name === 'string'; | |
} | |
const versionPolicyRule = createRule({ | |
name: "version-policy", | |
create({ program }) { | |
return { | |
operation: (op) => { | |
const params = op.parameters?.properties; | |
if (![...params.values()].some(p => isApiVersion(p) && isQueryParam(program, p))) { | |
myLibrary.reportDiagnostic(program, { | |
code: "version-policy", | |
target: op, | |
}); | |
} | |
} | |
}; | |
}, | |
}); | |
const linter = getLinter(myLibrary); | |
export function $onValidate(program) { | |
linter.registerRule(versionPolicyRule, { enable: true }); | |
linter.lintOnValidate(program); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment