Skip to content

Commit

Permalink
feat: adding valid check (YAML validation)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssmirr committed Feb 12, 2020
1 parent 649118d commit 736a27b
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/inspect/checks/valid.js
@@ -1,5 +1,6 @@
const chalk = require('chalk');
const Check = require('./check');
const yaml = require('js-yaml');

/**
* This checks if known files have a valid format
Expand All @@ -16,21 +17,25 @@ class validCheck extends Check {
const type = Object.keys(arg)[0]; // json, yaml, ...
const filePath = arg[type];

// json files
if (type === 'json') {
// try to read the file
const readFile = (await this.connector.exec(`cat ${filePath}`));
let valid;
// try to read the file
const readFile = (await this.connector.exec(`cat ${filePath}`));
let valid;

// if was able to read the file
if (readFile.exitCode == 0)
// if was able to read the file
if (readFile.exitCode == 0) {

if (type === 'json')
valid = this._validateJSON(readFile.stdout);

else
valid = readFile.stderr.includes('No such file') ? 'No such file or directory' : readFile.stderr;
else if (type === 'yaml' || type === 'yml')
valid = this._validateYAML(readFile.stdout);

results.push({ type, filePath, valid });
}

else
valid = readFile.stderr.includes('No such file') ? 'No such file or directory' : readFile.stderr;

results.push({ type, filePath, valid });
}
return results;
}
Expand All @@ -52,7 +57,12 @@ class validCheck extends Check {
}

_validateYAML(YAMLText) {
// TODO
try {
yaml.safeLoad(YAMLText);
return true;
} catch (err) {
return false;
}
}
}

Expand Down

0 comments on commit 736a27b

Please sign in to comment.