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.

293 lines
10 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 allOptions;
  4. let printStyle = 'background: #FFeeee; color: #dd0000';
  5. /**
  6. * Used to validate options.
  7. */
  8. class Validator {
  9. constructor() {
  10. }
  11. /**
  12. * Main function to be called
  13. * @param options
  14. * @param subObject
  15. * @returns {boolean}
  16. */
  17. static validate(options, referenceOptions, subObject) {
  18. errorFound = false;
  19. allOptions = referenceOptions;
  20. let usedOptions = referenceOptions;
  21. if (subObject !== undefined) {
  22. usedOptions = referenceOptions[subObject];
  23. }
  24. Validator.parse(options, usedOptions, []);
  25. return errorFound;
  26. }
  27. /**
  28. * Will traverse an object recursively and check every value
  29. * @param options
  30. * @param referenceOptions
  31. * @param path
  32. */
  33. static parse(options, referenceOptions, path) {
  34. for (let option in options) {
  35. if (options.hasOwnProperty(option)) {
  36. Validator.check(option, options, referenceOptions, path);
  37. }
  38. }
  39. }
  40. /**
  41. * Check every value. If the value is an object, call the parse function on that object.
  42. * @param option
  43. * @param options
  44. * @param referenceOptions
  45. * @param path
  46. */
  47. static check(option, options, referenceOptions, path) {
  48. if (referenceOptions[option] === undefined && referenceOptions.__any__ === undefined) {
  49. Validator.getSuggestion(option, referenceOptions, path);
  50. }
  51. else if (referenceOptions[option] === undefined && referenceOptions.__any__ !== undefined) {
  52. // __any__ is a wildcard. Any value is accepted and will be further analysed by reference.
  53. if (Validator.getType(options[option]) === 'object') {
  54. Validator.checkFields(option, options, referenceOptions, '__any__', referenceOptions['__any__'].__type__, path);
  55. }
  56. }
  57. else {
  58. // Since all options in the reference are objects, we can check whether they are supposed to be object to look for the __type__ field.
  59. if (referenceOptions[option].__type__ !== undefined) {
  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. path = util.copyAndExtendArray(path, option);
  90. Validator.parse(options[option], referenceOptions[referenceOption], path);
  91. }
  92. }
  93. else if (optionType === 'object') {
  94. path = util.copyAndExtendArray(path, option);
  95. Validator.parse(options[option], referenceOptions[referenceOption], path);
  96. }
  97. }
  98. else {
  99. if (refOptionObj['undef'] !== undefined && optionType === 'undefined') {
  100. // item is undefined, which is allowed
  101. }
  102. else if (refOptionObj['fn'] !== undefined && optionType === 'function') {
  103. // item is a function, which is allowed
  104. }
  105. else {
  106. // type of the field is incorrect
  107. console.log('%cInvalid type received for "' + option + '". Expected: ' + Validator.print(Object.keys(refOptionObj)) + '. Received [' + optionType + '] "' + options[option] + '"' + Validator.printLocation(path, option), printStyle);
  108. errorFound = true;
  109. }
  110. }
  111. }
  112. static getType(object) {
  113. var type = typeof object;
  114. if (type === 'object') {
  115. if (object === null) {
  116. return 'null';
  117. }
  118. if (object instanceof Boolean) {
  119. return 'boolean';
  120. }
  121. if (object instanceof Number) {
  122. return 'number';
  123. }
  124. if (object instanceof String) {
  125. return 'string';
  126. }
  127. if (Array.isArray(object)) {
  128. return 'array';
  129. }
  130. if (object instanceof Date) {
  131. return 'date';
  132. }
  133. if (object.nodeType !== undefined) {
  134. return 'dom'
  135. }
  136. return 'object';
  137. }
  138. else if (type === 'number') {
  139. return 'number';
  140. }
  141. else if (type === 'boolean') {
  142. return 'boolean';
  143. }
  144. else if (type === 'string') {
  145. return 'string';
  146. }
  147. else if (type === undefined) {
  148. return 'undefined';
  149. }
  150. return type;
  151. }
  152. static getSuggestion(option, options, path) {
  153. let localSearch = Validator.findInOptions(option,options,path,false);
  154. let globalSearch = Validator.findInOptions(option,allOptions,[],true);
  155. let localSearchThreshold = 8;
  156. let globalSearchThreshold = 5;
  157. if (globalSearch.distance <= globalSearchThreshold && localSearch.distance > globalSearch.distance) {
  158. console.log('%cUnknown option detected: "' + option + '" in ' + Validator.printLocation(localSearch.path, option,'') + 'Perhaps it was misplaced? Matching option found at: ' + Validator.printLocation(globalSearch.path, option,''), printStyle);
  159. }
  160. else if (localSearch.distance <= localSearchThreshold) {
  161. console.log('%cUnknown option detected: "' + option + '". Did you mean "' + localSearch.closestMatch + '"?' + Validator.printLocation(localSearch.path, option), printStyle);
  162. }
  163. else {
  164. console.log('%cUnknown option detected: "' + option + '". Did you mean one of these: ' + Validator.print(Object.keys(options)) + Validator.printLocation(path, option), printStyle);
  165. }
  166. errorFound = true;
  167. }
  168. /**
  169. * traverse the options in search for a match.
  170. * @param option
  171. * @param options
  172. * @param path
  173. * @param recursive
  174. * @returns {{closestMatch: string, path: Array, distance: number}}
  175. */
  176. static findInOptions(option, options, path, recursive = false) {
  177. let min = 1e9;
  178. let closestMatch = '';
  179. let closestMatchPath = [];
  180. for (let op in options) {
  181. let type = Validator.getType(options[op]);
  182. let distance;
  183. if (type === 'object' && recursive === true) {
  184. let result = Validator.findInOptions(option, options[op], util.copyAndExtendArray(path,op));
  185. if (min > result.distance) {
  186. closestMatch = result.closestMatch;
  187. closestMatchPath = result.path;
  188. min = result.distance;
  189. }
  190. }
  191. else {
  192. distance = Validator.levenshteinDistance(option, op);
  193. if (min > distance) {
  194. closestMatch = op;
  195. closestMatchPath = util.copyArray(path);
  196. min = distance;
  197. }
  198. }
  199. }
  200. return {closestMatch:closestMatch, path:closestMatchPath, distance:min}
  201. }
  202. static printLocation(path, option, prefix = 'Problem value found at: \n') {
  203. let str = '\n\n' + prefix + 'options = {\n';
  204. for (let i = 0; i < path.length; i++) {
  205. for (let j = 0; j < i + 1; j++) {
  206. str += ' ';
  207. }
  208. str += path[i] + ': {\n'
  209. }
  210. for (let j = 0; j < path.length + 1; j++) {
  211. str += ' ';
  212. }
  213. str += option + '\n';
  214. for (let i = 0; i < path.length + 1; i++) {
  215. for (let j = 0; j < path.length - i; j++) {
  216. str += ' ';
  217. }
  218. str += '}\n'
  219. }
  220. return str + '\n\n';
  221. }
  222. static print(options) {
  223. return JSON.stringify(options).replace(/(\")|(\[)|(\])|(,"__type__")/g, "").replace(/(\,)/g, ', ')
  224. }
  225. // Compute the edit distance between the two given strings
  226. // http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript
  227. /*
  228. Copyright (c) 2011 Andrei Mackenzie
  229. 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:
  230. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  231. 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.
  232. */
  233. static levenshteinDistance(a, b) {
  234. if (a.length === 0) return b.length;
  235. if (b.length === 0) return a.length;
  236. var matrix = [];
  237. // increment along the first column of each row
  238. var i;
  239. for (i = 0; i <= b.length; i++) {
  240. matrix[i] = [i];
  241. }
  242. // increment each column in the first row
  243. var j;
  244. for (j = 0; j <= a.length; j++) {
  245. matrix[0][j] = j;
  246. }
  247. // Fill in the rest of the matrix
  248. for (i = 1; i <= b.length; i++) {
  249. for (j = 1; j <= a.length; j++) {
  250. if (b.charAt(i - 1) == a.charAt(j - 1)) {
  251. matrix[i][j] = matrix[i - 1][j - 1];
  252. } else {
  253. matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
  254. Math.min(matrix[i][j - 1] + 1, // insertion
  255. matrix[i - 1][j] + 1)); // deletion
  256. }
  257. }
  258. }
  259. return matrix[b.length][a.length];
  260. }
  261. ;
  262. }
  263. export default Validator;
  264. export {printStyle}