Decouple image+video (URI) shape matching from specific image+video file types/extensions
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3530351. -->
Reported by: [wim leers](https://www.drupal.org/user/99777)
Related to !3 !1465 !1464
>>>
<h3 id="overview">Overview</h3>
<p>As recently <a href="https://git.drupalcode.org/project/experience_builder/-/merge_requests/1137#note_535087">pointed out by @isholgueras</a> on the MR for <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/experience_builder/issues/3518620" title="Status: Closed (fixed)">#3518620: Image prop shape matching should be case-insensitive, but JSON Schema doesn't support that</a></span>, the current <code>3.1.2.a structured data → matching field instances ⇒ dynamic prop source</code> infrastructure for matching <code>$ref: json-schema-definitions://experience_builder.module/image-uri</code> and <code>$ref: json-schema-definitions://experience_builder.module/image</code>, which rely on this:</p>
<pre> "image-uri": {<br> "title": "Image URL",<br> "type": "string",<br> "format": "uri-reference",<br> "pattern": "^(/|https?://)?.*\\.(png|gif|jpg|jpeg|webp)(\\?.*)?(#.*)?$"<br> },</pre><p>
getting matched against Drupal core's (semantically equivalent) <code>FileExtension</code> validation constraint, which necessarily requires translating from/to two wildly different representations.</p>
<p>The key problem: the list of file extensions is hardcoded. Which means customizations to the list of allowed image types would be a problem. Recently, AVIF support landed in <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-1"><a href="https://www.drupal.org/project/drupal/issues/3372932" title="Status: Active">#3372932: [Meta] High-performance images (nearly) out of the box</a></span>, which shows how likely this is to happen over time.</p>
<p>The current code dates back to April 2024, _before_ we had even started using d.o issues. This is some of the oldest code in XB and dates back to the <a href="https://git.drupalcode.org/project/experience_builder/-/tree/research__data_model"><code>research__data_model</code> PoC research branch</a>.</p>
<p><strong>Note: this <a href="https://github.com/json-schema-org/json-schema-spec/issues/233#issuecomment-279180514">follows a 2017 best practice!</a></strong></p>
<h4>Blockers discovered while working on this</h4>
<ol>
<li><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/experience_builder/issues/3540470" title="Status: Closed (fixed)">#3540470: Require 6.x version of `justinrainbow/json-schema` package to get correct JSON Schema validation</a></span>
</li><li><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/experience_builder/issues/3543770" title="Status: Closed (fixed)">#3543770: `noUi: true` SDCs do still show up in the UI</a></span>
</li><li><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/experience_builder/issues/3543783" title="Status: Closed (fixed)">#3543783: `card-with-stream-wrapper-image` test SDC generates an invalid `<img src>`</a></span>
</li><li><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/experience_builder/issues/3543805" title="Status: Closed (fixed)">#3543805: Resolved image file URLs invalid in kernel tests</a></span>
</li></ol>
<h3 id="proposed-resolution">Proposed resolution</h3>
<p>Use <a href="https://json-schema.org/draft/2020-12/json-schema-validation#name-contentmediatype"><code> contentMediaType</code></a>.</p>
<p>So then we'd go from:</p>
<pre> "image-uri": {<br> "title": "Image URL",<br> "type": "string",<br> "format": "uri-reference",<br> "pattern": "^(/|https?://)?.*\\.(png|gif|jpg|jpeg|webp)(\\?.*)?(#.*)?$"<br> },</pre><p>
to:</p>
<pre> "image-uri": {<br> "title": "Image URL",<br> "type": "string",<br> "format": "uri-reference",<br> "contentMediaType": "image/*"<br> },</pre><p>🐛 EDIT: This is in adequate — because it would also allow <code>ftp://cat.jpg</code>. Which means we need to <em>keep</em> <code>pattern</code> but make it less specific:</p>
<pre> "image-uri": {<br> "title": "Image URL",<br> "type": "string",<br> "format": "uri-reference",<br> "contentMediaType": "image/*",<br> "pattern": "^(/|https?://)?(?!.*\\://)[^\\s]+$"<br> },</pre><p>
(explanation: see the new <code>SchemaJsonPatternsTest</code>)</p>
<p>… which is <em>not quite</em> exactly how <code>contentMediaType</code> is meant to be used (that's really describing the contents of the string itself), but is … close enough? Something along these lines <a href="https://github.com/json-schema-org/json-schema-spec/issues/1557">has been proposed as <code>resourceMediaType</code></a>.</p>
<p>(Examples of actually defining new JSON Schema formats seem <em>impossible</em> to find.)</p>
<p>This would then also allow us to apply a similar improvement to "video URLs", which is being added at <span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/canvas/-/work_items/3524130" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/canvas/-/work_items/3524130</a></span>.</p>
<h3 id="ui-changes">User interface changes</h3>
<p>None.</p>
> Related issue: [Issue #3518620](https://www.drupal.org/node/3518620)
> Related issue: [Issue #3499279](https://www.drupal.org/node/3499279)
> Related issue: [Issue #3516359](https://www.drupal.org/node/3516359)
> Related issue: [Issue #3524130](https://www.drupal.org/node/3524130)
> Related issue: [Issue #3540470](https://www.drupal.org/node/3540470)
> Related issue: [Issue #3543770](https://www.drupal.org/node/3543770)
> Related issue: [Issue #3543783](https://www.drupal.org/node/3543783)
> Related issue: [Issue #3543805](https://www.drupal.org/node/3543805)
> Related issue: [Issue #3542895](https://www.drupal.org/node/3542895)
issue