npm-package-json-lint icon indicating copy to clipboard operation
npm-package-json-lint copied to clipboard

[perhaps a Feature Request] Custom rule

Open askarby opened this issue 5 years ago • 10 comments

It may just be me, but...

I can't seem to find a way to include my own, custom rule (just like ESLint and TSLint provides). Granted, I could create a rule, and submit a pull request for it, but I may have a very domain specific rule, that I want to apply.

Is there already an option to do that.... if not, this can be considered a feature request 😄

askarby avatar Mar 14 '19 08:03 askarby

Hey @askarby! We don't have that capability yet. I like the suggestion.

What rule are you thinking about adding?

tclindner avatar Mar 16 '19 01:03 tclindner

I'm thinking about rules like:

  • disallowing relative paths in dependencies
  • disallowing git-repo dependencies
  • disallowing specific dependency versions (based on certain git repos)

etc.

Granted, I could contribute separate rules for those, but sometimes, the rules you require can be very specific (hence not very general purpose)

askarby avatar Mar 20 '19 07:03 askarby

I'm in for this too. For example, we want to restrict the 'files' value for library packages in our mono-repo, so adding something to validate the value of it against something we want would be awesome.

dietergeerts avatar Jul 30 '19 09:07 dietergeerts

Thanks, @dietergeerts. I'm moving (slowly) through v4. I'm hoping this will be a part of it. Do you have any thoughts on your preferred interface for writing custom rules? I'm starting to think through how this might work.

tclindner avatar Aug 06 '19 02:08 tclindner

I too would love this feature. In terms of an interface, I'd prefer to have one similar to that of tslint's custom rules API.

On that same note, I'd prefer to have a TypeScript interface available to write custom rules.

aaroncadrian avatar Nov 09 '19 23:11 aaroncadrian

Consolidating issue #208 here. Adding the notes mentioned on the other issue:

There are use cases where certain projects may want to implement custom rule integration. A plugin system similar to adding rules to eslint or stylelint.

Examples of custom rules:

  • Disallow specific packages from being listed in dependencies, devDependencies, or peerDependencies.
  • Require certain dependencies be listed in certain sections. IE: Package A can never be listed in dependencies. It should always be a devDependency.
  • Mutually exclusive dependencies. IE: If dependency A is listed dependency B cannot be.
  • Require certain custom keys be populated in the package.json
  • Versioning restraints. Dependency A should never be hard locked. "1.0.0" vs "^1.0.0"

StephenEsser avatar Feb 24 '20 01:02 StephenEsser

Would it not be a possibility to have a JSON Schema provided that can be checked? Because then we can provide all our custom rules through that JSON Schema. Off course, error messages may be a bit more difficult to understand then, but at least, then we can define any custom rule we want. It is JSON after all.

I like a lot of the rules that are available, and they make it easy to configure and know what it will check without needing to know some internals on JSON Schemas, but the way I see it is that such extra optional JSON Schema is similar to what ESLint provides with the https://eslint.org/docs/rules/no-restricted-syntax rule, where you can use a "selector" for the syntax you don't want.

dietergeerts avatar Nov 11 '21 10:11 dietergeerts

A few custom rules I'd make if I could:

  • enforce that there's no resolutions field in a package.json file
  • enforce that a specific value is set in the browserlist field so that all front end repos support the same set of browsers
  • enforce that workspaces don't have any root-level packages (or a specified set of them)
  • enforce certain commands existing

abejfehr avatar Mar 15 '22 16:03 abejfehr

I would like to create a rule for enforcing naming conventions for scripts. We have multiple repos and the naming is all over the place.

kevinbarabash avatar Feb 20 '23 18:02 kevinbarabash

Even though it is not officially supported and requires using internal api, but custom rules can be added using nodejs api:

// lint-package.mjs, run this with `node lint-package.mjs`
import path from 'path';
import { fileURLToPath } from 'url';
import { NpmPackageJsonLint } from 'npm-package-json-lint';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const npmPackageJsonLint = new NpmPackageJsonLint({
  patterns: ['./package.json'],
});

// add custom rule
npmPackageJsonLint.rules.rules['peer-deps-star'] = path.resolve(__dirname, './rule-peer-deps-star.js');

const results = npmPackageJsonLint.lint();
console.log(results); // need to handle results and throw errors manually here

Custom rule should be provided in commonjs format. Inspired by https://github.com/tclindner/npm-package-json-lint/issues/596#issue-1164980440

pawelgur avatar Apr 22 '24 16:04 pawelgur