[x3d-public] Death to JSON schema. Long live X3D JSON! Long live X3D XML schema

John Carlson yottzumm at gmail.com
Sun Jun 23 02:40:06 PDT 2024


So I thought to myself.  I've already written one X3D XML Schema JSON
validator in Java.  How about one in JavaScript?

So using online resources, I searched for a schema validator and found one,
xsd-schema-validator. Within a matter of moments, I was able to hobble
together a simple validator using your and my friend X3DJSONLD.js and now I
can validate X3D JSON documents with XML schema (again!).  Next, we'll work
on a web-based one. Would X3DOM or X_ITE be able to follow my lead?  Is
there more actual good reasons why we can't adopt a JSON standard encoding
for X3D?


let fs = require("fs");
let validator = require('xsd-schema-validator');
let X3DJSONLD = require('./X3DJSONLD.js');
let xmldom = require('@xmldom/xmldom');
let DOMImplementation = new xmldom.DOMImplementation();

function validateWithXMLSchema(jsobj, path) {
        try {
          [ child, xml ]  = X3DJSONLD.loadJsonIntoDom(DOMImplementation,
jsobj, path);
          if (!xml) {
                xml = X3DJSONLD.serializeDOM(jsobj, child, true);
          }
          const result = validator.validateXML(xml,
'../../specifications/x3d-4.0.xsd');

          return result;
        } catch (err) {
          console.error('validation error', err);
        }
}

let promise =
validateWithXMLSchema(JSON.parse(fs.readFileSync(process.argv[2]),
process.argv[2]));
promise.then(
        function (result) { console.log(result); },
        function (rest) { console.error(rest); }
);
=======================================================
Example:

$ node xmlvalidate.js ../data/ball.json
Error: invalid xml (status=WITH_ERRORS)
        [error] cvc-complex-type.2.4.a: Invalid content was found starting
with element 'component'. One of '{meta}' is
expected. (8:50)
    at buildError
(C:\Users\john\X3DJSONLD\node_modules\xsd-schema-validator\lib\validator.js:195:16)
    at finish
(C:\Users\john\X3DJSONLD\node_modules\xsd-schema-validator\lib\validator.js:155:38)
    at ChildProcess.<anonymous>
(C:\Users\john\X3DJSONLD\node_modules\xsd-schema-validator\lib\validator.js:205:9)
    at ChildProcess.emit (node:events:519:28)
    at ChildProcess._handle.onexit (node:internal/child_process:294:12) {
  valid: false,
  result: 'WITH_ERRORS',
  messages: [
    "[error] cvc-complex-type.2.4.a: Invalid content was found starting
with element 'component'. One of '{meta}' is exp
ected. (8:50)"
  ]
}
=====================================
Another:
$ node xmlvalidate.js ../data/abox.json
{ valid: true, result: 'OK', messages: [] }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20240623/32a1b88c/attachment-0001.html>


More information about the x3d-public mailing list