Create a Conformance Rule

For local conformance rules, the resolution utilities from @nx/js are used in the same way they are for all other JavaScript/TypeScript files in Nx. Therefore, you can simply reference an adhoc JavaScript file or TypeScript file in your "rule" property (as long as the path is resolvable based on your package manager and/or tsconfig setup), and the rule will be loaded/transpiled as needed. The rule implementation file should also have a schema.json file next to it that defines the available rule options, if any.

Therefore, in practice, writing your local conformance rules in an Nx generated library is the easiest way to organize them and ensure that they are easily resolvable via TypeScript. The library in question could also be an Nx plugin, but it does not have to be.

To write your own conformance rule, run the @nx/powerpack-conformance:create-rule generator and answer the prompts.

โฏ

nx g @nx/powerpack-conformance:create-rule

1 NX Generating @nx/powerpack-conformance:create-rule 2 3โœ” What is the name of the rule? ยท local-conformance-rule-example 4โœ” Which directory do you want to create the rule directory in? ยท packages/my-plugin/local-conformance-rule 5โœ” What category does this rule belong to? ยท security 6โœ” What reporter do you want to use for this rule? ยท project-reporter 7โœ” What is the description of the rule? ยท an example of a conformance rule 8CREATE packages/my-plugin/local-conformance-rule/local-conformance-rule-example/index.ts 9CREATE packages/my-plugin/local-conformance-rule/local-conformance-rule-example/schema.json 10

The generated rule definition file should look like this:

packages/my-plugin/local-conformance-rule/index.ts
1import { 2 createConformanceRule, 3 ProjectViolation, 4} from '@nx/powerpack-conformance'; 5 6export default createConformanceRule({ 7 name: 'local-conformance-rule-example', 8 category: 'security', 9 description: 'an example of a conformance rule', 10 reporter: 'project-reporter', 11 implementation: async (context) => { 12 const violations: ProjectViolation[] = []; 13 14 return { 15 severity: 'low', 16 details: { 17 violations, 18 }, 19 }; 20 }, 21}); 22

To enable the rule, you need to register it in the nx.json file.

nx.json
1{ 2 "conformance": { 3 "rules": [ 4 { 5 "rule": "./packages/my-plugin/local-conformance-rule/index.ts" 6 } 7 ] 8 } 9} 10

Note that the severity of the error is defined by the rule author and can be adjusted based on the specific violations that are found.

Conformance Rule Examples

There are three types of reporters that a rule can use.

  • project-reporter - The rule evaluates an entire project at a time.
  • project-files-reporter - The rule evaluates a single project file at a time.
  • non-project-files-reporter - The rule evaluates files that don't belong to any project.

The @nx/powerpack-owners plugin adds an owners metadata property to every project node that has an owner in the project graph. This rule checks each project node metadata to make sure that each project has some owner defined.

1import { ProjectGraphProjectNode } from '@nx/devkit'; 2import { 3 createConformanceRule, 4 ProjectViolation, 5} from '@nx/powerpack-conformance'; 6 7export default createConformanceRule({ 8 name: 'ensure-owners', 9 category: 'consistency', 10 description: 'Ensure that all projects have owners defined via Nx Owners.', 11 reporter: 'project-reporter', 12 implementation: async (context) => { 13 const violations: ProjectViolation[] = []; 14 15 for (const node of Object.values( 16 context.projectGraph.nodes 17 ) as ProjectGraphProjectNode[]) { 18 const metadata = node.data.metadata; 19 if (!metadata?.owners || Object.keys(metadata.owners).length === 0) { 20 violations.push({ 21 sourceProject: node.name, 22 message: `This project currently has no owners defined via Nx Owners.`, 23 }); 24 } 25 } 26 27 return { 28 severity: 'medium', 29 details: { 30 violations, 31 }, 32 }; 33 }, 34}); 35

Share Conformance Rules Across Workspaces

If you have an Enterprise Nx Cloud contract, you can share your conformance rules across every repository in your organization. Read more in these articles: