schema.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /**
  2. * Schema for validating JSDoc doclets.
  3. * @module jsdoc/schema
  4. * @see <https://trac.tools.ietf.org/html/draft-wright-json-schema-validation-01>
  5. */
  6. // JSON schema types
  7. const ARRAY = 'array';
  8. const BOOLEAN = 'boolean';
  9. const NULL = 'null';
  10. const NUMBER = 'number';
  11. const OBJECT = 'object';
  12. const STRING = 'string';
  13. const BOOLEAN_OPTIONAL = [BOOLEAN, NULL];
  14. const STRING_OPTIONAL = [STRING, NULL];
  15. const EVENT_REGEXP = 'event:[\\S]+';
  16. const PACKAGE_REGEXP = 'package:[\\S]+';
  17. const STRING_SCHEMA = {
  18. type: STRING
  19. };
  20. // information about the code associated with a doclet
  21. const META_SCHEMA = exports.META_SCHEMA = {
  22. type: OBJECT,
  23. additionalProperties: false,
  24. properties: {
  25. code: {
  26. type: OBJECT,
  27. additionalProperties: false,
  28. properties: {
  29. funcscope: {
  30. type: STRING
  31. },
  32. id: {
  33. type: STRING
  34. },
  35. name: {},
  36. node: {
  37. type: OBJECT
  38. },
  39. paramnames: {
  40. type: ARRAY,
  41. uniqueItems: true,
  42. items: {
  43. type: STRING
  44. }
  45. },
  46. type: {
  47. type: STRING
  48. },
  49. value: {}
  50. }
  51. },
  52. columnno: {
  53. title: 'The column number of the code associated with this doclet.',
  54. type: NUMBER
  55. },
  56. filename: {
  57. title: 'The name of the file that contains the code associated with this doclet.',
  58. type: STRING
  59. },
  60. lineno: {
  61. title: 'The line number of the code associated with this doclet.',
  62. type: NUMBER
  63. },
  64. path: {
  65. title: 'The path in which the code associated with this doclet is located.',
  66. type: STRING
  67. },
  68. range: {
  69. title: 'The positions of the first and last characters of the code associated with ' +
  70. 'this doclet.',
  71. type: ARRAY,
  72. minItems: 2,
  73. maxItems: 2,
  74. items: {
  75. type: NUMBER
  76. }
  77. },
  78. vars: {
  79. type: OBJECT
  80. }
  81. }
  82. };
  83. // type property containing type names
  84. const TYPE_PROPERTY_SCHEMA = exports.TYPE_PROPERTY_SCHEMA = {
  85. type: OBJECT,
  86. additionalProperties: false,
  87. properties: {
  88. names: {
  89. type: ARRAY,
  90. minItems: 1,
  91. items: {
  92. type: STRING
  93. }
  94. },
  95. // type parser output
  96. parsedType: {
  97. type: OBJECT,
  98. additionalProperties: true
  99. }
  100. }
  101. };
  102. // enumeration properties
  103. const ENUM_PROPERTY_SCHEMA = exports.ENUM_PROPERTY_SCHEMA = {
  104. type: OBJECT,
  105. additionalProperties: false,
  106. properties: {
  107. comment: {
  108. type: STRING
  109. },
  110. defaultvalue: {},
  111. description: {
  112. type: STRING_OPTIONAL
  113. },
  114. kind: {
  115. type: STRING,
  116. enum: ['member']
  117. },
  118. longname: {
  119. type: STRING
  120. },
  121. memberof: {
  122. type: STRING
  123. },
  124. meta: META_SCHEMA,
  125. name: {
  126. type: STRING
  127. },
  128. // is this member nullable? (derived from the type expression)
  129. nullable: {
  130. type: BOOLEAN_OPTIONAL
  131. },
  132. // is this member optional? (derived from the type expression)
  133. optional: {
  134. type: BOOLEAN_OPTIONAL
  135. },
  136. scope: {
  137. type: STRING,
  138. enum: ['static']
  139. },
  140. type: TYPE_PROPERTY_SCHEMA,
  141. // can this member be provided more than once? (derived from the type expression)
  142. variable: {
  143. type: BOOLEAN_OPTIONAL
  144. }
  145. }
  146. };
  147. // function parameter, or object property defined with @property tag
  148. const PARAM_SCHEMA = exports.PARAM_SCHEMA = {
  149. type: OBJECT,
  150. additionalProperties: false,
  151. properties: {
  152. // what is the default value for this parameter?
  153. defaultvalue: {},
  154. // a description of the parameter
  155. description: {
  156. type: STRING_OPTIONAL
  157. },
  158. // what name does this parameter have within the function?
  159. name: {
  160. type: STRING
  161. },
  162. // can the value for this parameter be null?
  163. nullable: {
  164. type: BOOLEAN_OPTIONAL
  165. },
  166. // is a value for this parameter optional?
  167. optional: {
  168. type: BOOLEAN_OPTIONAL
  169. },
  170. // what are the types of value expected for this parameter?
  171. type: TYPE_PROPERTY_SCHEMA,
  172. // can this parameter be repeated?
  173. variable: {
  174. type: BOOLEAN_OPTIONAL
  175. }
  176. }
  177. };
  178. const DOCLET_SCHEMA = exports.DOCLET_SCHEMA = {
  179. type: OBJECT,
  180. additionalProperties: false,
  181. properties: {
  182. // what access privileges are allowed
  183. access: {
  184. type: STRING,
  185. enum: [
  186. 'package',
  187. 'private',
  188. 'protected',
  189. 'public'
  190. ]
  191. },
  192. alias: {
  193. type: STRING
  194. },
  195. async: {
  196. type: BOOLEAN
  197. },
  198. augments: {
  199. type: ARRAY,
  200. uniqueItems: true,
  201. items: {
  202. type: STRING
  203. }
  204. },
  205. author: {
  206. type: ARRAY,
  207. items: {
  208. type: STRING
  209. }
  210. },
  211. borrowed: {
  212. type: ARRAY,
  213. uniqueItems: true,
  214. items: {
  215. type: OBJECT,
  216. additionalProperties: false,
  217. properties: {
  218. // name of the target
  219. as: {
  220. type: STRING
  221. },
  222. // name of the source
  223. from: {
  224. type: STRING
  225. }
  226. }
  227. }
  228. },
  229. // a description of the class that this constructor belongs to
  230. classdesc: {
  231. type: STRING
  232. },
  233. comment: {
  234. type: STRING
  235. },
  236. copyright: {
  237. type: STRING
  238. },
  239. defaultvalue: {},
  240. defaultvaluetype: {
  241. type: STRING,
  242. enum: [OBJECT, ARRAY]
  243. },
  244. // is usage of this symbol deprecated?
  245. deprecated: {
  246. type: [STRING, BOOLEAN]
  247. },
  248. // a description
  249. description: {
  250. type: STRING_OPTIONAL
  251. },
  252. // something else to consider
  253. examples: {
  254. type: ARRAY,
  255. items: {
  256. type: STRING
  257. }
  258. },
  259. exceptions: {
  260. type: ARRAY,
  261. items: PARAM_SCHEMA
  262. },
  263. // the path to another constructor
  264. extends: {
  265. type: ARRAY,
  266. uniqueItems: true,
  267. items: {
  268. type: STRING
  269. }
  270. },
  271. // the path to another doc object
  272. fires: {
  273. type: ARRAY,
  274. uniqueItems: true,
  275. items: {
  276. type: STRING,
  277. pattern: EVENT_REGEXP
  278. }
  279. },
  280. forceMemberof: {
  281. type: BOOLEAN_OPTIONAL
  282. },
  283. generator: {
  284. type: BOOLEAN
  285. },
  286. hideconstructor: {
  287. type: BOOLEAN
  288. },
  289. ignore: {
  290. type: BOOLEAN
  291. },
  292. implementations: {
  293. type: ARRAY,
  294. items: {
  295. type: STRING
  296. }
  297. },
  298. implements: {
  299. type: ARRAY,
  300. items: {
  301. type: STRING
  302. }
  303. },
  304. inheritdoc: {
  305. type: STRING
  306. },
  307. inherited: {
  308. type: BOOLEAN
  309. },
  310. inherits: {
  311. type: STRING,
  312. dependency: {
  313. inherited: true
  314. }
  315. },
  316. isEnum: {
  317. type: BOOLEAN
  318. },
  319. // what kind of symbol is this?
  320. kind: {
  321. type: STRING,
  322. enum: [
  323. 'class',
  324. 'constant',
  325. 'event',
  326. 'external',
  327. 'file',
  328. 'function',
  329. 'interface',
  330. 'member',
  331. 'mixin',
  332. 'module',
  333. 'namespace',
  334. 'package',
  335. 'param',
  336. 'typedef'
  337. ]
  338. },
  339. license: {
  340. type: STRING
  341. },
  342. listens: {
  343. type: ARRAY,
  344. uniqueItems: true,
  345. items: {
  346. type: STRING,
  347. pattern: EVENT_REGEXP
  348. }
  349. },
  350. longname: {
  351. type: STRING
  352. },
  353. // probably a leading substring of the path
  354. memberof: {
  355. type: STRING
  356. },
  357. // information about this doc
  358. meta: META_SCHEMA,
  359. // was this doclet mixed in?
  360. mixed: {
  361. type: BOOLEAN
  362. },
  363. mixes: {
  364. type: ARRAY,
  365. uniqueItems: true,
  366. items: {
  367. type: STRING
  368. }
  369. },
  370. modifies: {
  371. type: ARRAY,
  372. uniqueItems: true,
  373. items: PARAM_SCHEMA
  374. },
  375. // probably a trailing substring of the path
  376. name: {
  377. type: STRING
  378. },
  379. // is this member nullable? (derived from the type expression)
  380. nullable: {
  381. type: BOOLEAN_OPTIONAL
  382. },
  383. // is this member optional? (derived from the type expression)
  384. optional: {
  385. type: BOOLEAN_OPTIONAL
  386. },
  387. // does this member explicitly override the parent?
  388. override: {
  389. type: BOOLEAN
  390. },
  391. overrides: {
  392. type: STRING
  393. },
  394. // are there function parameters associated with this doc?
  395. params: {
  396. type: ARRAY,
  397. uniqueItems: true,
  398. items: PARAM_SCHEMA
  399. },
  400. preserveName: {
  401. type: BOOLEAN
  402. },
  403. properties: {
  404. type: ARRAY,
  405. uniqueItems: true,
  406. minItems: 1,
  407. items: {
  408. anyOf: [ENUM_PROPERTY_SCHEMA, PARAM_SCHEMA]
  409. }
  410. },
  411. readonly: {
  412. type: BOOLEAN
  413. },
  414. // the symbol being documented requires another symbol
  415. requires: {
  416. type: ARRAY,
  417. uniqueItems: true,
  418. minItems: 1,
  419. items: {
  420. type: STRING
  421. }
  422. },
  423. returns: {
  424. type: ARRAY,
  425. minItems: 1,
  426. items: PARAM_SCHEMA
  427. },
  428. // what sort of parent scope does this symbol have?
  429. scope: {
  430. type: STRING,
  431. enum: [
  432. 'global',
  433. 'inner',
  434. 'instance',
  435. 'static'
  436. ]
  437. },
  438. // something else to consider
  439. see: {
  440. type: ARRAY,
  441. minItems: 1,
  442. items: {
  443. type: STRING
  444. }
  445. },
  446. // at what previous version was this doc added?
  447. since: {
  448. type: STRING
  449. },
  450. summary: {
  451. type: STRING
  452. },
  453. // arbitrary tags associated with this doc
  454. tags: {
  455. type: ARRAY,
  456. minItems: 1,
  457. items: {
  458. type: OBJECT,
  459. additionalProperties: false,
  460. properties: {
  461. originalTitle: {
  462. type: STRING
  463. },
  464. text: {
  465. type: STRING
  466. },
  467. title: {
  468. type: STRING
  469. },
  470. value: {
  471. oneOf: [STRING_SCHEMA, PARAM_SCHEMA]
  472. }
  473. }
  474. }
  475. },
  476. 'this': {
  477. type: STRING
  478. },
  479. todo: {
  480. type: ARRAY,
  481. minItems: 1,
  482. items: {
  483. type: STRING
  484. }
  485. },
  486. // extended tutorials
  487. tutorials: {
  488. type: ARRAY,
  489. minItems: 1,
  490. items: {
  491. type: STRING
  492. }
  493. },
  494. // what type is the value that this doc is associated with, like `number`
  495. type: TYPE_PROPERTY_SCHEMA,
  496. undocumented: {
  497. type: BOOLEAN
  498. },
  499. // can this member be provided more than once? (derived from the type expression)
  500. variable: {
  501. type: BOOLEAN_OPTIONAL
  502. },
  503. variation: {
  504. type: STRING
  505. },
  506. // what is the version of this doc
  507. version: {
  508. type: STRING
  509. },
  510. // is a member left to be implemented during inheritance?
  511. virtual: {
  512. type: BOOLEAN
  513. },
  514. yields: {
  515. type: ARRAY,
  516. minItems: 1,
  517. items: PARAM_SCHEMA
  518. }
  519. }
  520. };
  521. const CONTACT_INFO_SCHEMA = exports.CONTACT_INFO_SCHEMA = {
  522. type: OBJECT,
  523. additionalProperties: false,
  524. properties: {
  525. email: {
  526. type: STRING
  527. },
  528. name: {
  529. type: STRING
  530. },
  531. url: {
  532. type: STRING,
  533. format: 'uri'
  534. }
  535. }
  536. };
  537. const BUGS_SCHEMA = exports.BUGS_SCHEMA = {
  538. type: OBJECT,
  539. additionalProperties: false,
  540. properties: {
  541. email: {
  542. type: STRING
  543. },
  544. url: {
  545. type: STRING,
  546. format: 'uri'
  547. }
  548. }
  549. };
  550. const PACKAGE_SCHEMA = exports.PACKAGE_SCHEMA = {
  551. type: OBJECT,
  552. additionalProperties: false,
  553. properties: {
  554. author: {
  555. anyOf: [STRING_SCHEMA, CONTACT_INFO_SCHEMA]
  556. },
  557. bugs: {
  558. anyOf: [STRING_SCHEMA, BUGS_SCHEMA]
  559. },
  560. contributors: {
  561. type: ARRAY,
  562. minItems: 0,
  563. items: {
  564. anyOf: [STRING_SCHEMA, CONTACT_INFO_SCHEMA]
  565. }
  566. },
  567. dependencies: {
  568. type: OBJECT
  569. },
  570. description: {
  571. type: STRING
  572. },
  573. devDependencies: {
  574. type: OBJECT
  575. },
  576. engines: {
  577. type: OBJECT
  578. },
  579. files: {
  580. type: ARRAY,
  581. uniqueItems: true,
  582. minItems: 0,
  583. items: {
  584. type: STRING
  585. }
  586. },
  587. homepage: {
  588. type: STRING,
  589. format: 'uri'
  590. },
  591. keywords: {
  592. type: ARRAY,
  593. minItems: 0,
  594. items: {
  595. type: STRING
  596. }
  597. },
  598. kind: {
  599. type: STRING,
  600. enum: ['package']
  601. },
  602. licenses: {
  603. type: ARRAY,
  604. minItems: 1,
  605. items: {
  606. type: OBJECT,
  607. additionalProperties: false,
  608. properties: {
  609. type: {
  610. type: STRING
  611. },
  612. url: {
  613. type: STRING,
  614. format: 'uri'
  615. }
  616. }
  617. }
  618. },
  619. longname: {
  620. type: STRING,
  621. pattern: PACKAGE_REGEXP
  622. },
  623. main: {
  624. type: STRING
  625. },
  626. name: {
  627. type: STRING
  628. },
  629. repository: {
  630. type: OBJECT,
  631. additionalProperties: false,
  632. properties: {
  633. type: {
  634. type: STRING
  635. },
  636. // we don't use `format: 'uri'` here because repo URLs are atypical
  637. url: {
  638. type: STRING
  639. }
  640. }
  641. },
  642. version: {
  643. type: STRING
  644. }
  645. }
  646. };
  647. exports.DOCLETS_SCHEMA = {
  648. type: ARRAY,
  649. items: {
  650. anyOf: [DOCLET_SCHEMA, PACKAGE_SCHEMA]
  651. }
  652. };