All files / services/validation shacl-report-exporter.ts

0% Statements 0/27
0% Branches 0/6
0% Functions 0/2
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import { Store } from '@faubulous/mentor-rdf';
import { getSeverityLabel } from '@src/utilities/shacl';
import { rdfDataFactory } from '@src/utilities/rdf';
import { ShaclValidationResult } from './shacl-validator-engine';
 
/**
 * Formats a validation result as a plain-text report.
 */
export function formatReportAsText(result: ShaclValidationResult): string {
	const lines: string[] = [];
	lines.push(`SHACL Validation Report`);
	lines.push(`Conforms: ${result.conforms}`);
	lines.push(`Results: ${result.results.length}`);
 
	// A run with missing shape graphs validated against less than the profile
	// promises — flag the report as potentially incomplete.
	if (result.missingShapeGraphs?.length) {
		lines.push('');
		lines.push(`WARNING: The result may be incomplete. ${result.missingShapeGraphs.length} configured shape graph(s) do not exist:`);
 
		for (const uri of result.missingShapeGraphs) {
			lines.push(`  ${uri}`);
		}
	}
 
	lines.push('');
 
	for (const r of result.results) {
		lines.push(`  Focus Node: ${r.focusNode}`);
		lines.push(`  Severity:   ${getSeverityLabel(r.severity)}`);
		if (r.path) {
			lines.push(`  Path:       ${r.path}`);
		}
		for (const msg of r.messages) {
			lines.push(`  Message:    ${msg}`);
		}
		if (r.value) {
			lines.push(`  Value:      ${r.value}`);
		}
		lines.push(`  Shape:      ${r.sourceShape}`);
		lines.push('');
	}
 
	return lines.join('\n');
}
 
/**
 * Serializes a validation result's report dataset as Turtle. The dataset is
 * copied into a temporary store graph because the serializer operates on named
 * graphs of a store rather than on bare datasets.
 */
export async function serializeReportAsTurtle(result: ShaclValidationResult): Promise<string> {
	const tempStore = new Store();
	const tempGraphUri = 'urn:shacl:report';
 
	for (const q of result.reportDataset) {
		tempStore.add(rdfDataFactory.quad(q.subject, q.predicate, q.object, rdfDataFactory.namedNode(tempGraphUri)));
	}
 
	return tempStore.serializeGraph(tempGraphUri, 'text/turtle', undefined, {
		'sh': 'http://www.w3.org/ns/shacl#',
		'xsd': 'http://www.w3.org/2001/XMLSchema#',
		'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
	});
}