npm-package-json-lint
npm-package-json-lint copied to clipboard
[perhaps a Feature Request] Custom rule
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 😄
Hey @askarby! We don't have that capability yet. I like the suggestion.
What rule are you thinking about adding?
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)
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.
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.
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.
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"
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.
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
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.
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