definitions.def 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.util.schemaHasRules(_schema, it.RULES.all)
  52. #}}
  53. {{## def.strLength:
  54. {{? it.opts.unicode === false }}
  55. {{=$data}}.length
  56. {{??}}
  57. ucs2length({{=$data}})
  58. {{?}}
  59. #}}
  60. {{## def.willOptimize:
  61. it.util.varOccurences($code, $nextData) < 2
  62. #}}
  63. {{## def.generateSubschemaCode:
  64. {{
  65. var $code = it.validate($it);
  66. $it.baseId = $currentBaseId;
  67. }}
  68. #}}
  69. {{## def.insertSubschemaCode:
  70. {{= it.validate($it) }}
  71. {{ $it.baseId = $currentBaseId; }}
  72. #}}
  73. {{## def._optimizeValidate:
  74. it.util.varReplace($code, $nextData, $passData)
  75. #}}
  76. {{## def.optimizeValidate:
  77. {{? {{# def.willOptimize}} }}
  78. {{= {{# def._optimizeValidate }} }}
  79. {{??}}
  80. var {{=$nextData}} = {{=$passData}};
  81. {{= $code }}
  82. {{?}}
  83. #}}
  84. {{## def.cleanUp: {{ out = it.util.cleanUpCode(out); }} #}}
  85. {{## def.finalCleanUp: {{ out = it.util.finalCleanUpCode(out, $async); }} #}}
  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.beginDefOut:
  107. {{
  108. var $$outStack = $$outStack || [];
  109. $$outStack.push(out);
  110. out = '';
  111. }}
  112. #}}
  113. {{## def.storeDefOut:_variable:
  114. {{
  115. var _variable = out;
  116. out = $$outStack.pop();
  117. }}
  118. #}}
  119. {{## def.dataPath:(dataPath || ''){{? it.errorPath != '""'}} + {{= it.errorPath }}{{?}}#}}
  120. {{## def.setParentData:
  121. {{
  122. var $parentData = $dataLvl ? 'data' + (($dataLvl-1)||'') : 'parentData'
  123. , $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
  124. }}
  125. #}}
  126. {{## def.passParentData:
  127. {{# def.setParentData }}
  128. , {{= $parentData }}
  129. , {{= $parentDataProperty }}
  130. #}}
  131. {{## def.iterateProperties:
  132. {{? $ownProperties }}
  133. {{=$dataProperties}} = {{=$dataProperties}} || Object.keys({{=$data}});
  134. for (var {{=$idx}}=0; {{=$idx}}<{{=$dataProperties}}.length; {{=$idx}}++) {
  135. var {{=$key}} = {{=$dataProperties}}[{{=$idx}}];
  136. {{??}}
  137. for (var {{=$key}} in {{=$data}}) {
  138. {{?}}
  139. #}}
  140. {{## def.noPropertyInData:
  141. {{=$useData}} === undefined
  142. {{? $ownProperties }}
  143. || !{{# def.isOwnProperty }}
  144. {{?}}
  145. #}}
  146. {{## def.isOwnProperty:
  147. Object.prototype.hasOwnProperty.call({{=$data}}, '{{=it.util.escapeQuotes($propertyKey)}}')
  148. #}}