josdejong / jsoneditor

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

Having trouble with schema validator #1399

Closed Harkort55 closed 2 years ago

Harkort55 commented 2 years ago

Hello. I am new to JsonEditor and I have the editor working in my page but cannot get schema validation to work. I have created a schema object and added it to the editor options.

var JsonEditor; // Set as global object in my .js function OpenJsonEditor() { var schema = { "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "MyTestSchema", ... }; var options = { schema: schema, mode: 'tree', search: true }; JsonEditor = new JSONEditor(container, options, obj); JsonEditor.expandAll(); }

What am I missing? I have tried adding a call to .validate() when the editor is closed. but .validate() is not a function.

function onJsonEditorClose() { JsonEditor.validate(); var jsonObj = JsonEditor.get(); }

I am sure I have not setup everything correctly. The validate example in the docs doesn't seem to do anything more however. Any ideas on what I am missing?

Thank you, Harkort

josdejong commented 2 years ago

It could be related to jsoneditor currently still using ajv@6 instead of the latest, and "https://json-schema.org/draft/2020-12/schema" is probably not supported in that version.

Harkort55 commented 2 years ago

I am using ...link href="Content/jsoneditor/jsoneditor.css" rel="stylesheet" type="text/css" ...script src="Scripts/jsoneditor/jsoneditor.js">

as I interpret the docs ("Which files do I need?" ) to mean that if I don't use the .min. version of jsoneditor it should include the json validator. Is that correct?

However, this statement aborts: var errs = JsonEditor.validate(); DFMS.js:4129 Uncaught TypeError: JsonEditor.validate is not a function

josdejong commented 2 years ago

Are you calling .validate() on the editor instance that you created, or on the constructor? You should do the first

Harkort55 commented 2 years ago

Calling it on my instance (perhaps poorly named). JsonEditor = new JSONEditor(container, options, obj); JsonEditor.expandAll(); <-- That works var errs = JsonEditor.validate(); <-- this does not work

josdejong commented 2 years ago

Ok that is indeed the right way 👍

Maybe the method is broken because of loading a JSON schema with unknown type of schema $schema? If you try out the following example you'll see that editor has a .validate() method:

https://github.com/josdejong/jsoneditor/blob/develop/examples/07_json_schema_validation.html

Harkort55 commented 2 years ago

I changed my code to use your exact schema. jobs, options, from that example. The previous line var jsonObj = JsonEditor.get(); does in fact return my edited object, but the next line var errs = JsonEditor.validate(); aborts because .validate() is not defined.

Perhaps it is where I stored the javascript files? I did not store them in /dist/. This is a .net aspx app and the json editor scripts are stored in ../Scripts/jsoneditor/jsoneditor.js. The script is being loaded into the broswer and the jsoneditor object is obviously being created and works for editing the json, but the .validate() is still missing. Is there validator code that also has to be included in the project, or is the validator code contained in jsoneditor.js?

looking at jsoneditor.js /**

I see that the validate function is expecting a string (which is not in the example) and makes no reference to the schema object. Not sure how that can can validate against a schema anyway. In fact that looks like nodejs code and not browser code?

josdejong commented 2 years ago

The function that you refer to is an internal util function, it is not the .validate() method exposed on the editor.

Are you using the latest version of the jsoneditor library?

Harkort55 commented 2 years ago

Apparently I am not using the latest version. I appear to be using @version 4.2.1 as that's the latest version brought in by nuget. I see 9.5.7 is the latest on github. I will try to get that version installed.

Harkort55 commented 2 years ago

I am not using git in my dev stack (unfortunately we must use tfs). I downloaded the .zip for 9.5.7 but not sure what to copy where, or if that will even work directly. Are there any docs on how to install manually from the .zip? It appears to need a nodejs server and a build to create the final .js files needed to install on my webserver? Or can I install the .js files in /src directly?

josdejong commented 2 years ago

ok that clarifies the issue.

The library is only published on npm. If you download the sourcecode from github, you'll have to build the library first as described in the readme.

Harkort55 commented 2 years ago

I see. I did find 9.5.7 on https://cdnjs.com/libraries/jsoneditor. And just started using it and it appears to be working for validation as well. I am sorry I wasted your time. Thank you!

josdejong commented 2 years ago

👍 glad to hear you solved it.