aboutsummaryrefslogtreecommitdiffstats
path: root/JSONSchema/validate.js
blob: 9206f961c27c26028cf1b471fe51f0407d22ced1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#! /bin/env node

var fs = require('fs');
var validate = require('jsonschema').validate;
var readline = require('readline');
var process = require('process');

var schemaFile = process.argv[2];

var schema = '';
var testCode = '';
var success = true;
var numFiles = 0;
var numFailed = 0;
var numSucceeded = 0;
var fileNames = [];

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});

rl.on('line', function(fileName) {
    if (fileName == 'BlockchainTests/bcForgedTest/bcInvalidRLPTest.json') {
        return
    }
    fileNames.push(fileName);
});

rl.on('close', function() {
    schema = JSON.parse(fs.readFileSync(schemaFile));
    
    //sort file names alphabetically so that log output ordering is consistent
    fileNames.sort(function(a,b) {
        if(a<b) {
            return -1;
        } else {
            return 1;
        }

        return 0;
    });

    for (var i = 0; i < fileNames.length; i++) {
        try {
            testCode = JSON.parse(fs.readFileSync(fileNames[i]));
        } catch(e) {
            console.log('error on file:', fileNames[i])
            console.log(e);
            numFailed++;
            continue;
        }

        try {
            var x = validate(testCode, schema);

            if (x.errors.length > 0) {
                numFailed++;
                console.log(fileNames[i]+ ':\n');
                for (var j = 0; j < x.errors.length; j++) {
                    console.log('   ' + x.errors[j] + '\n')
                }
            } else {
                numSucceeded++;
            }
        } catch (e) {
            console.log(e);
            numFailed++;
        }
    }
    
    console.log("Valid: "+numSucceeded+"\n");
    console.log("Failed: "+numFailed+"\n");

    if(numFailed > 0) {
        process.exit(-1);
    }
});