diff --git a/Test/Test/ValidatorTests.cs b/Test/Test/ValidatorTests.cs index 71fab13..28163c1 100644 --- a/Test/Test/ValidatorTests.cs +++ b/Test/Test/ValidatorTests.cs @@ -142,6 +142,76 @@ public void HostGetRouteCollisionIsReported() diagnostic.Message.Contains("docker-test")); } + [TestMethod] + [DataRow("value", "value")] + [DataRow("value", "Value")] + public void DuplicateParameterNameIsReported(string first, string second) + { + ShouldReportShapeViolation( + Manifest("dup", extras: $""" + :parameter [ :name "{first}" ; :description "d" ; :example "x" ], [ :name "{second}" ; :description "d2" ; :example "y" ] ; + """), + "Parameter name must be unique within its endpoint"); + } + + [TestMethod] + public void LiteralParameterDatatypeIsReported() + { + ShouldReportShapeViolation( + Manifest("datatype", extras: """:parameter [ :name "value" ; :description "d" ; :example "x" ; :datatype "integer" ] ;"""), + "Parameter datatype must be an IRI"); + } + + [TestMethod] + public void MissingParameterNameIsReported() + { + ShouldReportShapeViolation( + Manifest("noname", extras: """:parameter [ :description "d" ; :example "x" ] ;"""), + "Parameter must have at least one name"); + } + + [TestMethod] + public void InvalidParameterNameIsReported() + { + ShouldReportShapeViolation( + Manifest("badname", extras: """:parameter [ :name "not valid" ; :description "d" ; :example "x" ] ;"""), + "Parameter name must be a valid SPARQL variable name"); + } + + [TestMethod] + public void LiteralFrameIsReported() + { + ShouldReportShapeViolation( + Manifest("frameliteral", extras: """:frame "./f.jsonld" ;"""), + "Endpoint frame must be xsd:anyURI literal"); + } + + [TestMethod] + public void RelativeFrameWithoutDotSlashIsReported() + { + ShouldReportShapeViolation( + Manifest("framed", "f.jsonld"), + "Endpoint frame must start with './'"); + } + + [TestMethod] + public void LeadingSlashPathIsReported() + { + ShouldReportShapeViolation( + Manifest("slash", path: "/slash"), + "Endpoint path must not start with '/'"); + } + + [TestMethod] + public void TypedParameterHasNoDiagnostics() + { + using var directory = new TempDefinitions( + Manifest("typed", extras: """:parameter [ :name "value_1" ; :description "d" ; :example "1" ; :datatype xsd:integer ] ;"""), + "SELECT * WHERE { ?s ?p ?value_1 }"); + + Validate(directory.Provider).Should().BeEmpty(); + } + [TestMethod] public void HostPostOnlyRouteDoesNotCollide() { @@ -150,22 +220,35 @@ public void HostPostOnlyRouteDoesNotCollide() private static IFileProvider Fixture(string name) => new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, "Fixtures", name)); - private static string Manifest(string name, string? frame = null) => $$""" + private static string Manifest(string name, string? frame = null, string extras = "", string? path = null) => $$""" PREFIX : PREFIX xsd: [ :name "{{name}}" ; :endpoint [ - :path "{{name}}" ; + :path "{{path ?? name}}" ; :sparql "./q.sparql"^^xsd:anyURI ; {{(frame is null ? "" : $":frame \"{frame}\"^^xsd:anyURI ;")}} :name "{{name}}" ; :description "{{name}}" ; + {{extras}} ] ; ] . """; + private static void ShouldReportShapeViolation(string manifest, string message) + { + using var directory = new TempDefinitions(manifest, "SELECT * WHERE { ?s ?p ?o }", "{}"); + + var diagnostics = Validate(directory.Provider); + + diagnostics.Should().Contain(diagnostic => + diagnostic.Code == DiagnosticCodes.ShapeViolation && + diagnostic.Message.Contains(message)); + diagnostics.Should().NotContain(diagnostic => diagnostic.Code == DiagnosticCodes.Internal); + } + private static IReadOnlyList Validate(IFileProvider definitions, Action? map = null) { var builder = WebApplication.CreateBuilder(); diff --git a/TheODI.NamedQuery/README.md b/TheODI.NamedQuery/README.md index 6b60f79..91eda4d 100644 --- a/TheODI.NamedQuery/README.md +++ b/TheODI.NamedQuery/README.md @@ -128,17 +128,17 @@ Each endpoint: | `:sparql` | yes | Relative path of the query file, an `xsd:anyURI` starting with `./` | | `:name` | yes | Title (home page, OpenAPI, MCP tool title) | | `:description` | yes | Description (home page, OpenAPI, MCP tool description) | -| `:frame` | no | Relative path of a JSON-LD frame applied to graph results served as JSON-LD | +| `:frame` | no | Relative path of a JSON-LD frame applied to graph results served as JSON-LD, an `xsd:anyURI` starting with `./` | | `:parameter` | no, repeatable | A query parameter | Each parameter: | Property | Required | Meaning | |---|---|---| -| `:name` | yes | Query string parameter name and SPARQL variable name | +| `:name` | yes | Query string parameter name and SPARQL variable name; letters, digits and `_` only; unique per endpoint, case-insensitive | | `:description` | yes | Description (OpenAPI, MCP) | | `:example` | yes | Example value (home page links, OpenAPI, MCP) | -| `:datatype` | no | Datatype of the bound literal; default `xsd:string` | +| `:datatype` | no | Datatype of the bound literal, an IRI such as `xsd:integer`; default `xsd:string` | Every parameter must be supplied exactly once; otherwise the response is `400 Bad Request`. `format` is reserved. diff --git a/TheODI.NamedQuery/Validation/shapes.ttl b/TheODI.NamedQuery/Validation/shapes.ttl index 6398e6b..ea31dc7 100644 --- a/TheODI.NamedQuery/Validation/shapes.ttl +++ b/TheODI.NamedQuery/Validation/shapes.ttl @@ -8,28 +8,35 @@ _:prefixes :declare [ ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:path ; :datatype xsd:string ; :message "Endpoint path must be string literal" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:path ; :minCount 1 ; :message "Endpoint must have at least one path" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:path ; :maxCount 1 ; :message "Endpoint must have at most one path" ; ] . [ - :targetSubjectsOf nq:path ; + :targetSubjectsOf nq:path, nq:sparql ; + :path nq:path ; + :pattern "^$|^[^/]" ; + :message "Endpoint path must not start with '/'" ; +] . + +[ + :targetSubjectsOf nq:path ; :sparql [ :message "Endpoint path must be unique" ; :prefixes _:prefixes ; @@ -38,140 +45,212 @@ _:prefixes :declare [ ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:sparql ; :datatype xsd:anyURI ; :message "Endpoint SPARQL must be xsd:anyURI literal" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:sparql ; :pattern "^\\./.+" ; :message "Endpoint SPARQL must start with './'" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:sparql ; :minCount 1 ; :message "Endpoint must have at least one SPARQL" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:sparql ; :maxCount 1 ; :message "Endpoint must have at most one SPARQL" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; + :path nq:frame ; + :datatype xsd:anyURI ; + :message "Endpoint frame must be xsd:anyURI literal" ; +] . + +[ + :targetSubjectsOf nq:path, nq:sparql ; + :path nq:frame ; + :pattern "^\\./.+" ; + :message "Endpoint frame must start with './'" ; +] . + +[ + :targetSubjectsOf nq:path, nq:sparql ; + :path nq:frame ; + :maxCount 1 ; + :message "Endpoint must have at most one frame" ; +] . + +[ + :targetSubjectsOf nq:path, nq:sparql ; :path nq:name ; :datatype xsd:string ; :message "Endpoint name must be string literal" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:name ; :minCount 1 ; :message "Endpoint must have at least one name" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:name ; :maxCount 1 ; :message "Endpoint must have at most one name" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:description ; :datatype xsd:string ; :message "Endpoint description must be string literal" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:description ; :minCount 1 ; :message "Endpoint must have at least one description" ; ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path nq:description ; :maxCount 1 ; :message "Endpoint must have at most one description" ; ] . [ - :targetObjectsOf nq:parameter ; + :targetObjectsOf nq:parameter ; + :path nq:name ; + :datatype xsd:string ; + :message "Parameter name must be string literal" ; +] . + +[ + :targetObjectsOf nq:parameter ; + :path nq:name ; + :minCount 1 ; + :message "Parameter must have at least one name" ; +] . + +[ + :targetObjectsOf nq:parameter ; + :path nq:name ; + :maxCount 1 ; + :message "Parameter must have at most one name" ; +] . + +[ + :targetObjectsOf nq:parameter ; + :path nq:name ; + :pattern "^[A-Za-z0-9_]+$" ; + :message "Parameter name must be a valid SPARQL variable name (letters, digits, underscore)" ; +] . + +[ + :targetObjectsOf nq:parameter ; + :sparql [ + :message "Parameter name must be unique within its endpoint (case-insensitive)" ; + :prefixes _:prefixes ; + :select "SELECT $this ?value { ?endpoint nq:parameter $this, ?other . $this nq:name ?value . ?other nq:name ?otherValue . FILTER (?other != $this && LCASE(STR(?value)) = LCASE(STR(?otherValue))) }" ; + ] ; +] . + +[ + :targetObjectsOf nq:parameter ; + :path nq:datatype ; + :nodeKind :IRI ; + :message "Parameter datatype must be an IRI" ; +] . + +[ + :targetObjectsOf nq:parameter ; + :path nq:datatype ; + :maxCount 1 ; + :message "Parameter must have at most one datatype" ; +] . + +[ + :targetObjectsOf nq:parameter ; :path nq:description ; :datatype xsd:string ; :message "Parameter description must be string literal" ; ] . [ - :targetObjectsOf nq:parameter ; + :targetObjectsOf nq:parameter ; :path nq:description ; :minCount 1 ; :message "Parameter must have at least one description" ; ] . [ - :targetObjectsOf nq:parameter ; + :targetObjectsOf nq:parameter ; :path nq:description ; :maxCount 1 ; :message "Parameter must have at most one description" ; ] . [ - :targetObjectsOf nq:parameter ; + :targetObjectsOf nq:parameter ; :path nq:example ; :datatype xsd:string ; :message "Parameter example must be string literal" ; ] . [ - :targetObjectsOf nq:parameter ; + :targetObjectsOf nq:parameter ; :path nq:example ; :minCount 1 ; :message "Parameter must have at least one example" ; ] . [ - :targetObjectsOf nq:parameter ; + :targetObjectsOf nq:parameter ; :path nq:example ; :maxCount 1 ; :message "Parameter must have at most one example" ; ] . [ - :targetSubjectsOf nq:endpoint ; + :targetSubjectsOf nq:endpoint ; :path nq:name ; :datatype xsd:string ; :message "Manifest name must be string literal" ; ] . [ - :targetSubjectsOf nq:endpoint ; + :targetSubjectsOf nq:endpoint ; :path nq:name ; :minCount 1 ; :message "Manifest must have at least one name" ; ] . [ - :targetSubjectsOf nq:endpoint ; + :targetSubjectsOf nq:endpoint ; :path nq:name ; :maxCount 1 ; :message "Manifest must have at most one name" ; ] . [ - :targetNode [] ; + :targetNode [] ; :sparql [ :message "Manifest must have at least one manifest node" ; :prefixes _:prefixes ; @@ -180,7 +259,7 @@ _:prefixes :declare [ ] . [ - :targetNode [] ; + :targetNode [] ; :sparql [ :message "Manifest must have at most one manifest node" ; :prefixes _:prefixes ; @@ -189,14 +268,14 @@ _:prefixes :declare [ ] . [ - :targetSubjectsOf nq:path, nq:sparql ; + :targetSubjectsOf nq:path, nq:sparql ; :path [ :inversePath nq:endpoint ] ; :minCount 1 ; :message "Endpoint must be in manifest endpoints" ; ] . [ - :targetNode [] ; + :targetNode [] ; :sparql [ :message "Manifest must have at least one endpoint" ; :prefixes _:prefixes ;