aboutsummaryrefslogtreecommitdiffstats
path: root/JSONSchema/validate.js
blob: 368d28da28f2c5c58a8a13f8f5158ebe2c0ceb24 (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
#! /bin/env node

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

var readline = require('readline');
var schema = '';
var testCode = '';

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

rl.on('line', function(line){
    fs.readFile('schema.json', function(err, data) {
        if (err) {
            throw err;
        }

        schema = JSON.parse(data);

        fs.readFile(line, function(err, data) {
            if (err) {
                throw err;
            }

            try {
                testCode = JSON.parse(data);
            } catch(e) {
                console.log(e);
            }

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

                if (x.errors.length > 0) {
                    console.log(line+':\n');
                    for (var i = 0; i < x.errors.length; i++) {
                        console.log('   '+x.errors[i]+'\n')
                    }
                }
            } catch(e) {
                console.log(e);
            }
        });
    });
});