digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.64k stars 195 forks source link

Invalid URLs in list are nulled without proper feedback #556

Closed GerwinBosch closed 2 weeks ago

GerwinBosch commented 2 weeks ago

Hey,

When using the toRDF to parse lists with invalid IRIs (in our case, containing a space), the function doesn't throw an error. But instead returns a quad with null as the subject for the rdf:first relation. This causes issues when further processing the document. We can detect it by checking for null in the object position for now, However, the quad on its own is not that informative. In the end, I'd prefer an error to be thrown (as during parsing there should be more contextual information available)

The issue can be reproduced with the following snippet

[
  {
    "@context": [
      "http://www.w3.org/ns/anno.jsonld",
      {
        "target": {
          "@id": "oa:hasTarget",
          "@type": "@id",
          "@container": "@list"
        }
      }
    ],
    "target": [
      "https://example.org/x/ spaced-iri"
    ]
  }
]
dlongley commented 2 weeks ago

You should be able to pass the safe: true option to cause an error to be thrown with some details on what went wrong, see: https://github.com/digitalbazaar/jsonld.js?tab=readme-ov-file#safe-mode

The JSON-LD playground has an Options tab next to the JSON-LD Input tab that lets you set safe mode to true as well. It will default to true for canonicalized N-Quads, but defaults to false for other APIs for backwards compatibility.

GerwinBosch commented 2 weeks ago

Ah, thanks for the quick reply!

I did see the safe option for the expand function in the documentation. However, the TS types didn't show it in the typescript definitions for toRDF so I assumed it wasn't available. It works as expected now.