Skip to content

Commit

Permalink
#403: Clarify error wording. (#409)
Browse files Browse the repository at this point in the history
* #403: Clarify error wording.

* #403: Improve wording for payload vs options
  • Loading branch information
aldermoovel authored and ziluvatar committed Oct 9, 2017
1 parent 81501a1 commit bb27eb3
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var sign_options_schema = {
subject: { isValid: isString, message: '"subject" must be a string' },
jwtid: { isValid: isString, message: '"jwtid" must be a string' },
noTimestamp: { isValid: isBoolean, message: '"noTimestamp" must be a boolean' },
keyid: { isValid: isString, message: '"keyid" must be a string' },
keyid: { isValid: isString, message: '"keyid" must be a string' }
};

var registered_claims_schema = {
Expand All @@ -29,16 +29,16 @@ var registered_claims_schema = {
nbf: { isValid: isNumber, message: '"nbf" should be a number of seconds' }
};

function validate(schema, unknown, object) {
function validate(schema, allowUnknown, object, parameterName) {
if (!isPlainObject(object)) {
throw new Error('Expected object');
throw new Error('Expected "' + parameterName + '" to be a plain object.');
}
Object.keys(object)
.forEach(function(key) {
var validator = schema[key];
if (!validator) {
if (!unknown) {
throw new Error('"' + key + '" is not allowed');
if (!allowUnknown) {
throw new Error('"' + key + '" is not allowed in "' + parameterName + '"');
}
return;
}
Expand All @@ -48,6 +48,14 @@ function validate(schema, unknown, object) {
});
}

function validateOptions(options) {
return validate(sign_options_schema, false, options, 'options');
}

function validatePayload(payload) {
return validate(registered_claims_schema, true, payload, 'payload');
}

var options_to_payload = {
'audience': 'aud',
'issuer': 'iss',
Expand Down Expand Up @@ -97,7 +105,7 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
return failure(new Error('payload is required'));
} else if (isObjectPayload) {
try {
validate(registered_claims_schema, true, payload);
validatePayload(payload);
}
catch (error) {
return failure(error);
Expand All @@ -122,7 +130,7 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
}

try {
validate(sign_options_schema, false, options);
validateOptions(options);
}
catch (error) {
return failure(error);
Expand Down

0 comments on commit bb27eb3

Please sign in to comment.