josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
814 stars 108 forks source link

Support for JSON Schema Draft 2020-12 #431

Closed b-wils closed 1 month ago

b-wils commented 1 month ago

I have a schema that is Draft 2020-12 and specified as instructed in https://json-schema.org/understanding-json-schema/reference/schema.

I'm getting the following error when passing the schema into createAjvValidator -

caught error while rendering third party code Error: no schema with key or ref "https://json-schema.org/draft/2020-12/schema"
    at Ajv2.validate (core.ts:359:21)

Digging into the ajv docs, looks like there are some slight changes to how you initialize the library for ajv- https://ajv.js.org/guide/schema-language.html#draft-2019-09-and-draft-2020-12.

I also attempted to instantiate ajv directly and pass it in directly, but it seems like the type jsoneditor expects is slightly different and I would need to pull in immutable-json-patch to remap the error list.

Is this something that is supported, or something that could be added?

josdejong commented 1 month ago

Yeah, it's not that easy to setup an Ajv instance that "just" supports any schema draft, from Draft 6 and 7 to 2019 and 2020.

The API of createAjvValidator has an option onCreateAjv: (ajv: Ajv) => Ajv | void which allows you to alter the Ajv instance or return an instance that you create yourself. That way you can load draft 2020-12, and you don't need to do the error mapping stuff yourself.

Help improving this with a better default behavior of createAjvValidator would be welcome though!

b-wils commented 1 month ago

Ah that did the trick, thanks! I'll look into improving default behavior when I have some time.