简体   繁体   中英

How do I validate JSON data - with full error report - using the jsweet::is-my-json-valid candy?

I'm working on a Javascript project and use the JSweet framework for writing the Javascript part. As part of it's function, my application will asynchronously load some JSON data (stored as separate file). I would like to validate that data using a JSON schema validator. JSweet offers several such validators, one of which is a port of 'is-my-json-valid' (original Javascript library in https://github.com/mafintosh/is-my-json-valid , JSweet port in http://public.jsweet.org/apidocs/releases/org/jsweet/candies/is-my-json-valid ).

I am already able to use the is-my-json-valid library to give me a true/false indication of whether a given JSON chunk conforms to a given JSON schema. However, in case of a failing validation, I would also like to retrieve error details as to why it failed (eg 'required attribute blah').

How would I have to use the is-my-json-valid library in JSweet so I get more than a true/false indication - ie I am also getting a full report telling me what was wrong with my JSON data?

Here is my JSweet test program. The transpiled Javascript already throws the exception correctly - the only thing that would gladden my heart is a detailed error report:

package json_valid.test_01;

import static def.is_my_json_valid.Globals.is_my_json_valid;

import jsweet.lang.Error;
import jsweet.lang.JSON;

public class Test_01
{
    public static void main (String[] args)
    {
        String              sSchema;
        String              sTestFail;
        String              sTestPass;

        sSchema     =
            "{\n"
            + "  \"required\": true,\n"
            + "  \"type\": \"object\",\n"
            + "  \"properties\": {\n"
            + "    \"hello\": {\n"
            + "      \"required\": true,\n"
            + "      \"type\": \"string\"\n"
            + "    }\n"
            + "  }\n"
            + "}\n";
        sTestFail       = "{\"a\": \"b\"}";
        sTestPass       = "{\"hello\": \"world\"}";
        _AssertConforms (sTestPass, sSchema);   /* Ok - expect nothing printed to console  */
        _AssertConforms (sTestFail, sSchema);   /* AiaOw - expect error message on console */
    }

    private static void _AssertConforms (String jsonData, String jsonSchema)
    {
        Object              oSchema;
        Object              oTested;
        boolean             result;
        String              specifics;
        String              message;

        oTested = JSON.parse (jsonData);
        oSchema = JSON.parse (jsonSchema);
        result = is_my_json_valid (oSchema).apply (oTested);
        if (! result)
        {
            specifics = "Sorry, still waiting for that nifty code to extract error specifics from is-my-json-valid.";
            message   = "Validation failed. Error specifics: " + specifics;
            throw new Error (message);
        }
    }
}

My HTML test page is rather boring:

<!DOCTYPE html>
<html>
<head>
<meta charset="US-ASCII">
<title>JSON validator test</title>
<script src="../../../target/dist/bundle-test_01.js"></script>
</head>
<body>

</body>
</html>

However, when I load the page into my Chrome web browser, the web console provides me with an exception as expected:

bundle-test_01.js:1535 Uncaught Error: Validation failed. Error specifics: Sorry, still waiting for that nifty code to extract error specifics from is-my-json-valid.
    Test_01._AssertConforms @ bundle-test_01.js:1535
    Test_01.main @ bundle-test_01.js:1521
    12.is-my-json-valid @ bundle-test_01.js:1541
    s @ bundle-test_01.js:1
    e @ bundle-test_01.js:1
    (anonymous function) @ bundle-test_01.js:1

All I am missing is the error details in the exception message.

Thank you so much for your consideration!

Looks like the current version of JSweet doesn't yet work together with the is-my-json-valid component. From the JSweet author:

Ok. I have bad news.

I took a closer look to is-my-json-valid, and it seems that it is only provided as a node module. That wouldn't be an issue and in theory, you would just need to compile with the "module" option. However, it seems that the way the module is defined is not supported yet by JSweet. It looks like JSweet cannot require properly the module, as it is specified here: https://github.com/mafintosh/is-my-json-valid .

In general, before using a module, it is good to know if it can be used from the browser, because node modules are still only partially supported in JSweet.

The good news is that is-my-json-valid seems to be the perfect use case that I was looking for to improve JSweet's requires, as already discussed in #129. So I will be working on it ASAP.

See https://github.com/cincheo/jsweet/issues/144

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM