|
| 1 | +/** |
| 2 | + * Guard the shacl-form implementation so that multi-value blank nodes remain visible even when |
| 3 | + * nested nodes currently violate their shape (e.g., missing required fields). Without this guard |
| 4 | + * the component drops every value after validation and users cannot fix the data. |
| 5 | + * |
| 6 | + * This patch must be loaded AFTER the shacl-form web component is imported. |
| 7 | + */ |
| 8 | + |
| 9 | +(function() { |
| 10 | + 'use strict'; |
| 11 | + |
| 12 | + // Wait for custom elements to be defined |
| 13 | + async function applyPatch() { |
| 14 | + // Wait for shacl-property element to be defined |
| 15 | + await customElements.whenDefined('shacl-property'); |
| 16 | + |
| 17 | + const shaclPropertyCtor = customElements.get('shacl-property'); |
| 18 | + |
| 19 | + if (shaclPropertyCtor && !shaclPropertyCtor.__rdmFilterGuardApplied) { |
| 20 | + const prototype = shaclPropertyCtor.prototype; |
| 21 | + const originalFilter = prototype.filterValidValues; |
| 22 | + |
| 23 | + if (typeof originalFilter === 'function') { |
| 24 | + console.log('[SHACL Patch] Applying filterValidValues guard for multi-value blank nodes'); |
| 25 | + |
| 26 | + prototype.filterValidValues = async function(values, subject) { |
| 27 | + // If there's no qualifiedValueShape, don't filter - keep all values |
| 28 | + if (!this?.template?.qualifiedValueShape) { |
| 29 | + return values; |
| 30 | + } |
| 31 | + |
| 32 | + // Otherwise, use the original validation logic |
| 33 | + return originalFilter.call(this, values, subject); |
| 34 | + }; |
| 35 | + |
| 36 | + shaclPropertyCtor.__rdmFilterGuardApplied = true; |
| 37 | + console.log('[SHACL Patch] Guard applied successfully'); |
| 38 | + } else { |
| 39 | + console.warn('[SHACL Patch] filterValidValues method not found'); |
| 40 | + } |
| 41 | + } else if (shaclPropertyCtor?.__rdmFilterGuardApplied) { |
| 42 | + console.log('[SHACL Patch] Guard already applied'); |
| 43 | + } else { |
| 44 | + console.error('[SHACL Patch] shacl-property element not found'); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Apply patch when DOM is ready |
| 49 | + if (document.readyState === 'loading') { |
| 50 | + document.addEventListener('DOMContentLoaded', applyPatch); |
| 51 | + } else { |
| 52 | + applyPatch(); |
| 53 | + } |
| 54 | +})(); |
0 commit comments