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.

253 lines
8.6 KiB

  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, true);
  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, true);
  62. }
  63. else {
  64. Validator.checkFields(option, options, referenceOptions, option, referenceOptions[option], path, false);
  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 path
  76. * @param recursive
  77. */
  78. static checkFields(option, options, referenceOptions, referenceOption, refOptionObj, path, recursive) {
  79. let optionType = Validator.getType(options[option]);
  80. let refOptionType = refOptionObj[optionType];
  81. if (refOptionType !== undefined) {
  82. // if the type is correct, we check if it is supposed to be one of a few select values
  83. if (Validator.getType(refOptionType) === 'array') {
  84. if (refOptionType.indexOf(options[option]) === -1) {
  85. console.log('%cInvalid option detected in "' + option + '".' +
  86. ' Allowed values are:' + Validator.print(refOptionType) + ' not "' + options[option] + '". ' + Validator.printLocation(path, option), printStyle);
  87. errorFound = true;
  88. }
  89. else if (recursive === true) {
  90. Validator.parse(options[option], referenceOptions[referenceOption], path);
  91. }
  92. }
  93. else if (recursive === true) {
  94. Validator.parse(options[option], referenceOptions[referenceOption], path);
  95. }
  96. }
  97. else {
  98. // type of the field is incorrect
  99. console.log('%cInvalid type received for "' + option + '". Expected: ' + Validator.print(Object.keys(referenceOptions[referenceOption].__type__)) + '. Received [' + optionType + '] "' + options[option] + '"' + Validator.printLocation(path, option), printStyle);
  100. errorFound = true;
  101. }
  102. }
  103. static getType(object) {
  104. var type = typeof object;
  105. if (type === 'object') {
  106. if (object === null) {
  107. return 'null';
  108. }
  109. if (object instanceof Boolean) {
  110. return 'boolean';
  111. }
  112. if (object instanceof Number) {
  113. return 'number';
  114. }
  115. if (object instanceof String) {
  116. return 'string';
  117. }
  118. if (Array.isArray(object)) {
  119. return 'array';
  120. }
  121. if (object instanceof Date) {
  122. return 'date';
  123. }
  124. if (object.nodeType !== undefined) {
  125. return 'dom'
  126. }
  127. return 'object';
  128. }
  129. else if (type === 'number') {
  130. return 'number';
  131. }
  132. else if (type === 'boolean') {
  133. return 'boolean';
  134. }
  135. else if (type === 'string') {
  136. return 'string';
  137. }
  138. else if (type === undefined) {
  139. return 'undefined';
  140. }
  141. return type;
  142. }
  143. static getSuggestion(option, options, path) {
  144. let closestMatch = '';
  145. let min = 1e9;
  146. let threshold = 10;
  147. for (let op in options) {
  148. let distance = Validator.levenshteinDistance(option, op);
  149. if (min > distance && distance < threshold) {
  150. closestMatch = op;
  151. min = distance;
  152. }
  153. }
  154. if (min < threshold) {
  155. console.log('%cUnknown option detected: "' + option + '". Did you mean "' + closestMatch + '"?' + Validator.printLocation(path, option), printStyle);
  156. }
  157. else {
  158. console.log('%cUnknown option detected: "' + option + '". Did you mean one of these: ' + Validator.print(Object.keys(options)) + Validator.printLocation(path, option), printStyle);
  159. }
  160. errorFound = true;
  161. return closestMatch;
  162. }
  163. static printLocation(path, option) {
  164. let str = '\n\nProblem value found at: \noptions = {\n';
  165. for (let i = 0; i < path.length; i++) {
  166. for (let j = 0; j < i + 1; j++) {
  167. str += ' ';
  168. }
  169. str += path[i] + ': {\n'
  170. }
  171. for (let j = 0; j < path.length + 1; j++) {
  172. str += ' ';
  173. }
  174. str += option + '\n';
  175. for (let i = 0; i < path.length + 1; i++) {
  176. for (let j = 0; j < path.length - i; j++) {
  177. str += ' ';
  178. }
  179. str += '}\n'
  180. }
  181. return str + '\n\n';
  182. }
  183. static print(options) {
  184. return JSON.stringify(options).replace(/(\")|(\[)|(\])|(,"__type__")/g, "").replace(/(\,)/g, ', ')
  185. }
  186. // Compute the edit distance between the two given strings
  187. // http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript
  188. /*
  189. Copyright (c) 2011 Andrei Mackenzie
  190. 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:
  191. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  192. 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.
  193. */
  194. static levenshteinDistance(a, b) {
  195. if (a.length === 0) return b.length;
  196. if (b.length === 0) return a.length;
  197. var matrix = [];
  198. // increment along the first column of each row
  199. var i;
  200. for (i = 0; i <= b.length; i++) {
  201. matrix[i] = [i];
  202. }
  203. // increment each column in the first row
  204. var j;
  205. for (j = 0; j <= a.length; j++) {
  206. matrix[0][j] = j;
  207. }
  208. // Fill in the rest of the matrix
  209. for (i = 1; i <= b.length; i++) {
  210. for (j = 1; j <= a.length; j++) {
  211. if (b.charAt(i - 1) == a.charAt(j - 1)) {
  212. matrix[i][j] = matrix[i - 1][j - 1];
  213. } else {
  214. matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
  215. Math.min(matrix[i][j - 1] + 1, // insertion
  216. matrix[i - 1][j] + 1)); // deletion
  217. }
  218. }
  219. }
  220. return matrix[b.length][a.length];
  221. }
  222. ;
  223. }
  224. export default Validator;
  225. export {printStyle}