josdejong / jsoneditor

A web-based tool to view, edit, format, and validate JSON
http://jsoneditoronline.org
Apache License 2.0
11.62k stars 2.05k forks source link

Support Ajv v8 #1371

Open msalahz opened 3 years ago

msalahz commented 3 years ago

Currently, Jsoneditpr only supports Ajv v6.12.6 which is behind with two major releases, so can you please support Ajv v8? Here is the migration document from v6 to v8

josdejong commented 3 years ago

Yes, would be nice to upgrade to the latest version of Ajv.

Anyone interested in helping out here?

StephenAtCFA commented 2 years ago

Started looking into this in order to be able to support the discriminator property (in ajv v8). Ended up getting this error and I'm not familiar enough with this project to figure it out quickly. ``` main.jss:4287 Uncaught TypeError: Cannot read properties of undefined (reading '0') at _parsePath (main.jss:4287:500) at Node.findNode (main.jss:2806:60) at findNode (main.jss:3878:141) at Array.map () at JSONEditor.treemode.validate (main.jss:3878:103) at JSONEditor.treemode._validateAndCatch (main.jss:3880:474) at JSONEditor.treemode.set (main.jss:3763:1) at JSONEditor.setMode (main.jss:1675:104) at JSONEditor._create (main.jss:1648:213) at new JSONEditor (main.jss:1627:209)

josdejong commented 2 years ago

Thanks @StephenAtCFA . Looking at the stacktrace, something goes wrong in the validate function. If I remember well, the new version of Ajv has a different data model for the validation errors (like error.dataPath doesn't exist anymore). I think the function improveSchemaError needs to be adjusted for the new model.

Here is the piece of code that is triggered looking at your stacktrace:

https://github.com/josdejong/jsoneditor/blob/f5b30462d9034dcd058c2241b6c1d2e4af8fce68/src/js/treemode.js#L567-L584

howardjones commented 1 year ago

I've run into an ajv bug that seems to be fixed in v8, so I also have been looking at updating jsoneditor. Looks like .dataPath is now .instancePath and there is no longer a .schema property? (output below from the ajv-cli for ajv 8.11.0)

  {
    instancePath: '/dns_zone_name',
    schemaPath: '#/properties/dns_zone_name/pattern',
    keyword: 'pattern',
    params: { pattern: '^[a-z0-9\\.\\-]+$' },
    message: 'must match pattern "^[a-z0-9\\.\\-]+$"'
  }
]

[
  {
    instancePath: '/state',
    schemaPath: '#/properties/state/enum',
    keyword: 'enum',
    params: { allowedValues: [Array] },
    message: 'must be equal to one of the allowed values'
  }
]
josdejong commented 1 year ago

Yes, it would be nice to upgrade to Ajv 8. Anyone able to help out here?

howardjones commented 1 year ago

I started on this, but I think I don't know enough about javascript development with babel to do this effectively. I can't see how to run in the debugger in say Webstorm, since a compilation step is needed.

josdejong commented 1 year ago

You can for example open `./test/test_build.html in your browser, this uses the non-minified but bundled code, which is readable. You can either put debug points directly in your browser, or open the html file in Webstorm via right click, "Debug 'test_build.html'".

Or you can put temporary console.logs to see what's going on.

Or, if you're only working on logic and no UI stuff, you can go via the unit tests: put debug points there and in the code that's being tested, and run the Mocha unit tests via Debug mode in Webstorm.

josdejong commented 1 year ago

Thanks for giving it a try BTW Howard. Do you still want to give it a try, or do you prefer to leave it to someone else?

howardjones commented 1 year ago

No, I'll have another try! Thanks for the pointers. I'm interested to learn some more of this. (for me, jsoneditor was a drop-in for my Flask app, so far). One thing I will say is that the unit tests all pass with my definitely-broken version :-) (just changing the pinned version of ajv, and fixing the instantiation)

josdejong commented 1 year ago

💪

Yeah, the tests do not have a high coverage, only for some util functions. Also the build setup is a bit outdated, making debugging a bit cumbersome. On a side note: the successor of this library, svelte-jsoneditor, has a more modern setup and better tested, and also is already using Ajv 8.