sendproof-core 0.1.0.alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +55 -0
  4. data/lib/sendproof/assets.rb +70 -0
  5. data/lib/sendproof/attribution.rb +98 -0
  6. data/lib/sendproof/capture.rb +66 -0
  7. data/lib/sendproof/clock.rb +54 -0
  8. data/lib/sendproof/configuration.rb +241 -0
  9. data/lib/sendproof/data_store.rb +34 -0
  10. data/lib/sendproof/dynamodb/store.rb +183 -0
  11. data/lib/sendproof/dynamodb.rb +17 -0
  12. data/lib/sendproof/elasticsearch/store.rb +24 -0
  13. data/lib/sendproof/elasticsearch.rb +17 -0
  14. data/lib/sendproof/firestore/interceptor.rb +38 -0
  15. data/lib/sendproof/firestore/store.rb +150 -0
  16. data/lib/sendproof/firestore.rb +99 -0
  17. data/lib/sendproof/handler.rb +23 -0
  18. data/lib/sendproof/identity.rb +37 -0
  19. data/lib/sendproof/limits.rb +113 -0
  20. data/lib/sendproof/middleware.rb +68 -0
  21. data/lib/sendproof/mongo/engine.rb +692 -0
  22. data/lib/sendproof/mongo/guarded.rb +100 -0
  23. data/lib/sendproof/mongo/plain.rb +45 -0
  24. data/lib/sendproof/mongo/store.rb +54 -0
  25. data/lib/sendproof/mongo/subscriber.rb +21 -0
  26. data/lib/sendproof/mongo.rb +41 -0
  27. data/lib/sendproof/mongoid/store.rb +58 -0
  28. data/lib/sendproof/mongoid.rb +65 -0
  29. data/lib/sendproof/observed_request.rb +108 -0
  30. data/lib/sendproof/opensearch/store.rb +21 -0
  31. data/lib/sendproof/opensearch.rb +17 -0
  32. data/lib/sendproof/operation_matcher.rb +28 -0
  33. data/lib/sendproof/pool_hooks.rb +80 -0
  34. data/lib/sendproof/preview_request.rb +44 -0
  35. data/lib/sendproof/preview_transport.rb +331 -0
  36. data/lib/sendproof/protocol.rb +81 -0
  37. data/lib/sendproof/provider.rb +83 -0
  38. data/lib/sendproof/recorder.rb +298 -0
  39. data/lib/sendproof/redaction.rb +98 -0
  40. data/lib/sendproof/redis/commands.rb +76 -0
  41. data/lib/sendproof/redis/store.rb +205 -0
  42. data/lib/sendproof/redis.rb +18 -0
  43. data/lib/sendproof/search/middleware.rb +39 -0
  44. data/lib/sendproof/search/store.rb +213 -0
  45. data/lib/sendproof/search.rb +82 -0
  46. data/lib/sendproof/stores/classify.rb +346 -0
  47. data/lib/sendproof/stores/guard.rb +127 -0
  48. data/lib/sendproof/stores/tables.rb +1328 -0
  49. data/lib/sendproof/stores.rb +12 -0
  50. data/lib/sendproof/trace_store.rb +110 -0
  51. data/lib/sendproof/transaction_control.rb +187 -0
  52. data/lib/sendproof/ui/action-bar.js +91 -0
  53. data/lib/sendproof/ui/api.js +239 -0
  54. data/lib/sendproof/ui/app.css +848 -0
  55. data/lib/sendproof/ui/app.js +742 -0
  56. data/lib/sendproof/ui/autofill.js +137 -0
  57. data/lib/sendproof/ui/code-copy.js +71 -0
  58. data/lib/sendproof/ui/code-picker.js +149 -0
  59. data/lib/sendproof/ui/commands.js +197 -0
  60. data/lib/sendproof/ui/components.js +77 -0
  61. data/lib/sendproof/ui/context-bar.js +82 -0
  62. data/lib/sendproof/ui/copy.js +92 -0
  63. data/lib/sendproof/ui/coverage-popover.js +35 -0
  64. data/lib/sendproof/ui/crumb-fit.js +80 -0
  65. data/lib/sendproof/ui/deprecation.js +164 -0
  66. data/lib/sendproof/ui/display-settings.js +175 -0
  67. data/lib/sendproof/ui/doc-diff.js +209 -0
  68. data/lib/sendproof/ui/docs-view.js +347 -0
  69. data/lib/sendproof/ui/favicon.png +0 -0
  70. data/lib/sendproof/ui/favicon.svg +1 -0
  71. data/lib/sendproof/ui/field-help.js +52 -0
  72. data/lib/sendproof/ui/field-modules.js +23 -0
  73. data/lib/sendproof/ui/fields/controls.js +179 -0
  74. data/lib/sendproof/ui/fields/dom.js +36 -0
  75. data/lib/sendproof/ui/fields/infer.js +71 -0
  76. data/lib/sendproof/ui/fields/lookup-source.js +63 -0
  77. data/lib/sendproof/ui/fields/lookup.js +285 -0
  78. data/lib/sendproof/ui/fields/render.js +496 -0
  79. data/lib/sendproof/ui/fields/select.js +175 -0
  80. data/lib/sendproof/ui/fields/validate.js +363 -0
  81. data/lib/sendproof/ui/fields/validation-formats.js +164 -0
  82. data/lib/sendproof/ui/fields/validation-references.js +68 -0
  83. data/lib/sendproof/ui/fields/validation-values.js +137 -0
  84. data/lib/sendproof/ui/generate-menu.js +44 -0
  85. data/lib/sendproof/ui/generate.js +184 -0
  86. data/lib/sendproof/ui/header-editor.js +777 -0
  87. data/lib/sendproof/ui/headers.js +188 -0
  88. data/lib/sendproof/ui/highlight.js +257 -0
  89. data/lib/sendproof/ui/history-popover.js +24 -0
  90. data/lib/sendproof/ui/i18n.js +253 -0
  91. data/lib/sendproof/ui/icons.js +87 -0
  92. data/lib/sendproof/ui/index.html +70 -0
  93. data/lib/sendproof/ui/issues.js +187 -0
  94. data/lib/sendproof/ui/json-view.js +166 -0
  95. data/lib/sendproof/ui/lang-icons.js +62 -0
  96. data/lib/sendproof/ui/layout.js +262 -0
  97. data/lib/sendproof/ui/listbox.js +334 -0
  98. data/lib/sendproof/ui/locales/ar-capture.js +366 -0
  99. data/lib/sendproof/ui/locales/ar-stores.js +116 -0
  100. data/lib/sendproof/ui/locales/ar.js +561 -0
  101. data/lib/sendproof/ui/locales/cs-capture.js +442 -0
  102. data/lib/sendproof/ui/locales/cs-stores.js +116 -0
  103. data/lib/sendproof/ui/locales/cs.js +689 -0
  104. data/lib/sendproof/ui/locales/da-capture.js +347 -0
  105. data/lib/sendproof/ui/locales/da-stores.js +116 -0
  106. data/lib/sendproof/ui/locales/da.js +522 -0
  107. data/lib/sendproof/ui/locales/de-capture.js +347 -0
  108. data/lib/sendproof/ui/locales/de-stores.js +116 -0
  109. data/lib/sendproof/ui/locales/de.js +519 -0
  110. data/lib/sendproof/ui/locales/el-capture.js +347 -0
  111. data/lib/sendproof/ui/locales/el-stores.js +116 -0
  112. data/lib/sendproof/ui/locales/el.js +520 -0
  113. data/lib/sendproof/ui/locales/en-capture.js +349 -0
  114. data/lib/sendproof/ui/locales/en-stores.js +95 -0
  115. data/lib/sendproof/ui/locales/en.js +522 -0
  116. data/lib/sendproof/ui/locales/es-capture.js +353 -0
  117. data/lib/sendproof/ui/locales/es-stores.js +116 -0
  118. data/lib/sendproof/ui/locales/es.js +525 -0
  119. data/lib/sendproof/ui/locales/fa-capture.js +348 -0
  120. data/lib/sendproof/ui/locales/fa-stores.js +116 -0
  121. data/lib/sendproof/ui/locales/fa.js +521 -0
  122. data/lib/sendproof/ui/locales/fi-capture.js +347 -0
  123. data/lib/sendproof/ui/locales/fi-stores.js +116 -0
  124. data/lib/sendproof/ui/locales/fi.js +523 -0
  125. data/lib/sendproof/ui/locales/fr-capture.js +353 -0
  126. data/lib/sendproof/ui/locales/fr-stores.js +116 -0
  127. data/lib/sendproof/ui/locales/fr.js +525 -0
  128. data/lib/sendproof/ui/locales/he-capture.js +358 -0
  129. data/lib/sendproof/ui/locales/he-stores.js +116 -0
  130. data/lib/sendproof/ui/locales/he.js +529 -0
  131. data/lib/sendproof/ui/locales/hi-capture.js +348 -0
  132. data/lib/sendproof/ui/locales/hi-stores.js +116 -0
  133. data/lib/sendproof/ui/locales/hi.js +523 -0
  134. data/lib/sendproof/ui/locales/id-capture.js +347 -0
  135. data/lib/sendproof/ui/locales/id-stores.js +116 -0
  136. data/lib/sendproof/ui/locales/id.js +521 -0
  137. data/lib/sendproof/ui/locales/it-capture.js +361 -0
  138. data/lib/sendproof/ui/locales/it-stores.js +116 -0
  139. data/lib/sendproof/ui/locales/it.js +523 -0
  140. data/lib/sendproof/ui/locales/ja-capture.js +346 -0
  141. data/lib/sendproof/ui/locales/ja-stores.js +116 -0
  142. data/lib/sendproof/ui/locales/ja.js +521 -0
  143. data/lib/sendproof/ui/locales/ko-capture.js +346 -0
  144. data/lib/sendproof/ui/locales/ko-stores.js +116 -0
  145. data/lib/sendproof/ui/locales/ko.js +522 -0
  146. data/lib/sendproof/ui/locales/nb-capture.js +349 -0
  147. data/lib/sendproof/ui/locales/nb-stores.js +116 -0
  148. data/lib/sendproof/ui/locales/nb.js +521 -0
  149. data/lib/sendproof/ui/locales/nl-capture.js +355 -0
  150. data/lib/sendproof/ui/locales/nl-stores.js +116 -0
  151. data/lib/sendproof/ui/locales/nl.js +520 -0
  152. data/lib/sendproof/ui/locales/pl-capture.js +432 -0
  153. data/lib/sendproof/ui/locales/pl-stores.js +116 -0
  154. data/lib/sendproof/ui/locales/pl.js +528 -0
  155. data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
  156. data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
  157. data/lib/sendproof/ui/locales/pt-BR.js +523 -0
  158. data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
  159. data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
  160. data/lib/sendproof/ui/locales/pt-PT.js +523 -0
  161. data/lib/sendproof/ui/locales/ru-capture.js +437 -0
  162. data/lib/sendproof/ui/locales/ru-stores.js +116 -0
  163. data/lib/sendproof/ui/locales/ru.js +624 -0
  164. data/lib/sendproof/ui/locales/sv-capture.js +349 -0
  165. data/lib/sendproof/ui/locales/sv-stores.js +116 -0
  166. data/lib/sendproof/ui/locales/sv.js +518 -0
  167. data/lib/sendproof/ui/locales/th-capture.js +347 -0
  168. data/lib/sendproof/ui/locales/th-stores.js +116 -0
  169. data/lib/sendproof/ui/locales/th.js +521 -0
  170. data/lib/sendproof/ui/locales/tr-capture.js +348 -0
  171. data/lib/sendproof/ui/locales/tr-stores.js +116 -0
  172. data/lib/sendproof/ui/locales/tr.js +521 -0
  173. data/lib/sendproof/ui/locales/uk-capture.js +442 -0
  174. data/lib/sendproof/ui/locales/uk-stores.js +116 -0
  175. data/lib/sendproof/ui/locales/uk.js +630 -0
  176. data/lib/sendproof/ui/locales/vi-capture.js +346 -0
  177. data/lib/sendproof/ui/locales/vi-stores.js +116 -0
  178. data/lib/sendproof/ui/locales/vi.js +523 -0
  179. data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
  180. data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
  181. data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
  182. data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
  183. data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
  184. data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
  185. data/lib/sendproof/ui/manifest.json +866 -0
  186. data/lib/sendproof/ui/markdown.js +39 -0
  187. data/lib/sendproof/ui/model-view.js +182 -0
  188. data/lib/sendproof/ui/navigation.js +233 -0
  189. data/lib/sendproof/ui/operation-header.js +76 -0
  190. data/lib/sendproof/ui/operations.js +7 -0
  191. data/lib/sendproof/ui/overview-view.js +63 -0
  192. data/lib/sendproof/ui/personas.js +59 -0
  193. data/lib/sendproof/ui/popover.js +71 -0
  194. data/lib/sendproof/ui/preview-false.js +108 -0
  195. data/lib/sendproof/ui/request-editor.js +496 -0
  196. data/lib/sendproof/ui/response-metadata.js +354 -0
  197. data/lib/sendproof/ui/response-viewer.js +618 -0
  198. data/lib/sendproof/ui/result-blocked.js +155 -0
  199. data/lib/sendproof/ui/result-capture.js +25 -0
  200. data/lib/sendproof/ui/result-database.js +185 -0
  201. data/lib/sendproof/ui/result-effects.js +176 -0
  202. data/lib/sendproof/ui/result-headers.js +106 -0
  203. data/lib/sendproof/ui/result-logs.js +408 -0
  204. data/lib/sendproof/ui/result-states.js +213 -0
  205. data/lib/sendproof/ui/results.js +371 -0
  206. data/lib/sendproof/ui/router.js +118 -0
  207. data/lib/sendproof/ui/schema-model.js +336 -0
  208. data/lib/sendproof/ui/schema-tree.js +344 -0
  209. data/lib/sendproof/ui/schema.js +397 -0
  210. data/lib/sendproof/ui/security.js +31 -0
  211. data/lib/sendproof/ui/seeded-menu.js +69 -0
  212. data/lib/sendproof/ui/settings.js +93 -0
  213. data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
  214. data/lib/sendproof/ui/shortcuts.js +190 -0
  215. data/lib/sendproof/ui/sidebar.js +289 -0
  216. data/lib/sendproof/ui/signin.js +259 -0
  217. data/lib/sendproof/ui/snippets.js +530 -0
  218. data/lib/sendproof/ui/store-marks.js +47 -0
  219. data/lib/sendproof/ui/stores.js +131 -0
  220. data/lib/sendproof/ui/styles.css +971 -0
  221. data/lib/sendproof/ui/toggletip.js +53 -0
  222. data/lib/sendproof/ui/uuid.js +10 -0
  223. data/lib/sendproof/ui/view.js +122 -0
  224. data/lib/sendproof/ui/views.css +653 -0
  225. data/lib/sendproof/version.rb +9 -0
  226. data/lib/sendproof/workbench.rb +102 -0
  227. data/lib/sendproof-core.rb +3 -0
  228. data/lib/sendproof.rb +121 -0
  229. metadata +292 -0
