From eef0f600c26589e2db0504be302842aba1f69884 Mon Sep 17 00:00:00 2001 From: oren Date: Wed, 6 Jan 2016 12:02:01 -0800 Subject: [PATCH 1/2] Handle JSON in validate() --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index 69a8907..76ba955 100644 --- a/index.js +++ b/index.js @@ -362,6 +362,10 @@ function validate(input, callback) { return validate.validateSigned(input, callback); if (isUrl(input)) return validate.validateHostedUrl(input, callback); + if (isJson(input)) + var json = JSON.parse(input); + if (typeof json.verify.url !== 'undefined' && isUrl(json.verify.url)) + return validate.validateHostedUrl(json.verify.url, 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 })); @@ -541,6 +545,10 @@ function getInternalClass(thing) { return Object.prototype.toString.call(thing); } +function isJson (str) { + try { JSON.parse(str); return true } catch(e) { return false } +} + const isUrl = regexToValidator(re.url, 'must be a URL'); const isAbsoluteUrl = regexToValidator(re.absoluteUrl, 'must be an absolute URL'); const isEmail = regexToValidator(re.email, 'must be an email address'); From 73b9e75c902d27cf89ea4e0d2ac20e3fddbdc424 Mon Sep 17 00:00:00 2001 From: oren Date: Thu, 7 Jan 2016 17:55:58 -0800 Subject: [PATCH 2/2] Handle JSON in validate() --- index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 76ba955..34f113b 100644 --- a/index.js +++ b/index.js @@ -363,9 +363,9 @@ function validate(input, callback) { if (isUrl(input)) return validate.validateHostedUrl(input, callback); if (isJson(input)) - var json = JSON.parse(input); - if (typeof json.verify.url !== 'undefined' && isUrl(json.verify.url)) - return validate.validateHostedUrl(json.verify.url, callback, ''); + var url = urlFromAssertion(JSON.parse(input)); + if (url) + return validate.validateHostedUrl(url, 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 })); @@ -549,6 +549,15 @@ function isJson (str) { try { JSON.parse(str); return true } catch(e) { return false } } +function urlFromAssertion (json) { + try { + var url = json.verify.url; + if (isUrl(url)) + return url; + return false; + } catch(e) { return false; } +} + const isUrl = regexToValidator(re.url, 'must be a URL'); const isAbsoluteUrl = regexToValidator(re.absoluteUrl, 'must be an absolute URL'); const isEmail = regexToValidator(re.email, 'must be an email address');