summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/typecheck.js
blob: 5b5f86569ec02f9e47e8310035fa17f6dd16a53a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * Get the |key| attribute in the given |dict| and verify that it is an
 * array value.
 *
 * If the attribute is not an array, then an exception will be thrown unless
 * a default value is specified in |opt_default|.
 *
 * @param {Object.<string,*>} dict The dictionary containing the |key|
 * @param {string} key The key to typecheck in the |dict|.
 * @param {Array=} opt_default The value to return if the key is not a bool.
 * @return {Array} The |key| attribute value as an object.
 */
function getArrayAttr(dict, key, opt_default) {
  var value = /** @type {Array} */ (dict[key]);
  if (!(value instanceof Array)) {
    if (opt_default === undefined) {
      throw 'Invalid data type for ' + key +
          ' (expected: array, actual: ' + typeof value + ')';
    } else {
      return opt_default;
    }
  }
  return value;
}

/**
 * Get the |key| attribute in the given |dict| and verify that it is a
 * boolean value.
 *
 * If the attribute is not a boolean, then an exception will be thrown unless
 * a default value is specified in |opt_default|.
 *
 * @param {Object.<string,*>} dict The dictionary containing the |key|
 * @param {string} key The key to typecheck in the |dict|.
 * @param {boolean=} opt_default The value to return if the key is not a bool.
 * @return {boolean} The |key| attribute value as a boolean.
 */
function getBooleanAttr(dict, key, opt_default) {
  var value = /** @type {boolean} */ (dict[key]);
  if (typeof value != 'boolean') {
    if (opt_default === undefined) {
      throw 'Invalid data type for ' + key +
          ' (expected: boolean, actual: ' + typeof value + ')';
    } else {
      return opt_default;
    }
  }
  return value;
}

/**
 * Get the |key| attribute in the given |dict| and verify that it is a
 * number value.
 *
 * If the attribute is not a number, then an exception will be thrown unless
 * a default value is specified in |opt_default|.
 *
 * @param {Object.<string,*>} dict The dictionary containing the |key|
 * @param {string} key The key to typecheck in the |dict|.
 * @param {number=} opt_default The value to return if the key is not a number.
 * @return {number} The |key| attribute value as a number.
 */
function getNumberAttr(dict, key, opt_default) {
  var value = /** @type {number} */(dict[key]);
  if (typeof value != 'number') {
    if (opt_default === undefined) {
      throw 'Invalid data type for ' + key +
          ' (expected: number, actual: ' + typeof value + ')';
    } else {
      return opt_default;
    }
  }
  return value;
}

/**
 * Get the |key| attribute in the given |dict| and verify that it is an
 * object value.
 *
 * If the attribute is not an object, then an exception will be thrown unless
 * a default value is specified in |opt_default|.
 *
 * @param {Object.<string,*>} dict The dictionary containing the |key|
 * @param {string} key The key to typecheck in the |dict|.
 * @param {Object=} opt_default The value to return if the key is not a bool.
 * @return {Object} The |key| attribute value as an object.
 */
function getObjectAttr(dict, key, opt_default) {
  var value = /** @type {Object} */ (dict[key]);
  if (typeof value != 'object') {
    if (opt_default === undefined) {
      throw 'Invalid data type for ' + key +
          ' (expected: object, actual: ' + typeof value + ')';
    } else {
      return opt_default;
    }
  }
  return value;
}

/**
 * Get the |key| attribute in the given |dict| and verify that it is a
 * string value.
 *
 * If the attribute is not a string, then an exception will be thrown unless
 * a default value is specified in |opt_default|.
 *
 * @param {Object.<string,*>} dict The dictionary containing the |key|
 * @param {string} key The key to typecheck in the |dict|.
 * @param {string=} opt_default The value to return if the key is not a string.
 * @return {string} The |key| attribute value as a string.
 */
function getStringAttr(dict, key, opt_default) {
  var value =  /** @type {string} */ (dict[key]);
  if (typeof value != 'string') {
    if (opt_default === undefined) {
      throw 'Invalid data type for ' + key +
          ' (expected: string, actual: ' + typeof value + ')';
    } else {
      return opt_default;
    }
  }
  return value;
}

/**
 * Return a JSON object parsed from a string.
 *
 * If the string cannot be parsed, or does not result in an object, then an
 * exception will be thrown.
 *
 * @param {string} jsonString The JSON string to parse.
 * @return {Object} The JSON object created from the |jsonString|.
 */
function getJsonObjectFromString(jsonString) {
  var value = /** @type {Object} */ JSON.parse(jsonString);
  if (typeof value != 'object') {
    throw 'Invalid data type (expected: Object, actual: ' + typeof value + ')';
  }
  return value;
}