Ohm-Management - Projektarbeit B-ME
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.

required.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. 'use strict';
  2. module.exports = function generate_required(it, $keyword, $ruleType) {
  3. var out = ' ';
  4. var $lvl = it.level;
  5. var $dataLvl = it.dataLevel;
  6. var $schema = it.schema[$keyword];
  7. var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
  8. var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
  9. var $breakOnError = !it.opts.allErrors;
  10. var $data = 'data' + ($dataLvl || '');
  11. var $valid = 'valid' + $lvl;
  12. var $isData = it.opts.$data && $schema && $schema.$data,
  13. $schemaValue;
  14. if ($isData) {
  15. out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
  16. $schemaValue = 'schema' + $lvl;
  17. } else {
  18. $schemaValue = $schema;
  19. }
  20. var $vSchema = 'schema' + $lvl;
  21. if (!$isData) {
  22. if ($schema.length < it.opts.loopRequired && it.schema.properties && Object.keys(it.schema.properties).length) {
  23. var $required = [];
  24. var arr1 = $schema;
  25. if (arr1) {
  26. var $property, i1 = -1,
  27. l1 = arr1.length - 1;
  28. while (i1 < l1) {
  29. $property = arr1[i1 += 1];
  30. var $propertySch = it.schema.properties[$property];
  31. if (!($propertySch && it.util.schemaHasRules($propertySch, it.RULES.all))) {
  32. $required[$required.length] = $property;
  33. }
  34. }
  35. }
  36. } else {
  37. var $required = $schema;
  38. }
  39. }
  40. if ($isData || $required.length) {
  41. var $currentErrorPath = it.errorPath,
  42. $loopRequired = $isData || $required.length >= it.opts.loopRequired,
  43. $ownProperties = it.opts.ownProperties;
  44. if ($breakOnError) {
  45. out += ' var missing' + ($lvl) + '; ';
  46. if ($loopRequired) {
  47. if (!$isData) {
  48. out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
  49. }
  50. var $i = 'i' + $lvl,
  51. $propertyPath = 'schema' + $lvl + '[' + $i + ']',
  52. $missingProperty = '\' + ' + $propertyPath + ' + \'';
  53. if (it.opts._errorDataPathProperty) {
  54. it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
  55. }
  56. out += ' var ' + ($valid) + ' = true; ';
  57. if ($isData) {
  58. out += ' if (schema' + ($lvl) + ' === undefined) ' + ($valid) + ' = true; else if (!Array.isArray(schema' + ($lvl) + ')) ' + ($valid) + ' = false; else {';
  59. }
  60. out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { ' + ($valid) + ' = ' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] !== undefined ';
  61. if ($ownProperties) {
  62. out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + ']) ';
  63. }
  64. out += '; if (!' + ($valid) + ') break; } ';
  65. if ($isData) {
  66. out += ' } ';
  67. }
  68. out += ' if (!' + ($valid) + ') { ';
  69. var $$outStack = $$outStack || [];
  70. $$outStack.push(out);
  71. out = ''; /* istanbul ignore else */
  72. if (it.createErrors !== false) {
  73. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  74. if (it.opts.messages !== false) {
  75. out += ' , message: \'';
  76. if (it.opts._errorDataPathProperty) {
  77. out += 'is a required property';
  78. } else {
  79. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  80. }
  81. out += '\' ';
  82. }
  83. if (it.opts.verbose) {
  84. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  85. }
  86. out += ' } ';
  87. } else {
  88. out += ' {} ';
  89. }
  90. var __err = out;
  91. out = $$outStack.pop();
  92. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  93. if (it.async) {
  94. out += ' throw new ValidationError([' + (__err) + ']); ';
  95. } else {
  96. out += ' validate.errors = [' + (__err) + ']; return false; ';
  97. }
  98. } else {
  99. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  100. }
  101. out += ' } else { ';
  102. } else {
  103. out += ' if ( ';
  104. var arr2 = $required;
  105. if (arr2) {
  106. var $propertyKey, $i = -1,
  107. l2 = arr2.length - 1;
  108. while ($i < l2) {
  109. $propertyKey = arr2[$i += 1];
  110. if ($i) {
  111. out += ' || ';
  112. }
  113. var $prop = it.util.getProperty($propertyKey),
  114. $useData = $data + $prop;
  115. out += ' ( ( ' + ($useData) + ' === undefined ';
  116. if ($ownProperties) {
  117. out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
  118. }
  119. out += ') && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? $propertyKey : $prop)) + ') ) ';
  120. }
  121. }
  122. out += ') { ';
  123. var $propertyPath = 'missing' + $lvl,
  124. $missingProperty = '\' + ' + $propertyPath + ' + \'';
  125. if (it.opts._errorDataPathProperty) {
  126. it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
  127. }
  128. var $$outStack = $$outStack || [];
  129. $$outStack.push(out);
  130. out = ''; /* istanbul ignore else */
  131. if (it.createErrors !== false) {
  132. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  133. if (it.opts.messages !== false) {
  134. out += ' , message: \'';
  135. if (it.opts._errorDataPathProperty) {
  136. out += 'is a required property';
  137. } else {
  138. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  139. }
  140. out += '\' ';
  141. }
  142. if (it.opts.verbose) {
  143. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  144. }
  145. out += ' } ';
  146. } else {
  147. out += ' {} ';
  148. }
  149. var __err = out;
  150. out = $$outStack.pop();
  151. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  152. if (it.async) {
  153. out += ' throw new ValidationError([' + (__err) + ']); ';
  154. } else {
  155. out += ' validate.errors = [' + (__err) + ']; return false; ';
  156. }
  157. } else {
  158. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  159. }
  160. out += ' } else { ';
  161. }
  162. } else {
  163. if ($loopRequired) {
  164. if (!$isData) {
  165. out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
  166. }
  167. var $i = 'i' + $lvl,
  168. $propertyPath = 'schema' + $lvl + '[' + $i + ']',
  169. $missingProperty = '\' + ' + $propertyPath + ' + \'';
  170. if (it.opts._errorDataPathProperty) {
  171. it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
  172. }
  173. if ($isData) {
  174. out += ' if (' + ($vSchema) + ' && !Array.isArray(' + ($vSchema) + ')) { var err = '; /* istanbul ignore else */
  175. if (it.createErrors !== false) {
  176. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  177. if (it.opts.messages !== false) {
  178. out += ' , message: \'';
  179. if (it.opts._errorDataPathProperty) {
  180. out += 'is a required property';
  181. } else {
  182. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  183. }
  184. out += '\' ';
  185. }
  186. if (it.opts.verbose) {
  187. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  188. }
  189. out += ' } ';
  190. } else {
  191. out += ' {} ';
  192. }
  193. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else if (' + ($vSchema) + ' !== undefined) { ';
  194. }
  195. out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { if (' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] === undefined ';
  196. if ($ownProperties) {
  197. out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + ']) ';
  198. }
  199. out += ') { var err = '; /* istanbul ignore else */
  200. if (it.createErrors !== false) {
  201. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  202. if (it.opts.messages !== false) {
  203. out += ' , message: \'';
  204. if (it.opts._errorDataPathProperty) {
  205. out += 'is a required property';
  206. } else {
  207. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  208. }
  209. out += '\' ';
  210. }
  211. if (it.opts.verbose) {
  212. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  213. }
  214. out += ' } ';
  215. } else {
  216. out += ' {} ';
  217. }
  218. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } ';
  219. if ($isData) {
  220. out += ' } ';
  221. }
  222. } else {
  223. var arr3 = $required;
  224. if (arr3) {
  225. var $propertyKey, i3 = -1,
  226. l3 = arr3.length - 1;
  227. while (i3 < l3) {
  228. $propertyKey = arr3[i3 += 1];
  229. var $prop = it.util.getProperty($propertyKey),
  230. $missingProperty = it.util.escapeQuotes($propertyKey),
  231. $useData = $data + $prop;
  232. if (it.opts._errorDataPathProperty) {
  233. it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
  234. }
  235. out += ' if ( ' + ($useData) + ' === undefined ';
  236. if ($ownProperties) {
  237. out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
  238. }
  239. out += ') { var err = '; /* istanbul ignore else */
  240. if (it.createErrors !== false) {
  241. out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
  242. if (it.opts.messages !== false) {
  243. out += ' , message: \'';
  244. if (it.opts._errorDataPathProperty) {
  245. out += 'is a required property';
  246. } else {
  247. out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
  248. }
  249. out += '\' ';
  250. }
  251. if (it.opts.verbose) {
  252. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  253. }
  254. out += ' } ';
  255. } else {
  256. out += ' {} ';
  257. }
  258. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
  259. }
  260. }
  261. }
  262. }
  263. it.errorPath = $currentErrorPath;
  264. } else if ($breakOnError) {
  265. out += ' if (true) {';
  266. }
  267. return out;
  268. }