required.jst 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. {{# def.definitions }}
  2. {{# def.errors }}
  3. {{# def.missing }}
  4. {{# def.setupKeyword }}
  5. {{# def.$data }}
  6. {{ var $vSchema = 'schema' + $lvl; }}
  7. {{## def.setupLoop:
  8. {{? !$isData }}
  9. var {{=$vSchema}} = validate.schema{{=$schemaPath}};
  10. {{?}}
  11. {{
  12. var $i = 'i' + $lvl
  13. , $propertyPath = 'schema' + $lvl + '[' + $i + ']'
  14. , $missingProperty = '\' + ' + $propertyPath + ' + \'';
  15. if (it.opts._errorDataPathProperty) {
  16. it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
  17. }
  18. }}
  19. #}}
  20. {{? !$isData }}
  21. {{? $schema.length < it.opts.loopRequired &&
  22. it.schema.properties && Object.keys(it.schema.properties).length }}
  23. {{ var $required = []; }}
  24. {{~ $schema:$property }}
  25. {{ var $propertySch = it.schema.properties[$property]; }}
  26. {{? !($propertySch && {{# def.nonEmptySchema:$propertySch}}) }}
  27. {{ $required[$required.length] = $property; }}
  28. {{?}}
  29. {{~}}
  30. {{??}}
  31. {{ var $required = $schema; }}
  32. {{?}}
  33. {{?}}
  34. {{? $isData || $required.length }}
  35. {{
  36. var $currentErrorPath = it.errorPath
  37. , $loopRequired = $isData || $required.length >= it.opts.loopRequired;
  38. }}
  39. {{? $breakOnError }}
  40. var missing{{=$lvl}};
  41. {{? $loopRequired }}
  42. {{# def.setupLoop }}
  43. var {{=$valid}} = true;
  44. {{?$isData}}{{# def.check$dataIsArray }}{{?}}
  45. for (var {{=$i}} = 0; {{=$i}} < {{=$vSchema}}.length; {{=$i}}++) {
  46. {{=$valid}} = {{=$data}}[{{=$vSchema}}[{{=$i}}]] !== undefined;
  47. if (!{{=$valid}}) break;
  48. }
  49. {{? $isData }} } {{?}}
  50. {{# def.checkError:'required' }}
  51. else {
  52. {{??}}
  53. if ({{# def.checkMissingProperty:$required }}) {
  54. {{# def.errorMissingProperty:'required' }}
  55. } else {
  56. {{?}}
  57. {{??}}
  58. {{? $loopRequired }}
  59. {{# def.setupLoop }}
  60. {{? $isData }}
  61. if ({{=$vSchema}} && !Array.isArray({{=$vSchema}})) {
  62. {{# def.addError:'required' }}
  63. } else if ({{=$vSchema}} !== undefined) {
  64. {{?}}
  65. for (var {{=$i}} = 0; {{=$i}} < {{=$vSchema}}.length; {{=$i}}++) {
  66. if ({{=$data}}[{{=$vSchema}}[{{=$i}}]] === undefined) {
  67. {{# def.addError:'required' }}
  68. }
  69. }
  70. {{? $isData }} } {{?}}
  71. {{??}}
  72. {{~ $required:$reqProperty }}
  73. {{# def.allErrorsMissingProperty:'required' }}
  74. {{~}}
  75. {{?}}
  76. {{?}}
  77. {{ it.errorPath = $currentErrorPath; }}
  78. {{?? $breakOnError }}
  79. if (true) {
  80. {{?}}