@@ -20,7 +20,7 @@ var sign_options_schema = {
2020 subject : { isValid : isString , message : '"subject" must be a string' } ,
2121 jwtid : { isValid : isString , message : '"jwtid" must be a string' } ,
2222 noTimestamp : { isValid : isBoolean , message : '"noTimestamp" must be a boolean' } ,
23- keyid : { isValid : isString , message : '"keyid" must be a string' } ,
23+ keyid : { isValid : isString , message : '"keyid" must be a string' }
2424} ;
2525
2626var registered_claims_schema = {
@@ -29,16 +29,16 @@ var registered_claims_schema = {
2929 nbf : { isValid : isNumber , message : '"nbf" should be a number of seconds' }
3030} ;
3131
32- function validate ( schema , unknown , object ) {
32+ function validate ( schema , allowUnknown , object , parameterName ) {
3333 if ( ! isPlainObject ( object ) ) {
34- throw new Error ( 'Expected object' ) ;
34+ throw new Error ( 'Expected "' + parameterName + '" to be a plain object. ') ;
3535 }
3636 Object . keys ( object )
3737 . forEach ( function ( key ) {
3838 var validator = schema [ key ] ;
3939 if ( ! validator ) {
40- if ( ! unknown ) {
41- throw new Error ( '"' + key + '" is not allowed' ) ;
40+ if ( ! allowUnknown ) {
41+ throw new Error ( '"' + key + '" is not allowed in "' + parameterName + '" ') ;
4242 }
4343 return ;
4444 }
@@ -48,6 +48,14 @@ function validate(schema, unknown, object) {
4848 } ) ;
4949}
5050
51+ function validateOptions ( options ) {
52+ return validate ( sign_options_schema , false , options , 'options' ) ;
53+ }
54+
55+ function validatePayload ( payload ) {
56+ return validate ( registered_claims_schema , true , payload , 'payload' ) ;
57+ }
58+
5159var options_to_payload = {
5260 'audience' : 'aud' ,
5361 'issuer' : 'iss' ,
@@ -97,7 +105,7 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
97105 return failure ( new Error ( 'payload is required' ) ) ;
98106 } else if ( isObjectPayload ) {
99107 try {
100- validate ( registered_claims_schema , true , payload ) ;
108+ validatePayload ( payload ) ;
101109 }
102110 catch ( error ) {
103111 return failure ( error ) ;
@@ -122,7 +130,7 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
122130 }
123131
124132 try {
125- validate ( sign_options_schema , false , options ) ;
133+ validateOptions ( options ) ;
126134 }
127135 catch ( error ) {
128136 return failure ( error ) ;
0 commit comments