vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
8.9 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. var util = require('../../util');
  2. let errorFound = false;
  3. let printStyle = 'background: #FFeeee; color: #dd0000';
  4. /**
  5. * Used to validate options.
  6. */
  7. class Validator {
  8. constructor() {
  9. }
  10. /**
  11. * Main function to be called
  12. * @param options
  13. * @param subObject
  14. * @returns {boolean}
  15. */
  16. static validate(options, referenceOptions, subObject) {
  17. errorFound = false;
  18. let usedOptions = referenceOptions;
  19. if (subObject !== undefined) {
  20. usedOptions = referenceOptions[subObject];
  21. }
  22. Validator.parse(options, usedOptions, []);
  23. return errorFound;
  24. }
  25. /**
  26. * Will traverse an object recursively and check every value
  27. * @param options
  28. * @param referenceOptions
  29. * @param path
  30. */
  31. static parse(options, referenceOptions, path) {
  32. for (let option in options) {
  33. if (options.hasOwnProperty(option)) {
  34. Validator.check(option, options, referenceOptions, path);
  35. }
  36. }
  37. }
  38. /**
  39. * Check every value. If the value is an object, call the parse function on that object.
  40. * @param option
  41. * @param options
  42. * @param referenceOptions
  43. * @param path
  44. */
  45. static check(option, options, referenceOptions, path) {
  46. if (referenceOptions[option] === undefined && referenceOptions.__any__ === undefined) {
  47. Validator.getSuggestion(option, referenceOptions, path);
  48. }
  49. else if (referenceOptions[option] === undefined && referenceOptions.__any__ !== undefined) {
  50. // __any__ is a wildcard. Any value is accepted and will be further analysed by reference.
  51. if (Validator.getType(options[option]) === 'object') {
  52. util.copyAndExtendArray(path, option);
  53. Validator.checkFields(option, options, referenceOptions, '__any__', referenceOptions['__any__'].__type__, path);
  54. }
  55. }
  56. else {
  57. // Since all options in the reference are objects, we can check whether they are supposed to be object to look for the __type__ field.
  58. if (referenceOptions[option].__type__ !== undefined) {
  59. util.copyAndExtendArray(path, option);
  60. // if this should be an object, we check if the correct type has been supplied to account for shorthand options.
  61. Validator.checkFields(option, options, referenceOptions, option, referenceOptions[option].__type__, path);
  62. }
  63. else {
  64. Validator.checkFields(option, options, referenceOptions, option, referenceOptions[option], path);
  65. }
  66. }
  67. }
  68. /**
  69. *
  70. * @param {String} option | the option property
  71. * @param {Object} options | The supplied options object
  72. * @param {Object} referenceOptions | The reference options containing all options and their allowed formats
  73. * @param {String} referenceOption | Usually this is the same as option, except when handling an __any__ tag.
  74. * @param {String} refOptionType | This is the type object from the reference options
  75. * @param {Array} path | where in the object is the option
  76. */
  77. static checkFields(option, options, referenceOptions, referenceOption, refOptionObj, path) {
  78. let optionType = Validator.getType(options[option]);
  79. let refOptionType = refOptionObj[optionType];
  80. if (refOptionType !== undefined) {
  81. // if the type is correct, we check if it is supposed to be one of a few select values
  82. if (Validator.getType(refOptionType) === 'array') {
  83. if (refOptionType.indexOf(options[option]) === -1) {
  84. console.log('%cInvalid option detected in "' + option + '".' +
  85. ' Allowed values are:' + Validator.print(refOptionType) + ' not "' + options[option] + '". ' + Validator.printLocation(path, option), printStyle);
  86. errorFound = true;
  87. }
  88. else if (optionType === 'object') {
  89. Validator.parse(options[option], referenceOptions[referenceOption], path);
  90. }
  91. }
  92. else if (optionType === 'object') {
  93. Validator.parse(options[option], referenceOptions[referenceOption], path);
  94. }
  95. }
  96. else {
  97. if (refOptionObj['undef'] !== undefined && optionType === 'undefined') {
  98. // item is undefined, which is allowed
  99. }
  100. else if (refOptionObj['fn'] !== undefined && optionType === 'function') {
  101. // item is a function, which is allowed
  102. }
  103. else {
  104. // type of the field is incorrect
  105. console.log('%cInvalid type received for "' + option + '". Expected: ' + Validator.print(Object.keys(refOptionObj)) + '. Received [' + optionType + '] "' + options[option] + '"' + Validator.printLocation(path, option), printStyle);
  106. errorFound = true;
  107. }
  108. }
  109. }
  110. static getType(object) {
  111. var type = typeof object;
  112. if (type === 'object') {
  113. if (object === null) {
  114. return 'null';
  115. }
  116. if (object instanceof Boolean) {
  117. return 'boolean';
  118. }
  119. if (object instanceof Number) {
  120. return 'number';
  121. }
  122. if (object instanceof String) {
  123. return 'string';
  124. }
  125. if (Array.isArray(object)) {
  126. return 'array';
  127. }
  128. if (object instanceof Date) {
  129. return 'date';
  130. }
  131. if (object.nodeType !== undefined) {
  132. return 'dom'
  133. }
  134. return 'object';
  135. }
  136. else if (type === 'number') {
  137. return 'number';
  138. }
  139. else if (type === 'boolean') {
  140. return 'boolean';
  141. }
  142. else if (type === 'string') {
  143. return 'string';
  144. }
  145. else if (type === undefined) {
  146. return 'undefined';
  147. }
  148. return type;
  149. }
  150. static getSuggestion(option, options, path) {
  151. let closestMatch = '';
  152. let min = 1e9;
  153. let threshold = 8;
  154. for (let op in options) {
  155. let distance = Validator.levenshteinDistance(option, op);
  156. if (min > distance && distance < threshold) {
  157. closestMatch = op;
  158. min = distance;
  159. }
  160. }
  161. if (min < threshold) {
  162. console.log('%cUnknown option detected: "' + option + '". Did you mean "' + closestMatch + '"?' + Validator.printLocation(path, option), printStyle);
  163. }
  164. else {
  165. console.log('%cUnknown option detected: "' + option + '". Did you mean one of these: ' + Validator.print(Object.keys(options)) + Validator.printLocation(path, option), printStyle);
  166. }
  167. errorFound = true;
  168. return closestMatch;
  169. }
  170. static printLocation(path, option) {
  171. let str = '\n\nProblem value found at: \noptions = {\n';
  172. for (let i = 0; i < path.length; i++) {
  173. for (let j = 0; j < i + 1; j++) {
  174. str += ' ';
  175. }
  176. str += path[i] + ': {\n'
  177. }
  178. for (let j = 0; j < path.length + 1; j++) {
  179. str += ' ';
  180. }
  181. str += option + '\n';
  182. for (let i = 0; i < path.length + 1; i++) {
  183. for (let j = 0; j < path.length - i; j++) {
  184. str += ' ';
  185. }
  186. str += '}\n'
  187. }
  188. return str + '\n\n';
  189. }
  190. static print(options) {
  191. return JSON.stringify(options).replace(/(\")|(\[)|(\])|(,"__type__")/g, "").replace(/(\,)/g, ', ')
  192. }
  193. // Compute the edit distance between the two given strings
  194. // http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript
  195. /*
  196. Copyright (c) 2011 Andrei Mackenzie
  197. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  198. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  199. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  200. */
  201. static levenshteinDistance(a, b) {
  202. if (a.length === 0) return b.length;
  203. if (b.length === 0) return a.length;
  204. var matrix = [];
  205. // increment along the first column of each row
  206. var i;
  207. for (i = 0; i <= b.length; i++) {
  208. matrix[i] = [i];
  209. }
  210. // increment each column in the first row
  211. var j;
  212. for (j = 0; j <= a.length; j++) {
  213. matrix[0][j] = j;
  214. }
  215. // Fill in the rest of the matrix
  216. for (i = 1; i <= b.length; i++) {
  217. for (j = 1; j <= a.length; j++) {
  218. if (b.charAt(i - 1) == a.charAt(j - 1)) {
  219. matrix[i][j] = matrix[i - 1][j - 1];
  220. } else {
  221. matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
  222. Math.min(matrix[i][j - 1] + 1, // insertion
  223. matrix[i - 1][j] + 1)); // deletion
  224. }
  225. }
  226. }
  227. return matrix[b.length][a.length];
  228. }
  229. ;
  230. }
  231. export default Validator;
  232. export {printStyle}