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.

297 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
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. if (object._isAMomentObject === true) {
  137. return 'moment';
  138. }
  139. return 'object';
  140. }
  141. else if (type === 'number') {
  142. return 'number';
  143. }
  144. else if (type === 'boolean') {
  145. return 'boolean';
  146. }
  147. else if (type === 'string') {
  148. return 'string';
  149. }
  150. else if (type === undefined) {
  151. return 'undefined';
  152. }
  153. return type;
  154. }
  155. static getSuggestion(option, options, path) {
  156. let localSearch = Validator.findInOptions(option,options,path,false);
  157. let globalSearch = Validator.findInOptions(option,allOptions,[],true);
  158. let localSearchThreshold = 8;
  159. let globalSearchThreshold = 4;
  160. if (globalSearch.distance <= globalSearchThreshold && localSearch.distance > globalSearch.distance) {
  161. console.log('%cUnknown option detected: "' + option + '" in ' + Validator.printLocation(localSearch.path, option,'') + 'Perhaps it was misplaced? Matching option found at: ' + Validator.printLocation(globalSearch.path, globalSearch.closestMatch,''), printStyle);
  162. }
  163. else if (localSearch.distance <= localSearchThreshold) {
  164. console.log('%cUnknown option detected: "' + option + '". Did you mean "' + localSearch.closestMatch + '"?' + Validator.printLocation(localSearch.path, option), printStyle);
  165. }
  166. else {
  167. console.log('%cUnknown option detected: "' + option + '". Did you mean one of these: ' + Validator.print(Object.keys(options)) + Validator.printLocation(path, option), printStyle);
  168. }
  169. errorFound = true;
  170. }
  171. /**
  172. * traverse the options in search for a match.
  173. * @param option
  174. * @param options
  175. * @param path
  176. * @param recursive
  177. * @returns {{closestMatch: string, path: Array, distance: number}}
  178. */
  179. static findInOptions(option, options, path, recursive = false) {
  180. //console.log(option, options, path)
  181. let min = 1e9;
  182. let closestMatch = '';
  183. let closestMatchPath = [];
  184. for (let op in options) {
  185. let type = Validator.getType(options[op]);
  186. let distance;
  187. if (type === 'object' && recursive === true) {
  188. let result = Validator.findInOptions(option, options[op], util.copyAndExtendArray(path,op));
  189. if (min > result.distance) {
  190. closestMatch = result.closestMatch;
  191. closestMatchPath = result.path;
  192. min = result.distance;
  193. }
  194. }
  195. else {
  196. distance = Validator.levenshteinDistance(option, op);
  197. if (min > distance) {
  198. closestMatch = op;
  199. closestMatchPath = util.copyArray(path);
  200. min = distance;
  201. }
  202. }
  203. }
  204. return {closestMatch:closestMatch, path:closestMatchPath, distance:min}
  205. }
  206. static printLocation(path, option, prefix = 'Problem value found at: \n') {
  207. let str = '\n\n' + prefix + 'options = {\n';
  208. for (let i = 0; i < path.length; i++) {
  209. for (let j = 0; j < i + 1; j++) {
  210. str += ' ';
  211. }
  212. str += path[i] + ': {\n'
  213. }
  214. for (let j = 0; j < path.length + 1; j++) {
  215. str += ' ';
  216. }
  217. str += option + '\n';
  218. for (let i = 0; i < path.length + 1; i++) {
  219. for (let j = 0; j < path.length - i; j++) {
  220. str += ' ';
  221. }
  222. str += '}\n'
  223. }
  224. return str + '\n\n';
  225. }
  226. static print(options) {
  227. return JSON.stringify(options).replace(/(\")|(\[)|(\])|(,"__type__")/g, "").replace(/(\,)/g, ', ')
  228. }
  229. // Compute the edit distance between the two given strings
  230. // http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript
  231. /*
  232. Copyright (c) 2011 Andrei Mackenzie
  233. 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:
  234. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  235. 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.
  236. */
  237. static levenshteinDistance(a, b) {
  238. if (a.length === 0) return b.length;
  239. if (b.length === 0) return a.length;
  240. var matrix = [];
  241. // increment along the first column of each row
  242. var i;
  243. for (i = 0; i <= b.length; i++) {
  244. matrix[i] = [i];
  245. }
  246. // increment each column in the first row
  247. var j;
  248. for (j = 0; j <= a.length; j++) {
  249. matrix[0][j] = j;
  250. }
  251. // Fill in the rest of the matrix
  252. for (i = 1; i <= b.length; i++) {
  253. for (j = 1; j <= a.length; j++) {
  254. if (b.charAt(i - 1) == a.charAt(j - 1)) {
  255. matrix[i][j] = matrix[i - 1][j - 1];
  256. } else {
  257. matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
  258. Math.min(matrix[i][j - 1] + 1, // insertion
  259. matrix[i - 1][j] + 1)); // deletion
  260. }
  261. }
  262. }
  263. return matrix[b.length][a.length];
  264. }
  265. ;
  266. }
  267. export default Validator;
  268. export {printStyle}