Following are the types involved in the return type of our granular validate* functions.
Result
The Result type, is the common top-level return type for all of our granular validate* functions.
It provides rich information about the nature of the issues encountered as part of validating.
For complex objects like exports, its tree structure can give information on which parts of the structure have issues.
errorMessages: string[]
The full collection of error messages (including errors from child element).
This consists of the Issue.message for all of the items in this Result's issues, as well as the messages of all descendent issues.
issues: Issue[]
Collection of issues for this object (property or array element).
childResults: ChildResult[]
Collection of result objects for child elements (either properties or array elements), if this property is an object or array.
ChildResult extends Result
Result object for a child (either a property in an object or an element of an array).
index: number
The index of this property in relation to its parent's collection (properties or array elements).
Issue
message: string
The message with information about this issue.
Functions
validate(data, options?)
This function validates an entire package.json and returns a list of errors, if
any violations are found.
Parameters
data packageData object or a JSON-stringified version of the package data.
options is an object with the following:
interface Options { recommendations?: boolean; // show recommendations warnings?: boolean; // show warnings}
Starting in v1.4, you can opt-in to the new Result return type for validate.
This has been the return type for all of the more granular validate* functions for a while now, and gives more specific information about which parts of the package.json are failing validation.
See Result Types for more details about this construct.
If you'd like to opt-in to the new behavior before it becomes the default experience, pass true into the second parameter.
import { validateBugs } from 'package-json-validator';const packageData = { bugs: { email: 'example@npmjs.com', url: 'https://github.com/npm/example/issues', },};const result = validateBugs(packageData.bugs);
validateBundleDependencies(value)
This function validates the value of the bundleDependencies property of a package.json.
It takes the value, and validates it against the following criteria.
allowNamedRegistries: Relaxes validation on dependency versions to allow for named registries, a feature supported by pnpm since 11.1.0 (Default: false)
import { validateDependencies } from 'package-json-validator';const packageData = { dependencies: { '@my/package': '^1.2.3', '@company/package': 'work:svgo@^1.0.0', // Would have failed validation },};const result = validateDependencies(packageData.dependencies, { allowNamedRegistries: true,});
validateDescription(value)
This function validates the value of the description property of a package.json, checking that the value is a non-empty string.
import { validateDescription } from 'package-json-validator';const packageData = { description: 'The Fragile',};const result = validateDescription(packageData.description);
validateDirectories(value)
This function validates the value of the directories property of a package.json.
It takes the value, and validates it against the following criteria.
import { validateHomepage } from 'package-json-validator';const packageData = { homepage: 'The Fragile',};const result = validateDescription(packageData.homepage);
validateKeywords(value)
This function validates the value of the keywords property of a package.json.
It takes the value, and validates it against the following criteria.
the property is an array
all items in the array should be non-empty strings
import { validateKeywords } from 'package-json-validator';const packageData = { keywords: ['eslint', 'package.json'],};const result = validateKeywords(packageData.keywords);
validateLibc(value)
This function validates the value of the libc property of a package.json.
It takes the value, and validates that it's either a string or an Array of strings.
import { validateLibc } from 'package-json-validator';const packageData = { libc: 'glibc',};const result = validateLibc(packageData.man);
validateLicense(value)
This function validates the value of the license property of a package.json.
It takes the value, and validates it using validate-npm-package-license, which is the same package that npm uses.
import { validateMan } from 'package-json-validator';const packageData = { man: ['./man/foo.1', './man/bar.1'],};const result = validateMan(packageData.man);
validateName(value)
This function validates the value of the name property of a package.json.
It takes the value, and validates it using validate-npm-package-name, which is the same package that npm uses.
import { validateOs } from 'package-json-validator';const packageData = { os: ['linux', 'win32'],};const result = validateOs(packageData.os);
validatePackageManager(value)
This function validates the value of the packageManager property of a package.json.
It takes the value, and checks that it's a string with a valid package manager and version.
The package manager portion should be npm, pnpm, yarn, bun, or deno, while the version portion should be a valid semver version.
import { validatePackageManager } from 'package-json-validator';const packageData = { packageManager: 'pnpm@10.3.0',};const result = validatePackageManager(packageData.packageManager);
validatePeerDependenciesMeta(value)
This function validates the value of the peerDependenciesMeta property of a package.json.
It takes the value, and checks that it is an object whose keys are valid package names and whose values are objects with only an optional property.
The optional property must be a boolean.
import { validatePrivate } from 'package-json-validator';const packageData = { private: true,};const result = validatePrivate(packageData.private);
validatePublishConfig(value)
This function validates the value of the publishConfig property of a package.json.
It takes the value, and validates it against the following criteria.
the property is an object
if any of the following properties are present, they should conform to the type defined by the package manager
access
bin
cpu
directory
exports
main
provenance
tag
[!NOTE]
These properties are a (non-exhaustive) combination of those supported by npm, pnpm, and yarn.
import { validateType } from 'package-json-validator';const packageData = { type: 'module',};const result = validateType(packageData.type);
validateVersion(value)
This function validates the value of the version property of a package.json.
It takes the value, and validates it using semver, which is the same package that npm uses.
We never want to remove things, when we're building them!
But the reality is that libraries evolve and deprecations are a fact of life.
Following are the different timeframes that we've defined as it relates to deprecating APIs in this project.
RFC Timeframe (6 weeks)
When some aspect of our API is going to be deprecated (and eventually removed), it must initially go through an RFC phase.
Whoever's motivating the removal of the api, should create an RFC issue explaining the proposal and inviting feedback from the community.
That RFC should remain active for at least 6 weeks.
The RFC text should make clear what the target date is for closing the RFC.
Once the RFC period is over, if the removal is still moving forward, the API(s) should be officially deprecated.
Removal Timeframe (6 months)
Once an API has been marked as deprecated, it will remain intact for at least 6 months.
After 6 months from the date of deprecation, the API is subject to removal.