@azure-tools/typespec-azure-resource-manager
Version:
TypeSpec Azure Resource Manager library
48 lines • 2.06 kB
JavaScript
import { createRule, getProperty, } from "@typespec/compiler";
import * as http from "@typespec/http";
import { getArmResources } from "../resource.js";
import { isReadonlyProperty } from "../utils.js";
/**
* The resource 'name' field should be marked with 'read' visibility and an @path decorator.
*/
export const resourceNameRule = createRule({
name: "resource-name",
severity: "warning",
description: "Check the resource name.",
messages: {
default: `The resource 'name' field should be marked with 'read' visibility and an @path decorator.`,
nameInvalid: "Arm resource name must contain only alphanumeric characters.",
},
create(context) {
return {
model: (model) => {
const resources = getArmResources(context.program);
const armResource = resources.find((re) => re.typespecType === model);
if (armResource) {
if (!checkResourceModelName(model.name)) {
context.reportDiagnostic({
messageId: "nameInvalid",
target: model,
});
}
// check that the name property is properly attributed
const name = getProperty(model, "name");
if (name && checkResourceName(name)) {
context.reportDiagnostic({
target: name,
});
}
}
function checkResourceName(nameProp) {
return !(http.isPathParam(context.program, nameProp) &&
isReadonlyProperty(context.program, nameProp));
}
function checkResourceModelName(name) {
const matches = name.match(/^[A-Z][0-9A-Za-z]+$/);
return matches !== null && matches.length === 1;
}
},
};
},
});
//# sourceMappingURL=resource-name.js.map