definitions.def 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. || _schema === false
  54. : it.util.schemaHasRules(_schema, it.RULES.all))
  55. #}}
  56. {{## def.strLength:
  57. {{? it.opts.unicode === false }}
  58. {{=$data}}.length
  59. {{??}}
  60. ucs2length({{=$data}})
  61. {{?}}
  62. #}}
  63. {{## def.willOptimize:
  64. it.util.varOccurences($code, $nextData) < 2
  65. #}}
  66. {{## def.generateSubschemaCode:
  67. {{
  68. var $code = it.validate($it);
  69. $it.baseId = $currentBaseId;
  70. }}
  71. #}}
  72. {{## def.insertSubschemaCode:
  73. {{= it.validate($it) }}
  74. {{ $it.baseId = $currentBaseId; }}
  75. #}}
  76. {{## def._optimizeValidate:
  77. it.util.varReplace($code, $nextData, $passData)
  78. #}}
  79. {{## def.optimizeValidate:
  80. {{? {{# def.willOptimize}} }}
  81. {{= {{# def._optimizeValidate }} }}
  82. {{??}}
  83. var {{=$nextData}} = {{=$passData}};
  84. {{= $code }}
  85. {{?}}
  86. #}}
  87. {{## def.$data:
  88. {{
  89. var $isData = it.opts.$data && $schema && $schema.$data
  90. , $schemaValue;
  91. }}
  92. {{? $isData }}
  93. var schema{{=$lvl}} = {{= it.util.getData($schema.$data, $dataLvl, it.dataPathArr) }};
  94. {{ $schemaValue = 'schema' + $lvl; }}
  95. {{??}}
  96. {{ $schemaValue = $schema; }}
  97. {{?}}
  98. #}}
  99. {{## def.$dataNotType:_type:
  100. {{?$isData}} ({{=$schemaValue}} !== undefined && typeof {{=$schemaValue}} != _type) || {{?}}
  101. #}}
  102. {{## def.check$dataIsArray:
  103. if (schema{{=$lvl}} === undefined) {{=$valid}} = true;
  104. else if (!Array.isArray(schema{{=$lvl}})) {{=$valid}} = false;
  105. else {
  106. #}}
  107. {{## def.numberKeyword:
  108. {{? !($isData || typeof $schema == 'number') }}
  109. {{ throw new Error($keyword + ' must be number'); }}
  110. {{?}}
  111. #}}
  112. {{## def.beginDefOut:
  113. {{
  114. var $$outStack = $$outStack || [];
  115. $$outStack.push(out);
  116. out = '';
  117. }}
  118. #}}
  119. {{## def.storeDefOut:_variable:
  120. {{
  121. var _variable = out;
  122. out = $$outStack.pop();
  123. }}
  124. #}}
  125. {{## def.dataPath:(dataPath || ''){{? it.errorPath != '""'}} + {{= it.errorPath }}{{?}}#}}
  126. {{## def.setParentData:
  127. {{
  128. var $parentData = $dataLvl ? 'data' + (($dataLvl-1)||'') : 'parentData'
  129. , $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
  130. }}
  131. #}}
  132. {{## def.passParentData:
  133. {{# def.setParentData }}
  134. , {{= $parentData }}
  135. , {{= $parentDataProperty }}
  136. #}}
  137. {{## def.iterateProperties:
  138. {{? $ownProperties }}
  139. {{=$dataProperties}} = {{=$dataProperties}} || Object.keys({{=$data}});
  140. for (var {{=$idx}}=0; {{=$idx}}<{{=$dataProperties}}.length; {{=$idx}}++) {
  141. var {{=$key}} = {{=$dataProperties}}[{{=$idx}}];
  142. {{??}}
  143. for (var {{=$key}} in {{=$data}}) {
  144. {{?}}
  145. #}}
  146. {{## def.noPropertyInData:
  147. {{=$useData}} === undefined
  148. {{? $ownProperties }}
  149. || !{{# def.isOwnProperty }}
  150. {{?}}
  151. #}}
  152. {{## def.isOwnProperty:
  153. Object.prototype.hasOwnProperty.call({{=$data}}, '{{=it.util.escapeQuotes($propertyKey)}}')
  154. #}}