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.

202 lines
3.9 KiB

4 years ago
  1. {{## def.setupKeyword:
  2. {{
  3. var $lvl = it.level;
  4. var $dataLvl = it.dataLevel;
  5. var $schema = it.schema[$keyword];
  6. var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
  7. var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
  8. var $breakOnError = !it.opts.allErrors;
  9. var $errorKeyword;
  10. var $data = 'data' + ($dataLvl || '');
  11. var $valid = 'valid' + $lvl;
  12. var $errs = 'errs__' + $lvl;
  13. }}
  14. #}}
  15. {{## def.setCompositeRule:
  16. {{
  17. var $wasComposite = it.compositeRule;
  18. it.compositeRule = $it.compositeRule = true;
  19. }}
  20. #}}
  21. {{## def.resetCompositeRule:
  22. {{ it.compositeRule = $it.compositeRule = $wasComposite; }}
  23. #}}
  24. {{## def.setupNextLevel:
  25. {{
  26. var $it = it.util.copy(it);
  27. var $closingBraces = '';
  28. $it.level++;
  29. var $nextValid = 'valid' + $it.level;
  30. }}
  31. #}}
  32. {{## def.ifValid:
  33. {{? $breakOnError }}
  34. if ({{=$valid}}) {
  35. {{ $closingBraces += '}'; }}
  36. {{?}}
  37. #}}
  38. {{## def.ifResultValid:
  39. {{? $breakOnError }}
  40. if ({{=$nextValid}}) {
  41. {{ $closingBraces += '}'; }}
  42. {{?}}
  43. #}}
  44. {{## def.elseIfValid:
  45. {{? $breakOnError }}
  46. {{ $closingBraces += '}'; }}
  47. else {
  48. {{?}}
  49. #}}
  50. {{## def.nonEmptySchema:_schema:
  51. (it.opts.strictKeywords
  52. ? typeof _schema == 'object' && Object.keys(_schema).length > 0
  53. : it.util.schemaHasRules(_schema, it.RULES.all))
  54. #}}
  55. {{## def.strLength:
  56. {{? it.opts.unicode === false }}
  57. {{=$data}}.length
  58. {{??}}
  59. ucs2length({{=$data}})
  60. {{?}}
  61. #}}
  62. {{## def.willOptimize:
  63. it.util.varOccurences($code, $nextData) < 2
  64. #}}
  65. {{## def.generateSubschemaCode:
  66. {{
  67. var $code = it.validate($it);
  68. $it.baseId = $currentBaseId;
  69. }}
  70. #}}
  71. {{## def.insertSubschemaCode:
  72. {{= it.validate($it) }}
  73. {{ $it.baseId = $currentBaseId; }}
  74. #}}
  75. {{## def._optimizeValidate:
  76. it.util.varReplace($code, $nextData, $passData)
  77. #}}
  78. {{## def.optimizeValidate:
  79. {{? {{# def.willOptimize}} }}
  80. {{= {{# def._optimizeValidate }} }}
  81. {{??}}
  82. var {{=$nextData}} = {{=$passData}};
  83. {{= $code }}
  84. {{?}}
  85. #}}
  86. {{## def.$data:
  87. {{
  88. var $isData = it.opts.$data && $schema && $schema.$data
  89. , $schemaValue;
  90. }}
  91. {{? $isData }}
  92. var schema{{=$lvl}} = {{= it.util.getData($schema.$data, $dataLvl, it.dataPathArr) }};
  93. {{ $schemaValue = 'schema' + $lvl; }}
  94. {{??}}
  95. {{ $schemaValue = $schema; }}
  96. {{?}}
  97. #}}
  98. {{## def.$dataNotType:_type:
  99. {{?$isData}} ({{=$schemaValue}} !== undefined && typeof {{=$schemaValue}} != _type) || {{?}}
  100. #}}
  101. {{## def.check$dataIsArray:
  102. if (schema{{=$lvl}} === undefined) {{=$valid}} = true;
  103. else if (!Array.isArray(schema{{=$lvl}})) {{=$valid}} = false;
  104. else {
  105. #}}
  106. {{## def.numberKeyword:
  107. {{? !($isData || typeof $schema == 'number') }}
  108. {{ throw new Error($keyword + ' must be number'); }}
  109. {{?}}
  110. #}}
  111. {{## def.beginDefOut:
  112. {{
  113. var $$outStack = $$outStack || [];
  114. $$outStack.push(out);
  115. out = '';
  116. }}
  117. #}}
  118. {{## def.storeDefOut:_variable:
  119. {{
  120. var _variable = out;
  121. out = $$outStack.pop();
  122. }}
  123. #}}
  124. {{## def.dataPath:(dataPath || ''){{? it.errorPath != '""'}} + {{= it.errorPath }}{{?}}#}}
  125. {{## def.setParentData:
  126. {{
  127. var $parentData = $dataLvl ? 'data' + (($dataLvl-1)||'') : 'parentData'
  128. , $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
  129. }}
  130. #}}
  131. {{## def.passParentData:
  132. {{# def.setParentData }}
  133. , {{= $parentData }}
  134. , {{= $parentDataProperty }}
  135. #}}
  136. {{## def.iterateProperties:
  137. {{? $ownProperties }}
  138. {{=$dataProperties}} = {{=$dataProperties}} || Object.keys({{=$data}});
  139. for (var {{=$idx}}=0; {{=$idx}}<{{=$dataProperties}}.length; {{=$idx}}++) {
  140. var {{=$key}} = {{=$dataProperties}}[{{=$idx}}];
  141. {{??}}
  142. for (var {{=$key}} in {{=$data}}) {
  143. {{?}}
  144. #}}
  145. {{## def.noPropertyInData:
  146. {{=$useData}} === undefined
  147. {{? $ownProperties }}
  148. || !{{# def.isOwnProperty }}
  149. {{?}}
  150. #}}
  151. {{## def.isOwnProperty:
  152. Object.prototype.hasOwnProperty.call({{=$data}}, '{{=it.util.escapeQuotes($propertyKey)}}')
  153. #}}