@@ -55,6 +55,30 @@ const pJKeys: PJKeys[] = [{
5555 validate : runner => typeof runner === 'string' && runner . length ,
5656 msg : 'must specify a test runner' ,
5757 example : 'mocha-coderoad' ,
58+ } , {
59+ name : 'repository' ,
60+ optional : true ,
61+ validate : ( repo : string | { type : string , url : string } ) => {
62+ return typeof repo === 'string' && repo . length ||
63+ typeof repo === 'object' && repo . hasOwnProperty ( 'type' )
64+ && typeof repo . type === 'string' &&
65+ repo . hasOwnProperty ( 'url' ) && typeof repo . url === 'string' ;
66+ } ,
67+ msg : 'should have a valid repository' ,
68+ example : 'https://github.com/shmck/coderoad-tutorial-name' ,
69+ } , {
70+ name : 'bugs' ,
71+ optional : true ,
72+ validate : ( bugs : { url : string } ) => typeof bugs === 'object' &&
73+ bugs . hasOwnProperty ( 'url' ) && typeof bugs . url === 'string' ,
74+ msg : 'should have a link to where to post bugs' ,
75+ example : '"bugs": { "url": "https://github.com/shmck/coderoad-tutorial-name" }'
76+ } , {
77+ name : 'license' ,
78+ optional : true ,
79+ validate : license => typeof license === 'string' && license . length ,
80+ msg : 'should have a valid license (ex: MIT, ISC, etc.)' ,
81+ example : 'MIT' ,
5882 } ] ;
5983
6084interface PJErrors {
@@ -66,17 +90,31 @@ interface PJKeys extends PJErrors {
6690 name : string ;
6791 validate : ( content : string ) => boolean ;
6892 config ?: boolean ;
93+ optional ?: boolean ;
94+ }
95+
96+ interface ValidatePjOutput {
97+ errors : PJErrors [ ] ;
98+ warnings : PJErrors [ ] ;
6999}
70100
71- export default function validatePackageJson ( pj : PackageJson ) : PJErrors [ ] {
101+ export default function validatePackageJson ( pj : PackageJson ) : ValidatePjOutput {
72102 const errors = [ ] ;
103+ const warnings = [ ] ;
73104 pJKeys . forEach ( key => {
74105 // key on pj or pj.config
75106 const target = pj . config ? pj . config : pj ;
76107 // key doesn't exist or key is invalid
77108 if ( ! target . hasOwnProperty ( key . name ) || key . validate ( target [ key . name ] ) ) {
78- errors . push ( { msg : key . msg , example : key . example } ) ;
109+ if ( ! key . optional ) {
110+ errors . push ( { msg : key . msg , example : key . example } ) ;
111+ } else {
112+ warnings . push ( { msg : key . msg , example : key . example } ) ;
113+ }
79114 }
80115 } ) ;
81- return errors ;
116+ return {
117+ errors,
118+ warnings,
119+ } ;
82120}
0 commit comments