4

I have JS as defined below (JS File). On push to a repo I want to statically validate things defined below (Validate This). I've been researching this and my first idea is to validate items 1-4 using https://www.npmjs.com/package/espree. Appreciate it if someone can confirm if this would do the job (be the best method) and if so an example validating the returned AST.

Validating item 5 is a little more interesting I need to extract the contents that w.abc.myobj is set which will effectively always equate to JSON and validate it's contents against rules using something like https://www.npmjs.com/package/ajv. Appreciate any insights on how best to do this as well especially the extraction of the JSON from the static code file.

Validate This

/* 
1. Is the first statement a try/catch block
2. Is the first statement within the try/catch block an anonymous function with a "w" arg
3. Is the second statement what is shown
4. Is the anonymous function called with the window object
5. Next i'd like to grab w.abc.myobj and validate it using schema validation.
*/

JS File

try {
  (function (w) {
    w.abc = w.abc || {};
    w.abc.myobj = {
      "prop1": {
        "enabled": true,
        "type": "non-fiction",
        "params: {
          "serverInfo": {
            "url": "{arg} ? https://www.url1.com : https://www.url2.com",
            "path": "/some/directory"
          },
          "accountInfo: {
            "user": "someUser1"
          }
        }
      },
      "prop2: {
        "enabled": true,
        "type": "fiction",
        "params": {
          "serverInfo": {
            "url": "https://www.url2.com",
            "path": "/some/directory"
          },
          "accountInfo: {
            "user": "someUser2"
          }
        }
      }
    };
  })(window);
} catch (e) { /* do nothing */ }
Captain Kirk
  • 350
  • 6
  • 24
  • 1
    Stack overflow works better if you ask a specific question. What did you try? Where did you get stuck? – Evert Mar 21 '21 at 04:53
  • 1
    If the users pushing to the repo can only author js that assigns to a json structure, wouldn't it be easier to have them push actual json files and add the wrapping js using some form of code generation step at build time? – Jonas Høgh Mar 21 '21 at 06:49
  • @JonasHøgh good point. In this use case the JS and JSON have to statically live in one file in Github. An external tool we use points to Github and expects the file to be that way. We'd still have to validate the JS if that got solved. – Captain Kirk Mar 21 '21 at 15:07
  • If you're essentially not giving any degrees of freedom to the users in writing the js, couldn't this be solved with a simple set of regexes for extracting the header and trailer js strings, then pass the rest through a json schema validator? – Jonas Høgh Mar 21 '21 at 15:56
  • @JonasHøgh that's right we can't deviate from how the JS is designed based on the 3rd party tool consuming the code and it's requirements. Fair point on using regex. I didn't want to go down that path unless I had to, although maybe getting AST validation statement by statement is overkill. Hoping someone has done this and had advice around choice or could argue one over the other. – Captain Kirk Mar 21 '21 at 23:26
  • The answer to your question is fairly simple -> write a compiler... or use an existing one that gives you AST, then you will only have to write a linter that goes over the AST and checks for variety of conditions. Based on the information you provide, that is more or less the most accurate answer. Or maybe get a linter and write few plugins for it to do your validations: ESLint is a good one! – Martin Chaov Mar 24 '21 at 19:47
  • What you could _try_ to do is produce an AST for what you expect and autogenerate a JSON schema for it. Then use that JSON schema to validate any new code submitted. – customcommander Mar 26 '21 at 14:35

1 Answers1

2

EsLint is built on top of the packages you mention, so you could make your life easier by writing an eslint plugin for each of your tests.

David Bradshaw
  • 11,859
  • 3
  • 41
  • 70
  • Good idea. Are you advocating that the tools I'm targeting would be the best way to solve this? Examples would be appreciated if you have any especially around the JS validation of code statements with AST. – Captain Kirk Mar 21 '21 at 15:14
  • I think writing this as an eslint plugin will get you half way there and you could also publish it in ways that a useful to others. I've only dabbled in this area, but their are some good intro to the topic online – David Bradshaw Mar 23 '21 at 17:29