hyperjump-io / json-schema

JSON Schema Validation, Annotation, and Bundling. Supports Draft 04, 06, 07, 2019-09, 2020-12, OpenAPI 3.0, and OpenAPI 3.1
https://json-schema.hyperjump.io/
MIT License
216 stars 22 forks source link

Cannot use validation in background scripts/service workers of browser extensions #48

Closed frederikb closed 7 months ago

frederikb commented 7 months ago

Hi,

I would like to use your library in the context of a browser extension for Chrome/Firefox built using the Web Extensions API.

The main logic of the extension is run in a so-called background script. In Manifest V3 versions of that API these are executed using service workers. The main impact is of course that we do not have a reference to the DOM and the document global variable is undefined.

When running the following code in this context I unfortunately get an exception because it assumes a normal web browser context:

const BOOKMARK_SCHEMA_URI = 'https://raw.githubusercontent.com/frederikb/bookmarksync/main/src/utils/bookmark.schema.json';
registerSchema(bookmarkSchema); // this is a parsed JSON containing the schema with a `$id` defined matching the uri
const validator = await validate(BOOKMARK_SCHEMA_URI);

The error:

ReferenceError: document is not defined at contextUri (background.js:13617:46) at get (background.js:13620:69) at getSchema (background.js:13701:26) at validate

The stacktrace is a bit mangled due to not having source maps set up, but the gist of it is that it's failing in @hyperjump/browser:

export const contextUri = () => document.location.toString();

while attempting to determine baseUri because it assumes a browser context.

My schema does not have any references to external schemas, neither absolute nor relative to.

  1. Is running in a web/service worker supposed to work and I'm doing some wrong?
  2. If not, is this something you are interested in supporting?
  3. Is there a workaround you would suggest? I could probably stub out the global document, but with what kind of location?

Thanks!

jdesrosiers commented 7 months ago

That's an interesting case. I hadn't considered service workers. I'll work on it. I expect that verifying that it works will be harder than the fix itself.

jdesrosiers commented 7 months ago

I could probably stub out the global document, but with what kind of location?

That would work. If you stub out the location to just an empty string, it should work fine. Ultimately, the feature that code supports doesn't make sense in the context of service workers, so whatever you need to do to make it not blow up shouldn't hurt anything else you're trying to use the library for.

jdesrosiers commented 7 months ago

I published a fix that I think should work. Let me know if you have you problems.

frederikb commented 7 months ago

Thanks @jdesrosiers for the fast turnaround! I can happily confirm that I can validate files using a schema embedded in my codebase.

Enjoying your libary so far, thanks a lot!

jdesrosiers commented 7 months ago

Glad it's working out for you!