Skip to content

settings_validators

Marcel Kloubert edited this page Feb 12, 2017 · 6 revisions

Home >> Settings >> Validators

Validators

Validators are scripts that can be used to validate a connection or user and reject it if not valid.

First define the script in your settings:

{
    "rest.api": {
        // ...

        "validator": {
            "script": "./my-validator.js",
            "options": "!!!XUS ZP"
        }

        // ...
    }
}

The ./my-validator.js should look like this:

exports.validate = function(args) {
    // access VS Code API (s. https://code.visualstudio.com/Docs/extensionAPI/vscode-api)
    var vscode = require('vscode');
    
    // access Node.js API provided by VS Code
    // s.  (s. https://nodejs.org/api/)
    var fs = require('fs');
    
    // access an own module
    var myModule = require('./my-module.js');
    
    // access a module used by the extension:
    // s. https://mkloubert.github.io/vs-rest-api/modules/_helpers_.html
    var helpers = args.require('./helpers');
    // s. https://mkloubert.github.io/vs-rest-api/modules/_host_helpers_.html
    var hostHelpers = args.require('./host/helpers');
    
    // access a module that is part of the extentsion
    // s. https://github.com/mkloubert/vs-rest-api/blob/master/package.json
    var glob = args.require('glob');
    
    // access the data from the settings
    // from the example above this is: "!!!XUS ZP"
    var opts = args.options;
    
    // share / store data (while current session)...
    // ... for this script
    var myState = args.state;
    args.state = new Date();
    // ... with other scripts of this type
    args.globalState['myValidator'] = new Date();
    // ... with the whole workspace
    args.workspaceState['myValidator'] = new Date();


    // ...

    // (false) will reject the connection
    return args.user.account.name != "mkloubert";
}

You can return a Promise for async executions, a boolean or nothing (what means (true)) for sync executions.

The args parameter uses the ValidatorArguments interface.

The context property of args uses the ValidateConnectionContext interface.

The value property of args uses the RemoteClient interface.

Clone this wiki locally