From df700e4ea1c9401f08de277f949099cfb3c9e870 Mon Sep 17 00:00:00 2001 From: oren Date: Tue, 29 Dec 2015 15:40:08 -0800 Subject: [PATCH] Add CLI --- bin/openbadges-validate | 28 ++++++++++++++++++++++++++++ index.js | 7 +++++++ 2 files changed, 35 insertions(+) create mode 100755 bin/openbadges-validate diff --git a/bin/openbadges-validate b/bin/openbadges-validate new file mode 100755 index 0000000..321ac01 --- /dev/null +++ b/bin/openbadges-validate @@ -0,0 +1,28 @@ +#!/usr/bin/env node +var validator = require('../'); +var input = process.argv.slice(2)[0]; + +(function main () { + console.log('Validating input: ' + input); + validator.validate(input, handleResponse); +})(); + +function handleResponse (response) { + if (response === null) { + console.log('Okay'); + process.exit(0); + } + if (response instanceof Error) { + console.log('Error'); + if (response.name && response.message) { + console.log(response.name + ': ' + response.message); + } + if (response.reason) { + console.log('└── ' + response.reason); + } + process.exit(1); + } + console.log('Unknown error:'); + console.log(response); + process.exit(1); +} diff --git a/index.js b/index.js index 69a8907..daf65b1 100644 --- a/index.js +++ b/index.js @@ -362,6 +362,8 @@ function validate(input, callback) { return validate.validateSigned(input, callback); if (isUrl(input)) return validate.validateHostedUrl(input, callback); + if (isJson(input)) + return validate.validateHosted(JSON.parse(input), callback, ''); return callback(makeError('input', 'not a valid signed badge or url', { input: input })); } return callback(makeError('input', 'input must be a string or object', { input: input })); @@ -703,6 +705,10 @@ function validateInterdependentFields(info, cb) { cb(objectIfKeys(errs), info); } +function isJson (str) { + try { JSON.parse(str); return true } catch(e) { return false } +} + module.exports = validate; validate.sha256 = sha256; @@ -725,3 +731,4 @@ validate.doesHashedEmailMatch = doesHashedEmailMatch; validate.VALID_HASHES = VALID_HASHES; validate.validateOldInterdependentFields = validateOldInterdependentFields; validate.validateInterdependentFields = validateInterdependentFields; +validate.validate = validate; \ No newline at end of file