manifest.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. 'use strict';
  2. const Assert = require('@hapi/hoek/lib/assert');
  3. const Clone = require('@hapi/hoek/lib/clone');
  4. const Common = require('./common');
  5. const Messages = require('./messages');
  6. const Ref = require('./ref');
  7. const Template = require('./template');
  8. let Schemas;
  9. const internals = {};
  10. exports.describe = function (schema) {
  11. const def = schema._definition;
  12. // Type
  13. const desc = {
  14. type: schema.type,
  15. flags: {},
  16. rules: []
  17. };
  18. // Flags
  19. for (const flag in schema._flags) {
  20. if (flag[0] !== '_') {
  21. desc.flags[flag] = internals.describe(schema._flags[flag]);
  22. }
  23. }
  24. if (!Object.keys(desc.flags).length) {
  25. delete desc.flags;
  26. }
  27. // Preferences
  28. if (schema._preferences) {
  29. desc.preferences = Clone(schema._preferences, { shallow: ['messages'] });
  30. delete desc.preferences[Common.symbols.prefs];
  31. if (desc.preferences.messages) {
  32. desc.preferences.messages = Messages.decompile(desc.preferences.messages);
  33. }
  34. }
  35. // Allow / Invalid
  36. if (schema._valids) {
  37. desc.allow = schema._valids.describe();
  38. }
  39. if (schema._invalids) {
  40. desc.invalid = schema._invalids.describe();
  41. }
  42. // Rules
  43. for (const rule of schema._rules) {
  44. const ruleDef = def.rules[rule.name];
  45. if (ruleDef.manifest === false) { // Defaults to true
  46. continue;
  47. }
  48. const item = { name: rule.name };
  49. for (const custom in def.modifiers) {
  50. if (rule[custom] !== undefined) {
  51. item[custom] = internals.describe(rule[custom]);
  52. }
  53. }
  54. if (rule.args) {
  55. item.args = {};
  56. for (const key in rule.args) {
  57. const arg = rule.args[key];
  58. if (key === 'options' &&
  59. !Object.keys(arg).length) {
  60. continue;
  61. }
  62. item.args[key] = internals.describe(arg, { assign: key });
  63. }
  64. if (!Object.keys(item.args).length) {
  65. delete item.args;
  66. }
  67. }
  68. desc.rules.push(item);
  69. }
  70. if (!desc.rules.length) {
  71. delete desc.rules;
  72. }
  73. // Terms (must be last to verify no name conflicts)
  74. for (const term in schema.$_terms) {
  75. if (term[0] === '_') {
  76. continue;
  77. }
  78. Assert(!desc[term], 'Cannot describe schema due to internal name conflict with', term);
  79. const items = schema.$_terms[term];
  80. if (!items) {
  81. continue;
  82. }
  83. if (items instanceof Map) {
  84. if (items.size) {
  85. desc[term] = [...items.entries()];
  86. }
  87. continue;
  88. }
  89. if (Common.isValues(items)) {
  90. desc[term] = items.describe();
  91. continue;
  92. }
  93. Assert(def.terms[term], 'Term', term, 'missing configuration');
  94. const manifest = def.terms[term].manifest;
  95. const mapped = typeof manifest === 'object';
  96. if (!items.length &&
  97. !mapped) {
  98. continue;
  99. }
  100. const normalized = [];
  101. for (const item of items) {
  102. normalized.push(internals.describe(item));
  103. }
  104. // Mapped
  105. if (mapped) {
  106. const { from, to } = manifest.mapped;
  107. desc[term] = {};
  108. for (const item of normalized) {
  109. desc[term][item[to]] = item[from];
  110. }
  111. continue;
  112. }
  113. // Single
  114. if (manifest === 'single') {
  115. Assert(normalized.length === 1, 'Term', term, 'contains more than one item');
  116. desc[term] = normalized[0];
  117. continue;
  118. }
  119. // Array
  120. desc[term] = normalized;
  121. }
  122. internals.validate(schema.$_root, desc);
  123. return desc;
  124. };
  125. internals.describe = function (item, options = {}) {
  126. if (Array.isArray(item)) {
  127. return item.map(internals.describe);
  128. }
  129. if (item === Common.symbols.deepDefault) {
  130. return { special: 'deep' };
  131. }
  132. if (typeof item !== 'object' ||
  133. item === null) {
  134. return item;
  135. }
  136. if (options.assign === 'options') {
  137. return Clone(item);
  138. }
  139. if (Buffer && Buffer.isBuffer(item)) { // $lab:coverage:ignore$
  140. return { buffer: item.toString('binary') };
  141. }
  142. if (item instanceof Date) {
  143. return item.toISOString();
  144. }
  145. if (item instanceof Error) {
  146. return item;
  147. }
  148. if (item instanceof RegExp) {
  149. if (options.assign === 'regex') {
  150. return item.toString();
  151. }
  152. return { regex: item.toString() };
  153. }
  154. if (item[Common.symbols.literal]) {
  155. return { function: item.literal };
  156. }
  157. if (typeof item.describe === 'function') {
  158. if (options.assign === 'ref') {
  159. return item.describe().ref;
  160. }
  161. return item.describe();
  162. }
  163. const normalized = {};
  164. for (const key in item) {
  165. const value = item[key];
  166. if (value === undefined) {
  167. continue;
  168. }
  169. normalized[key] = internals.describe(value, { assign: key });
  170. }
  171. return normalized;
  172. };
  173. exports.build = function (joi, desc) {
  174. const builder = new internals.Builder(joi);
  175. return builder.parse(desc);
  176. };
  177. internals.Builder = class {
  178. constructor(joi) {
  179. this.joi = joi;
  180. }
  181. parse(desc) {
  182. internals.validate(this.joi, desc);
  183. // Type
  184. let schema = this.joi[desc.type]()._bare();
  185. const def = schema._definition;
  186. // Flags
  187. if (desc.flags) {
  188. for (const flag in desc.flags) {
  189. const setter = def.flags[flag] && def.flags[flag].setter || flag;
  190. Assert(typeof schema[setter] === 'function', 'Invalid flag', flag, 'for type', desc.type);
  191. schema = schema[setter](this.build(desc.flags[flag]));
  192. }
  193. }
  194. // Preferences
  195. if (desc.preferences) {
  196. schema = schema.preferences(this.build(desc.preferences));
  197. }
  198. // Allow / Invalid
  199. if (desc.allow) {
  200. schema = schema.allow(...this.build(desc.allow));
  201. }
  202. if (desc.invalid) {
  203. schema = schema.invalid(...this.build(desc.invalid));
  204. }
  205. // Rules
  206. if (desc.rules) {
  207. for (const rule of desc.rules) {
  208. Assert(typeof schema[rule.name] === 'function', 'Invalid rule', rule.name, 'for type', desc.type);
  209. const args = [];
  210. if (rule.args) {
  211. const built = {};
  212. for (const key in rule.args) {
  213. built[key] = this.build(rule.args[key], { assign: key });
  214. }
  215. const keys = Object.keys(built);
  216. const definition = def.rules[rule.name].args;
  217. if (definition) {
  218. Assert(keys.length <= definition.length, 'Invalid number of arguments for', desc.type, rule.name, '(expected up to', definition.length, ', found', keys.length, ')');
  219. for (const { name } of definition) {
  220. args.push(built[name]);
  221. }
  222. }
  223. else {
  224. Assert(keys.length === 1, 'Invalid number of arguments for', desc.type, rule.name, '(expected up to 1, found', keys.length, ')');
  225. args.push(built[keys[0]]);
  226. }
  227. }
  228. // Apply
  229. schema = schema[rule.name](...args);
  230. // Ruleset
  231. const options = {};
  232. for (const custom in def.modifiers) {
  233. if (rule[custom] !== undefined) {
  234. options[custom] = this.build(rule[custom]);
  235. }
  236. }
  237. if (Object.keys(options).length) {
  238. schema = schema.rule(options);
  239. }
  240. }
  241. }
  242. // Terms
  243. const terms = {};
  244. for (const key in desc) {
  245. if (['allow', 'flags', 'invalid', 'whens', 'preferences', 'rules', 'type'].includes(key)) {
  246. continue;
  247. }
  248. Assert(def.terms[key], 'Term', key, 'missing configuration');
  249. const manifest = def.terms[key].manifest;
  250. if (manifest === 'schema') {
  251. terms[key] = desc[key].map((item) => this.parse(item));
  252. continue;
  253. }
  254. if (manifest === 'values') {
  255. terms[key] = desc[key].map((item) => this.build(item));
  256. continue;
  257. }
  258. if (manifest === 'single') {
  259. terms[key] = this.build(desc[key]);
  260. continue;
  261. }
  262. if (typeof manifest === 'object') {
  263. terms[key] = {};
  264. for (const name in desc[key]) {
  265. const value = desc[key][name];
  266. terms[key][name] = this.parse(value);
  267. }
  268. continue;
  269. }
  270. terms[key] = this.build(desc[key]);
  271. }
  272. if (desc.whens) {
  273. terms.whens = desc.whens.map((when) => this.build(when));
  274. }
  275. schema = def.manifest.build(schema, terms);
  276. schema.$_temp.ruleset = false;
  277. return schema;
  278. }
  279. build(desc, options = {}) {
  280. if (desc === null) {
  281. return null;
  282. }
  283. if (Array.isArray(desc)) {
  284. return desc.map((item) => this.build(item));
  285. }
  286. if (desc instanceof Error) {
  287. return desc;
  288. }
  289. if (options.assign === 'options') {
  290. return Clone(desc);
  291. }
  292. if (options.assign === 'regex') {
  293. return internals.regex(desc);
  294. }
  295. if (options.assign === 'ref') {
  296. return Ref.build(desc);
  297. }
  298. if (typeof desc !== 'object') {
  299. return desc;
  300. }
  301. if (Object.keys(desc).length === 1) {
  302. if (desc.buffer) {
  303. Assert(Buffer, 'Buffers are not supported');
  304. return Buffer && Buffer.from(desc.buffer, 'binary'); // $lab:coverage:ignore$
  305. }
  306. if (desc.function) {
  307. return { [Common.symbols.literal]: true, literal: desc.function };
  308. }
  309. if (desc.override) {
  310. return Common.symbols.override;
  311. }
  312. if (desc.ref) {
  313. return Ref.build(desc.ref);
  314. }
  315. if (desc.regex) {
  316. return internals.regex(desc.regex);
  317. }
  318. if (desc.special) {
  319. Assert(['deep'].includes(desc.special), 'Unknown special value', desc.special);
  320. return Common.symbols.deepDefault;
  321. }
  322. if (desc.value) {
  323. return Clone(desc.value);
  324. }
  325. }
  326. if (desc.type) {
  327. return this.parse(desc);
  328. }
  329. if (desc.template) {
  330. return Template.build(desc);
  331. }
  332. const normalized = {};
  333. for (const key in desc) {
  334. normalized[key] = this.build(desc[key], { assign: key });
  335. }
  336. return normalized;
  337. }
  338. };
  339. internals.regex = function (string) {
  340. const end = string.lastIndexOf('/');
  341. const exp = string.slice(1, end);
  342. const flags = string.slice(end + 1);
  343. return new RegExp(exp, flags);
  344. };
  345. internals.validate = function (joi, desc) {
  346. Schemas = Schemas || require('./schemas');
  347. joi.assert(desc, Schemas.description);
  348. };