argument.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * @copyright
  3. * Copyright © Microsoft Open Technologies, Inc.
  4. *
  5. * All Rights Reserved
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http: *www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
  14. * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  15. * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
  16. * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
  17. *
  18. * See the Apache License, Version 2.0 for the specific language
  19. * governing permissions and limitations under the License.
  20. */
  21. 'use strict';
  22. var _ = require('underscore');
  23. var constants = require('./constants');
  24. var UserCodeResponseFields = constants.UserCodeResponseFields;
  25. var argumentValidation = {
  26. /**
  27. * Throws if the passed in parameter is not a string.
  28. * @param {string} param The parameter to validate.
  29. * @param {string} name The name of the parameter being validated.
  30. * @throws {Error} If the parameter is not a valid string.
  31. */
  32. validateStringParameter : function(param, name) {
  33. if (!param) {
  34. throw new Error('The ' + name + ' parameter is required.');
  35. }
  36. if (!_.isString(param)) {
  37. throw new Error('The ' + name + ' parameter must be of type String.');
  38. }
  39. },
  40. /**
  41. * Validates that the callback passed in {@link AuthenticationContext.acquireToken} is a function
  42. * @param {AcquireTokenCallback} callback
  43. * @throws {Error} If the callback parameter is not a function
  44. */
  45. validateCallbackType : function(callback) {
  46. if (!callback || !_.isFunction(callback)) {
  47. throw new Error('acquireToken requires a function callback parameter.');
  48. }
  49. },
  50. validateUserCodeInfo : function(userCodeInfo) {
  51. if (!userCodeInfo){
  52. throw new Error('The userCodeInfo parameter is required');
  53. }
  54. if (!userCodeInfo.hasOwnProperty(UserCodeResponseFields.DEVICE_CODE)){
  55. throw new Error('The userCodeInfo is missing device_code');
  56. }
  57. if (!userCodeInfo.hasOwnProperty(UserCodeResponseFields.INTERVAL)){
  58. throw new Error('The userCodeInfo is missing interval');
  59. }
  60. if (!userCodeInfo.hasOwnProperty(UserCodeResponseFields.EXPIRES_IN)){
  61. throw new Error('The userCodeInfo is missing expires_in');
  62. }
  63. }
  64. };
  65. module.exports = argumentValidation;