diff --git a/lib/inspect/checks/valid.js b/lib/inspect/checks/valid.js index 32be2b5..0cae376 100644 --- a/lib/inspect/checks/valid.js +++ b/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 @@ -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; } @@ -52,7 +57,12 @@ class validCheck extends Check { } _validateYAML(YAMLText) { - // TODO + try { + yaml.safeLoad(YAMLText); + return true; + } catch (err) { + return false; + } } }