Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions bin/openbadges-validate
Original file line number Diff line number Diff line change
@@ -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);
}
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually use validateHostedUrl

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 }));
Expand Down Expand Up @@ -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;
Expand All @@ -725,3 +731,4 @@ validate.doesHashedEmailMatch = doesHashedEmailMatch;
validate.VALID_HASHES = VALID_HASHES;
validate.validateOldInterdependentFields = validateOldInterdependentFields;
validate.validateInterdependentFields = validateInterdependentFields;
validate.validate = validate;