@@ -0,0 +1,175 @@
1
+ // Enum controls (mocks 76–80, 82, 112, m18, m19): a radio group for short enums and a
2
+ // field-styled select for everything else. Both replace the native <select> and the segmented
3
+ // enum of v4.
4
+ //
5
+ // radio group up to 4 values of up to 12 characters. Separate options with a radio dot; the
6
+ // chosen one has a filled dot, an accent border and a tint (three cues, none of
7
+ // them colour alone). WAI-ARIA radio group: one tab stop, arrows move and choose,
8
+ // Delete or Backspace clears an optional field.
9
+ // select looks like an input: the value fills the control and the trigger is a joined
10
+ // segment behind a divider. Its popover is the shared listbox (ui/listbox.js):
11
+ // "Any" first for optional fields, a check on the chosen value, each value's
12
+ // description (x-enum-descriptions), search past 8 values and type-ahead.
13
+ import { element, svg } from './dom.js';
14
+ import { closeListbox, openListbox } from '../listbox.js';
15
+ import { t } from '../i18n.js';
16
+
17
+ /** A radio group fits short enums; anything longer is a select. The one place the rule lives. */
18
+ export const useRadio = values => values.length > 0 && values.length <= 4 && values.every(value => value !== null && typeof value !== 'object' && String(value).length <= 12);
19
+
20
+ /** The descriptions an enum documents for its values: x-enum-descriptions, or oneOf of const + description. */
21
+ export function enumDescriptions(schema, resolve = value => value) {
22
+ const own = schema?.['x-enum-descriptions'] ?? schema?.['x-enumDescriptions'];
23
+ if (own && typeof own === 'object') return own;
24
+ const variants = (schema?.oneOf || schema?.anyOf || []).map(resolve);
25
+ if (variants.length && variants.every(variant => variant && Object.hasOwn(variant, 'const'))) {
26
+ return Object.fromEntries(variants.filter(variant => variant.description).map(variant => [String(variant.const), variant.description]));
27
+ }
28
+ return {};
29
+ }
30
+
31
+ const isDelete = event => event.key === 'Delete' || event.key === 'Backspace';
32
+
33
+ /**
34
+ * A radio group. `options` are { value, label, description }; `onChange(value)` gets undefined
35
+ * when an optional group is cleared.
36
+ */
37
+ export function radioGroup({ id, options, value, optional, onChange, labelledBy, required, disabled }) {
38
+ const group = element('div', 'rg');
39
+ group.id = id;
40
+ group.setAttribute('role', 'radiogroup');
41
+ if (labelledBy) group.setAttribute('aria-labelledby', labelledBy);
42
+ if (required) group.setAttribute('aria-required', 'true');
43
+ if (disabled) { group.setAttribute('aria-disabled', 'true'); group.classList.add('is-disabled'); }
44
+ const chosen = options.findIndex(option => Object.is(option.value, value));
45
+ const radios = options.map((option, index) => {
46
+ const radio = element('span', 'rg-opt');
47
+ radio.setAttribute('role', 'radio');
48
+ radio.setAttribute('aria-checked', String(index === chosen));
49
+ radio.tabIndex = disabled ? -1 : index === (chosen < 0 ? 0 : chosen) ? 0 : -1;
50
+ radio.lang = 'en';
51
+ radio.dataset.value = String(option.value);
52
+ if (option.description) radio.title = option.description;
53
+ const dot = element('span', 'rg-dot');
54
+ dot.setAttribute('aria-hidden', 'true');
55
+ const text = element('span', '', option.label ?? String(option.value));
56
+ text.dataset.label = text.textContent;
57
+ radio.append(dot, text);
58
+ radio.addEventListener('click', () => { if (!disabled) pick(index); });
59
+ return radio;
60
+ });
61
+ group.append(...radios);
62
+ function pick(index, focus = true) {
63
+ radios.forEach((radio, i) => {
64
+ radio.setAttribute('aria-checked', String(i === index));
65
+ radio.tabIndex = i === (index < 0 ? 0 : index) ? 0 : -1;
66
+ });
67
+ if (focus && radios[index]) radios[index].focus?.();
68
+ onChange(index < 0 ? undefined : options[index].value);
69
+ }
70
+ group.addEventListener('keydown', event => {
71
+ if (disabled) return;
72
+ const at = radios.indexOf(document.activeElement);
73
+ if (at < 0) return;
74
+ const rtl = getComputedStyle(group).direction === 'rtl';
75
+ const forward = { ArrowDown: 1, ArrowUp: -1, ArrowRight: rtl ? -1 : 1, ArrowLeft: rtl ? 1 : -1 }[event.key];
76
+ if (forward) pick((at + forward + radios.length) % radios.length);
77
+ else if (event.key === ' ' || event.key === 'Enter') pick(at);
78
+ else if (event.key === 'Home') pick(0);
79
+ else if (event.key === 'End') pick(radios.length - 1);
80
+ else if (isDelete(event) && optional && radios.some(radio => radio.getAttribute('aria-checked') === 'true')) { pick(-1, false); radios[at].focus(); }
81
+ else return;
82
+ event.preventDefault();
83
+ });
84
+ /** Focuses the checked option, or the first. */
85
+ group.focusControl = () => (radios.find(radio => radio.tabIndex === 0) || radios[0])?.focus();
86
+ return group;
87
+ }
88
+
89
+ /**
90
+ * A field-styled select. Returns the `.control` element (role="combobox").
91
+ * @param {object} o
92
+ * @param {Array<{value: *, label?: string, description?: string}>} o.options
93
+ * @param {*} o.value
94
+ * @param {boolean} o.optional adds "Any", which clears the field
95
+ * @param {string} o.labelledBy id of the field's label
96
+ * @param {{title: string, sub?: string}} o.sheet the phone sheet's title
97
+ * @param {boolean} [o.segment] a joined segment of another control (the currency of an amount)
98
+ * @param {string} [o.unsetHint] the description of "Any"
99
+ */
100
+ export function selectControl({ id, options, value, optional, onChange, labelledBy, label, required, placeholder, mono = false, sheet, segment = false, unsetHint, disabled }) {
101
+ const control = element(segment ? 'span' : 'div', segment ? 'part sel-seg' : 'control sel');
102
+ control.id = id;
103
+ control.tabIndex = disabled ? -1 : 0;
104
+ control.setAttribute('role', 'combobox');
105
+ control.setAttribute('aria-haspopup', 'listbox');
106
+ control.setAttribute('aria-expanded', 'false');
107
+ if (labelledBy) control.setAttribute('aria-labelledby', labelledBy);
108
+ if (label) control.setAttribute('aria-label', label);
109
+ if (required) control.setAttribute('aria-required', 'true');
110
+ if (disabled) { control.setAttribute('aria-disabled', 'true'); control.classList.add('is-disabled'); }
111
+ const shown = element('span', 'sel-value');
112
+ shown.lang = 'en';
113
+ const slot = element('span', 'slot');
114
+ slot.setAttribute('aria-hidden', 'true');
115
+ const trigger = element('span', segment ? 'sel-chev' : 'sel-trigger');
116
+ trigger.setAttribute('aria-hidden', 'true');
117
+ trigger.append(svg('chevron-down'));
118
+ control.append(shown, ...(segment ? [] : [slot]), trigger);
119
+ let current = value;
120
+
121
+ function paint() {
122
+ const option = options.find(item => Object.is(item.value, current));
123
+ shown.classList.toggle('placeholder', !option);
124
+ shown.classList.toggle('mono-val', !!option && mono);
125
+ shown.textContent = option ? option.label ?? String(option.value) : placeholder ?? (optional ? t('field.any') : t('field.choose'));
126
+ }
127
+ paint();
128
+
129
+ function set(next) {
130
+ current = next;
131
+ paint();
132
+ onChange(next);
133
+ }
134
+
135
+ function items() {
136
+ const list = options.map(option => ({ value: option.value, label: option.label ?? String(option.value), description: option.description, mono, valueLang: 'en', descriptionLang: 'en' }));
137
+ return optional ? [{ unset: true, value: undefined, label: t('field.any'), description: unsetHint ?? t('field.anyHint') }, ...list] : list;
138
+ }
139
+
140
+ function show(extra = {}) {
141
+ if (disabled) return undefined;
142
+ return openListbox(control, {
143
+ label: label || document.getElementById(labelledBy)?.textContent || '',
144
+ items: items(), value: current, search: 'auto', sheet, ...extra,
145
+ onPick: item => set(item.unset ? undefined : item.value),
146
+ });
147
+ }
148
+ control.addEventListener('click', event => {
149
+ if (control.getAttribute('aria-expanded') === 'true') closeListbox(true);
150
+ else { event.preventDefault(); show(); }
151
+ });
152
+ control.addEventListener('keydown', event => {
153
+ if (control.getAttribute('aria-expanded') === 'true' || disabled) return;
154
+ if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(event.key) || (event.altKey && event.key === 'ArrowDown')) {
155
+ event.preventDefault();
156
+ const active = event.key === 'ArrowUp' && current === undefined ? options.at(-1)?.value : undefined;
157
+ show(active !== undefined ? { active } : {});
158
+ } else if (isDelete(event) && optional && current !== undefined) {
159
+ event.preventDefault();
160
+ set(undefined);
161
+ } else if (event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
162
+ // Typing on a closed select opens it and starts type-ahead with that character.
163
+ event.preventDefault();
164
+ const search = show()?.pop.querySelector('.lb-search input');
165
+ if (search) { search.value = event.key; search.dispatchEvent(new Event('input')); }
166
+ else control.dispatchEvent(new KeyboardEvent('keydown', { key: event.key, bubbles: true }));
167
+ }
168
+ });
169
+ control.setValue = next => { current = next; paint(); };
170
+ /** Chooses a value as if it had been picked from the list. */
171
+ control.choose = next => set(next);
172
+ control.openOptions = () => show();
173
+ control.focusControl = () => control.focus();
174
+ return control;
175
+ }
@@ -0,0 +1,363 @@
1
+ import { formatDate, formatList, formatNumber, t } from '../i18n.js';
2
+ import { title } from './dom.js';
3
+ import { createReferenceResolver } from './validation-references.js';
4
+ import {
5
+ compilePattern, createValueKey, displayValue, isNumber, isObject, typeError,
6
+ validateNumber, validateRange, validateString,
7
+ } from './validation-values.js';
8
+
9
+ const emptySet = new Set();
10
+ const withArticle = noun => /^[aeiou]/.test(noun) ? 'an' : 'a';
11
+ const hasValue = (object, key) => Object.hasOwn(object, key) && object[key] !== undefined;
12
+
13
+ /** Validate a request or response against its original schema and applicator scopes. */
14
+ export function validateSchema(context, schema, value, required) {
15
+ const findReference = createReferenceResolver(context.document, schema);
16
+ const resolveReference = ref => {
17
+ const target = findReference(ref);
18
+ if (target === undefined && typeof ref === 'string') context.unresolvedReferences?.add(ref);
19
+ return target;
20
+ };
21
+ const excludedAnnotation = context.response ? 'writeOnly' : 'readOnly';
22
+ const openApiVersion = context.document?.openapi;
23
+ const legacyReferences = typeof openApiVersion === 'string' && openApiVersion.startsWith('3.0.');
24
+ const allowNullable = legacyReferences || !(openApiVersion || schema?.$schema);
25
+ const valueKey = createValueKey();
26
+ const active = new Map();
27
+ const shapes = new WeakMap();
28
+ // Mounted controls treat a blank required control as missing. JSON strings have their supplied length.
29
+ const isBlank = value => value === undefined || (value === '' && typeof context.set === 'function');
30
+
31
+ function isExcluded(raw, seen = new Set()) {
32
+ if (!raw || typeof raw !== 'object' || seen.has(raw)) return false;
33
+ seen.add(raw);
34
+ if (raw.$ref && isExcluded(resolveReference(raw.$ref), seen)) return true;
35
+ if (raw.$ref && legacyReferences) return false;
36
+ return raw[excludedAnnotation] === true || raw.allOf?.some(child => isExcluded(child, seen));
37
+ }
38
+
39
+ function schemaShape(raw) {
40
+ if (!raw || typeof raw !== 'object') return { required: emptySet, properties: new Map(), excluded: emptySet };
41
+ if (shapes.has(raw)) return shapes.get(raw);
42
+ const shape = { required: new Set(), properties: new Map(), excluded: new Set() };
43
+ const seen = new Set();
44
+
45
+ function collect(part) {
46
+ if (!part || typeof part !== 'object' || seen.has(part)) return;
47
+ seen.add(part);
48
+ if (part.$ref) {
49
+ collect(resolveReference(part.$ref));
50
+ if (legacyReferences) return;
51
+ }
52
+ for (const child of part.allOf ?? []) collect(child);
53
+ for (const name of part.required ?? []) shape.required.add(name);
54
+ for (const [name, child] of Object.entries(part.properties ?? {})) {
55
+ shape.properties.set(name, child);
56
+ if (isExcluded(child)) shape.excluded.add(name);
57
+ }
58
+ }
59
+
60
+ collect(raw);
61
+ shapes.set(raw, shape);
62
+ return shape;
63
+ }
64
+
65
+ /** The type, format, enum, lookup and title of a field, read through $ref and allOf. */
66
+ function describeField(raw) {
67
+ const field = {};
68
+ const seen = new Set();
69
+ (function collect(part) {
70
+ if (!part || typeof part !== 'object' || seen.has(part)) return;
71
+ seen.add(part);
72
+ if (part.$ref) collect(resolveReference(part.$ref));
73
+ for (const child of part.allOf ?? []) collect(child);
74
+ for (const key of ['title', 'type', 'format', 'enum', 'x-lookup', 'minItems']) {
75
+ if (part[key] !== undefined) field[key] = part[key];
76
+ }
77
+ })(raw);
78
+ return field;
79
+ }
80
+
81
+ /** Says what to enter for a missing required value, by the kind of field it is. */
82
+ function requiredMessage(raw, path) {
83
+ const field = describeField(raw);
84
+ const last = path.at(-1);
85
+ const label = typeof last === 'string' && last ? last : field.title;
86
+ const noun = label ? title(label).toLowerCase().replace(/ id$/, '') : t('valid.missing.valueNoun');
87
+ const type = [].concat(field.type ?? []).find(item => item !== 'null');
88
+ if (field['x-lookup']) return t('valid.missing.lookup', { article: withArticle(noun), name: noun });
89
+ if (Array.isArray(field.enum) && field.enum.length) {
90
+ const choices = field.enum.slice(0, 5).map(option => typeof option === 'string' ? option : displayValue(option));
91
+ return field.enum.length > 5
92
+ ? t('valid.missing.enumMore', { list: formatList(choices, 'unit') })
93
+ : t('valid.missing.enum', { list: formatList(choices, 'disjunction') });
94
+ }
95
+ if (type === 'boolean') return t('valid.missing.boolean');
96
+ if (type === 'array') return t('valid.missing.array', { n: Math.max(1, Number(field.minItems) || 1) });
97
+ if (type === 'integer') return t('valid.missing.integer', { example: formatNumber(1250) });
98
+ if (type === 'number') {
99
+ return t('valid.missing.number', { example: formatNumber(1250, { minimumFractionDigits: 2, maximumFractionDigits: 2 }) });
100
+ }
101
+ if (field.format === 'date') return t('valid.missing.date', { example: formatDate('2026-11-23') });
102
+ if (field.format === 'email' || field.format === 'idn-email') return t('valid.missing.email');
103
+ return t('valid.missing.text', { article: withArticle(noun), name: noun });
104
+ }
105
+
106
+ function missing(raw, path, errors, seen = new Set()) {
107
+ const { required, properties, excluded } = schemaShape(raw);
108
+ const children = [...required].filter(name => !excluded.has(name));
109
+ if (!children.length || seen.has(raw)) {
110
+ errors.push({ path: path.join('.'), message: requiredMessage(raw, path) });
111
+ return;
112
+ }
113
+ seen.add(raw);
114
+ for (const name of children) missing(properties.get(name), [...path, name], errors, seen);
115
+ seen.delete(raw);
116
+ }
117
+
118
+ function visit(raw, current, path, inheritedExcluded = emptySet) {
119
+ const errors = [];
120
+ const evaluated = new Set();
121
+ const add = (message, location = path) => errors.push({ path: location.join('.'), message });
122
+ const result = { errors, evaluated };
123
+ if (raw === false) add(t('valid.notAllowed'));
124
+ if (!raw || typeof raw !== 'object') return result;
125
+
126
+ // Recursive references can reapply a schema to the same instance without descending.
127
+ let instances = active.get(raw);
128
+ if (instances?.has(current)) return result;
129
+ if (!instances) active.set(raw, instances = new Set());
130
+ instances.add(current);
131
+
132
+ function merge(child) {
133
+ if (child.errors.length) errors.push(...child.errors);
134
+ else for (const key of child.evaluated) evaluated.add(key);
135
+ }
136
+
137
+ try {
138
+ const excluded = isObject(current) && schemaShape(raw).excluded.size
139
+ ? new Set([...inheritedExcluded, ...schemaShape(raw).excluded])
140
+ : inheritedExcluded;
141
+ const apply = part => visit(part, current, path, excluded);
142
+
143
+ if (raw.$ref) {
144
+ merge(apply(resolveReference(raw.$ref)));
145
+ if (legacyReferences) return result;
146
+ }
147
+
148
+ const mismatch = typeError(raw, current, allowNullable);
149
+ if (mismatch) {
150
+ add(mismatch);
151
+ return result;
152
+ }
153
+ if (raw.const !== undefined && valueKey(raw.const) !== valueKey(current)) {
154
+ add(t('valid.const', { value: displayValue(raw.const) }));
155
+ }
156
+ if (Array.isArray(raw.enum)) {
157
+ const key = valueKey(current);
158
+ if (!raw.enum.some(option => valueKey(option) === key)) {
159
+ const choices = raw.enum.slice(0, 5).map(displayValue).join(', ');
160
+ add(!choices ? t('valid.enum') : t(raw.enum.length > 5 ? 'valid.enumMore' : 'valid.enumChoices', { choices }));
161
+ }
162
+ }
163
+
164
+ for (const part of raw.allOf ?? []) merge(apply(part));
165
+ for (const keyword of ['anyOf', 'oneOf']) {
166
+ if (!Array.isArray(raw[keyword])) continue;
167
+ const choices = raw[keyword].map(apply);
168
+ const matches = choices.filter(choice => !choice.errors.length);
169
+ if (!matches.length) {
170
+ const names = raw[keyword].map((option, index) => variantName(option, index));
171
+ const preferred = preferredVariant(raw, keyword, current, path);
172
+ const fewestErrors = Math.min(...choices.map(choice => choice.errors.length));
173
+ const closest = choices.filter(choice => choice.errors.length === fewestErrors);
174
+ if (choices[preferred]) merge(choices[preferred]);
175
+ else if (closest.length === 1) merge(closest[0]);
176
+ add(t(keyword === 'oneOf' ? 'valid.oneOf' : 'valid.anyOf', { names: formatList(names, 'unit') }));
177
+ } else if (keyword === 'oneOf' && matches.length > 1) {
178
+ const names = raw[keyword].flatMap((option, index) => choices[index].errors.length ? [] : [variantName(option, index)]);
179
+ add(t('valid.oneOfMany', { names: formatList(names, 'unit') }));
180
+ } else {
181
+ for (const match of matches) merge(match);
182
+ }
183
+ }
184
+ if (raw.not !== undefined && !apply(raw.not).errors.length) {
185
+ const name = schemaName(raw.not);
186
+ add(name ? t('valid.notNamed', { name }) : t('valid.notSchema'));
187
+ }
188
+ if (raw.if !== undefined) {
189
+ const condition = apply(raw.if);
190
+ if (!condition.errors.length) {
191
+ merge(condition);
192
+ if (raw.then !== undefined) merge(apply(raw.then));
193
+ } else if (raw.else !== undefined) {
194
+ merge(apply(raw.else));
195
+ }
196
+ }
197
+
198
+ if (isObject(current)) {
199
+ validateObject(raw, current, path, excluded, evaluated, errors, add, merge, apply);
200
+ } else if (Array.isArray(current)) {
201
+ validateArray(raw, current, path, evaluated, errors, add);
202
+ } else if (typeof current === 'string') {
203
+ validateString(raw, current, add);
204
+ } else if (isNumber(current)) {
205
+ validateNumber(raw, current, add);
206
+ }
207
+ return result;
208
+ } finally {
209
+ instances.delete(current);
210
+ if (!instances.size) active.delete(raw);
211
+ }
212
+ }
213
+
214
+ function schemaName(option) {
215
+ return option?.title ?? resolveReference(option?.$ref)?.title;
216
+ }
217
+
218
+ function variantName(option, index) {
219
+ return schemaName(option) ?? option?.$ref?.split('/').at(-1) ?? option?.type?.toString() ?? t('valid.option', { n: index + 1 });
220
+ }
221
+
222
+ function preferredVariant(raw, keyword, current, path) {
223
+ const renderedKeyword = raw.oneOf?.length ? 'oneOf' : 'anyOf';
224
+ const selected = keyword === renderedKeyword ? context.variants?.get(JSON.stringify(path)) : undefined;
225
+ if (selected !== undefined) return selected;
226
+ const discriminator = raw.discriminator;
227
+ if (!discriminator?.propertyName || !isObject(current)) return;
228
+ const value = current[discriminator.propertyName];
229
+ if (value === undefined) return;
230
+ return raw[keyword].findIndex(option => {
231
+ const mapped = discriminator.mapping && Object.hasOwn(discriminator.mapping, value) && discriminator.mapping[value];
232
+ if (mapped) return option?.$ref === mapped || option?.$ref === '#/components/schemas/' + mapped;
233
+ const property = schemaShape(option).properties.get(discriminator.propertyName);
234
+ return property?.const === value || property?.enum?.includes(value) || option?.$ref?.split('/').at(-1) === value;
235
+ });
236
+ }
237
+
238
+ function validateObject(raw, current, path, excluded, evaluated, errors, add, merge, apply) {
239
+ const keys = Object.keys(current).filter(key => hasValue(current, key));
240
+ const properties = raw.properties ?? {};
241
+ const patterns = Object.entries(raw.patternProperties ?? {}).map(([pattern, child]) => [compilePattern(pattern), child]);
242
+ validateRange(keys.length, raw.minProperties, raw.maxProperties, 'property', add);
243
+ for (const name of raw.required ?? []) {
244
+ if (!excluded.has(name) && (!hasValue(current, name) || isBlank(current[name]))) {
245
+ missing(Object.hasOwn(properties, name) ? properties[name] : undefined, [...path, name], errors);
246
+ }
247
+ }
248
+
249
+ for (const key of keys) {
250
+ const childPath = [...path, key];
251
+ const named = Object.hasOwn(properties, key);
252
+ const matching = patterns.filter(([pattern]) => pattern?.test(key));
253
+ if (named || matching.length || raw.additionalProperties !== undefined) evaluated.add(key);
254
+ if (!excluded.has(key)) {
255
+ if (named && !(raw.required?.includes(key) && isBlank(current[key]))) {
256
+ errors.push(...visit(properties[key], current[key], childPath).errors);
257
+ }
258
+ for (const [, child] of matching) errors.push(...visit(child, current[key], childPath).errors);
259
+ if (!named && !matching.length) validateExtraProperty(raw.additionalProperties, current[key], childPath, errors, add);
260
+ }
261
+
262
+ if (raw.propertyNames !== undefined) {
263
+ for (const error of visit(raw.propertyNames, key, childPath).errors) {
264
+ add(t('valid.renameProperty', { name: displayValue(key), reason: error.message }), childPath);
265
+ }
266
+ }
267
+ }
268
+
269
+ for (const [trigger, dependency] of Object.entries(raw.dependencies ?? {})) {
270
+ if (!hasValue(current, trigger)) continue;
271
+ if (Array.isArray(dependency)) requireDependencies(trigger, dependency);
272
+ else merge(apply(dependency));
273
+ }
274
+ for (const [trigger, names] of Object.entries(raw.dependentRequired ?? {})) {
275
+ if (hasValue(current, trigger)) requireDependencies(trigger, names);
276
+ }
277
+ for (const [trigger, child] of Object.entries(raw.dependentSchemas ?? {})) {
278
+ if (hasValue(current, trigger)) merge(apply(child));
279
+ }
280
+
281
+ if (!errors.length && raw.unevaluatedProperties !== undefined) {
282
+ for (const key of keys) {
283
+ if (evaluated.has(key)) continue;
284
+ evaluated.add(key);
285
+ if (!excluded.has(key)) validateExtraProperty(raw.unevaluatedProperties, current[key], [...path, key], errors, add);
286
+ }
287
+ }
288
+
289
+ function requireDependencies(trigger, names) {
290
+ for (const name of names) {
291
+ if (!excluded.has(name) && (!hasValue(current, name) || isBlank(current[name]))) {
292
+ add(t('valid.requiredWhen', { trigger: displayValue(trigger) }), [...path, name]);
293
+ }
294
+ }
295
+ }
296
+ }
297
+
298
+ function validateExtraProperty(raw, current, path, errors, add) {
299
+ if (raw === false) add(t('valid.removeProperty', { name: displayValue(path.at(-1)) }), path);
300
+ else if (raw !== undefined) errors.push(...visit(raw, current, path).errors);
301
+ }
302
+
303
+ function validateArray(raw, current, path, evaluated, errors, add) {
304
+ validateRange(current.length, raw.minItems, raw.maxItems, 'item', add);
305
+ const seen = new Map();
306
+ const prefix = raw.prefixItems ?? (Array.isArray(raw.items) ? raw.items : []);
307
+ const rest = Array.isArray(raw.items) ? raw.additionalItems : raw.items;
308
+ let contains = 0;
309
+ for (let index = 0; index < current.length; index++) {
310
+ const item = current[index];
311
+ const childPath = [...path, index];
312
+ if (raw.uniqueItems === true) {
313
+ const key = valueKey(item);
314
+ if (seen.has(key)) {
315
+ add(t('valid.duplicateItem', { n: seen.get(key) + 1 }), childPath);
316
+ } else {
317
+ seen.set(key, index);
318
+ }
319
+ }
320
+ const child = index < prefix.length ? prefix[index] : rest;
321
+ if (child !== undefined) {
322
+ evaluated.add(index);
323
+ validateItem(child, item, childPath, errors, add);
324
+ }
325
+ if (raw.contains !== undefined && !visit(raw.contains, item, childPath).errors.length) {
326
+ evaluated.add(index);
327
+ contains++;
328
+ }
329
+ }
330
+ if (raw.contains !== undefined) {
331
+ const name = schemaName(raw.contains);
332
+ const expected = name ? t('valid.contains.name', { name })
333
+ : raw.contains?.const !== undefined ? t('valid.contains.const', { value: displayValue(raw.contains.const) })
334
+ : raw.contains?.type ? t('valid.contains.type', { type: raw.contains.type })
335
+ : t('valid.contains.schema');
336
+ validateRange(contains, raw.minContains ?? 1, raw.maxContains, 'contains', add, { expected });
337
+ }
338
+ if (!errors.length && raw.unevaluatedItems !== undefined) {
339
+ for (let index = 0; index < current.length; index++) {
340
+ if (evaluated.has(index)) continue;
341
+ evaluated.add(index);
342
+ validateItem(raw.unevaluatedItems, current[index], [...path, index], errors, add);
343
+ }
344
+ }
345
+ }
346
+
347
+ function validateItem(raw, item, path, errors, add) {
348
+ if (raw === false) add(t('valid.removeItem'), path);
349
+ else if (isBlank(item)) missing(raw, path, errors);
350
+ else errors.push(...visit(raw, item, path).errors);
351
+ }
352
+
353
+ const errors = [...context.parseErrors.values()];
354
+ if (isBlank(value) && required) missing(schema, [], errors);
355
+ else if (value !== undefined) errors.push(...visit(schema, value, []).errors);
356
+ const reported = new Set();
357
+ return errors.filter(error => {
358
+ const key = JSON.stringify([error.path, error.message]);
359
+ if (reported.has(key)) return false;
360
+ reported.add(key);
361
+ return true;
362
+ });
363
+ }