Give me a string and I\textquotesingle{}ll tell you if it\textquotesingle{}s a valid npm package license string.


\begin{DoxyCode}{0}
\DoxyCodeLine{var valid = require('validate-\/npm-\/package-\/license'); }

\end{DoxyCode}


SPDX license identifiers are valid license strings\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var assert = require('assert');}
\DoxyCodeLine{var validSPDXExpression = \{}
\DoxyCodeLine{  validForNewPackages: true,}
\DoxyCodeLine{  validForOldPackages: true,}
\DoxyCodeLine{  spdx: true}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{assert.deepEqual(valid('MIT'), validSPDXExpression);}
\DoxyCodeLine{assert.deepEqual(valid('BSD-\/2-\/Clause'), validSPDXExpression);}
\DoxyCodeLine{assert.deepEqual(valid('Apache-\/2.0'), validSPDXExpression);}
\DoxyCodeLine{assert.deepEqual(valid('ISC'), validSPDXExpression); }

\end{DoxyCode}
 The function will return a warning and suggestion for nearly-\/correct license identifiers\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{assert.deepEqual(}
\DoxyCodeLine{  valid('Apache 2.0'),}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    validForOldPackages: false,}
\DoxyCodeLine{    validForNewPackages: false,}
\DoxyCodeLine{    warnings: [}
\DoxyCodeLine{      'license should be ' +}
\DoxyCodeLine{      'a valid SPDX license expression (without "{}LicenseRef"{}), ' +}
\DoxyCodeLine{      '"{}UNLICENSED"{}, or ' +}
\DoxyCodeLine{      '"{}SEE LICENSE IN <filename>"{}',}
\DoxyCodeLine{      'license is similar to the valid expression "{}Apache-\/2.0"{}'}
\DoxyCodeLine{    ]}
\DoxyCodeLine{  \}}
\DoxyCodeLine{); }

\end{DoxyCode}


SPDX expressions are valid, too ...


\begin{DoxyCode}{0}
\DoxyCodeLine{// Simple SPDX license expression for dual licensing}
\DoxyCodeLine{assert.deepEqual(}
\DoxyCodeLine{  valid('(GPL-\/3.0-\/only OR BSD-\/2-\/Clause)'),}
\DoxyCodeLine{  validSPDXExpression}
\DoxyCodeLine{); }

\end{DoxyCode}


... except if they contain {\ttfamily License\+Ref}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var warningAboutLicenseRef = \{}
\DoxyCodeLine{  validForOldPackages: false,}
\DoxyCodeLine{  validForNewPackages: false,}
\DoxyCodeLine{  spdx: true,}
\DoxyCodeLine{  warnings: [}
\DoxyCodeLine{    'license should be ' +}
\DoxyCodeLine{    'a valid SPDX license expression (without "{}LicenseRef"{}), ' +}
\DoxyCodeLine{    '"{}UNLICENSED"{}, or ' +}
\DoxyCodeLine{    '"{}SEE LICENSE IN <filename>"{}',}
\DoxyCodeLine{  ]}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{assert.deepEqual(}
\DoxyCodeLine{  valid('LicenseRef-\/Made-\/Up'),}
\DoxyCodeLine{  warningAboutLicenseRef}
\DoxyCodeLine{);}
\DoxyCodeLine{}
\DoxyCodeLine{assert.deepEqual(}
\DoxyCodeLine{  valid('(MIT OR LicenseRef-\/Made-\/Up)'),}
\DoxyCodeLine{  warningAboutLicenseRef}
\DoxyCodeLine{); }

\end{DoxyCode}


If you can\textquotesingle{}t describe your licensing terms with standardized SPDX identifiers, put the terms in a file in the package and point users there\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{assert.deepEqual(}
\DoxyCodeLine{  valid('SEE LICENSE IN LICENSE.txt'),}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    validForNewPackages: true,}
\DoxyCodeLine{    validForOldPackages: true,}
\DoxyCodeLine{    inFile: 'LICENSE.txt'}
\DoxyCodeLine{  \}}
\DoxyCodeLine{);}
\DoxyCodeLine{}
\DoxyCodeLine{assert.deepEqual(}
\DoxyCodeLine{  valid('SEE LICENSE IN license.md'),}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    validForNewPackages: true,}
\DoxyCodeLine{    validForOldPackages: true,}
\DoxyCodeLine{    inFile: 'license.md'}
\DoxyCodeLine{  \}}
\DoxyCodeLine{); }

\end{DoxyCode}


If there aren\textquotesingle{}t any licensing terms, use {\ttfamily UNLICENSED}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var unlicensed = \{}
\DoxyCodeLine{  validForNewPackages: true,}
\DoxyCodeLine{  validForOldPackages: true,}
\DoxyCodeLine{  unlicensed: true}
\DoxyCodeLine{\};}
\DoxyCodeLine{assert.deepEqual(valid('UNLICENSED'), unlicensed);}
\DoxyCodeLine{assert.deepEqual(valid('UNLICENCED'), unlicensed); }

\end{DoxyCode}
 