properties.jst 10.1 KB
Newer Older
nittaya chompoosan's avatar
nittaya chompoosan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
{{# def.definitions }}
{{# def.errors }}
{{# def.setupKeyword }}
{{# def.setupNextLevel }}


{{## def.validateAdditional:
  {{ /* additionalProperties is schema */
    $it.schema = $aProperties;
    $it.schemaPath = it.schemaPath + '.additionalProperties';
    $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
    $it.errorPath = it.opts._errorDataPathProperty
                    ? it.errorPath
                    : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
    var $passData = $data + '[' + $key + ']';
    $it.dataPathArr[$dataNxt] = $key;
  }}

  {{# def.generateSubschemaCode }}
  {{# def.optimizeValidate }}
#}}


{{
  var $key = 'key' + $lvl
    , $idx = 'idx' + $lvl
    , $dataNxt = $it.dataLevel = it.dataLevel + 1
    , $nextData = 'data' + $dataNxt
    , $dataProperties = 'dataProperties' + $lvl;

  var $schemaKeys = Object.keys($schema || {})
    , $pProperties = it.schema.patternProperties || {}
    , $pPropertyKeys = Object.keys($pProperties)
    , $aProperties = it.schema.additionalProperties
    , $someProperties = $schemaKeys.length || $pPropertyKeys.length
    , $noAdditional = $aProperties === false
    , $additionalIsSchema = typeof $aProperties == 'object'
                              && Object.keys($aProperties).length
    , $removeAdditional = it.opts.removeAdditional
    , $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional
    , $ownProperties = it.opts.ownProperties
    , $currentBaseId = it.baseId;

  var $required = it.schema.required;
  if ($required && !(it.opts.v5 && $required.$data) && $required.length < it.opts.loopRequired)
    var $requiredHash = it.util.toHash($required);

  if (it.opts.patternGroups) {
    var $pgProperties = it.schema.patternGroups || {}
      , $pgPropertyKeys = Object.keys($pgProperties);
  }
}}


var {{=$errs}} = errors;
var {{=$nextValid}} = true;
{{? $ownProperties }}
  var {{=$dataProperties}} = undefined;
{{?}}

{{? $checkAdditional }}
  {{# def.iterateProperties }}
    {{? $someProperties }}
      var isAdditional{{=$lvl}} = !(false
        {{? $schemaKeys.length }}
          {{? $schemaKeys.length > 5 }}
            || validate.schema{{=$schemaPath}}[{{=$key}}]
          {{??}}
            {{~ $schemaKeys:$propertyKey }}
              || {{=$key}} == {{= it.util.toQuotedString($propertyKey) }}
            {{~}}
          {{?}}
        {{?}}
        {{? $pPropertyKeys.length }}
          {{~ $pPropertyKeys:$pProperty:$i }}
            || {{= it.usePattern($pProperty) }}.test({{=$key}})
          {{~}}
        {{?}}
        {{? it.opts.patternGroups && $pgPropertyKeys.length }}
          {{~ $pgPropertyKeys:$pgProperty:$i }}
            || {{= it.usePattern($pgProperty) }}.test({{=$key}})
          {{~}}
        {{?}}
      );

      if (isAdditional{{=$lvl}}) {
    {{?}}
    {{? $removeAdditional == 'all' }}
      delete {{=$data}}[{{=$key}}];
    {{??}}
      {{
        var $currentErrorPath = it.errorPath;
        var $additionalProperty = '\' + ' + $key + ' + \'';
        if (it.opts._errorDataPathProperty) {
          it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
        }
      }}
      {{? $noAdditional }}
        {{? $removeAdditional }}
          delete {{=$data}}[{{=$key}}];
        {{??}}
          {{=$nextValid}} = false;
          {{
            var $currErrSchemaPath = $errSchemaPath;
            $errSchemaPath = it.errSchemaPath + '/additionalProperties';
          }}
          {{# def.error:'additionalProperties' }}
          {{ $errSchemaPath = $currErrSchemaPath; }}
          {{? $breakOnError }} break; {{?}}
        {{?}}
      {{?? $additionalIsSchema }}
        {{? $removeAdditional == 'failing' }}
          var {{=$errs}} = errors;
          {{# def.setCompositeRule }}

          {{# def.validateAdditional }}

          if (!{{=$nextValid}}) {
            errors = {{=$errs}};
            if (validate.errors !== null) {
              if (errors) validate.errors.length = errors;
              else validate.errors = null;
            }
            delete {{=$data}}[{{=$key}}];
          }

          {{# def.resetCompositeRule }}
        {{??}}
          {{# def.validateAdditional }}
          {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}}
        {{?}}
      {{?}}
      {{ it.errorPath = $currentErrorPath; }}
    {{?}}
    {{? $someProperties }}
      }
    {{?}}
  }

  {{# def.ifResultValid }}
{{?}}

{{ var $useDefaults = it.opts.useDefaults && !it.compositeRule; }}

{{? $schemaKeys.length }}
  {{~ $schemaKeys:$propertyKey }}
    {{ var $sch = $schema[$propertyKey]; }}

    {{? {{# def.nonEmptySchema:$sch}} }}
      {{
        var $prop = it.util.getProperty($propertyKey)
          , $passData = $data + $prop
          , $hasDefault = $useDefaults && $sch.default !== undefined;
        $it.schema = $sch;
        $it.schemaPath = $schemaPath + $prop;
        $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
        $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
        $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
      }}

      {{# def.generateSubschemaCode }}

      {{? {{# def.willOptimize }} }}
        {{
          $code = {{# def._optimizeValidate }};
          var $useData = $passData;
        }}
      {{??}}
        {{ var $useData = $nextData; }}
        var {{=$nextData}} = {{=$passData}};
      {{?}}

      {{? $hasDefault }}
        {{= $code }}
      {{??}}
        {{? $requiredHash && $requiredHash[$propertyKey] }}
          if ({{# def.noPropertyInData }}) {
            {{=$nextValid}} = false;
            {{
              var $currentErrorPath = it.errorPath
                , $currErrSchemaPath = $errSchemaPath
                , $missingProperty = it.util.escapeQuotes($propertyKey);
              if (it.opts._errorDataPathProperty) {
                it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
              }
              $errSchemaPath = it.errSchemaPath + '/required';
            }}
            {{# def.error:'required' }}
            {{ $errSchemaPath = $currErrSchemaPath; }}
            {{ it.errorPath = $currentErrorPath; }}
          } else {
        {{??}}
          {{? $breakOnError }}
            if ({{# def.noPropertyInData }}) {
              {{=$nextValid}} = true;
            } else {
          {{??}}
            if ({{=$useData}} !== undefined
              {{? $ownProperties }}
                && {{# def.isOwnProperty }}
              {{?}}
            ) {
          {{?}}
        {{?}}

          {{= $code }}
        }
      {{?}}  {{ /* $hasDefault */ }}
    {{?}} {{ /* def.nonEmptySchema */ }}

    {{# def.ifResultValid }}
  {{~}}
{{?}}

{{? $pPropertyKeys.length }}
  {{~ $pPropertyKeys:$pProperty }}
    {{ var $sch = $pProperties[$pProperty]; }}

    {{? {{# def.nonEmptySchema:$sch}} }}
      {{
        $it.schema = $sch;
        $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
        $it.errSchemaPath = it.errSchemaPath + '/patternProperties/'
                                             + it.util.escapeFragment($pProperty);
      }}

      {{# def.iterateProperties }}
        if ({{= it.usePattern($pProperty) }}.test({{=$key}})) {
          {{
            $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
            var $passData = $data + '[' + $key + ']';
            $it.dataPathArr[$dataNxt] = $key;
          }}

          {{# def.generateSubschemaCode }}
          {{# def.optimizeValidate }}

          {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}}
        }
        {{? $breakOnError }} else {{=$nextValid}} = true; {{?}}
      }

      {{# def.ifResultValid }}
    {{?}} {{ /* def.nonEmptySchema */ }}
  {{~}}
{{?}}


{{? it.opts.patternGroups && $pgPropertyKeys.length }}
  {{~ $pgPropertyKeys:$pgProperty }}
    {{
      var $pgSchema = $pgProperties[$pgProperty]
        , $sch = $pgSchema.schema;
    }}

    {{? {{# def.nonEmptySchema:$sch}} }}
      {{
        $it.schema = $sch;
        $it.schemaPath = it.schemaPath + '.patternGroups' + it.util.getProperty($pgProperty) + '.schema';
        $it.errSchemaPath = it.errSchemaPath + '/patternGroups/'
                                             + it.util.escapeFragment($pgProperty)
                                             + '/schema';
      }}

      var pgPropCount{{=$lvl}} = 0;

      {{# def.iterateProperties }}
        if ({{= it.usePattern($pgProperty) }}.test({{=$key}})) {
          pgPropCount{{=$lvl}}++;

          {{
            $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
            var $passData = $data + '[' + $key + ']';
            $it.dataPathArr[$dataNxt] = $key;
          }}

          {{# def.generateSubschemaCode }}
          {{# def.optimizeValidate }}

          {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}}
        }
        {{? $breakOnError }} else {{=$nextValid}} = true; {{?}}
      }

      {{# def.ifResultValid }}

      {{
        var $pgMin = $pgSchema.minimum
          , $pgMax = $pgSchema.maximum;
      }}
      {{? $pgMin !== undefined || $pgMax !== undefined }}
        var {{=$valid}} = true;

        {{ var $currErrSchemaPath = $errSchemaPath; }}

        {{? $pgMin !== undefined }}
          {{ var $limit = $pgMin, $reason = 'minimum', $moreOrLess = 'less'; }}
          {{=$valid}} = pgPropCount{{=$lvl}} >= {{=$pgMin}};
          {{ $errSchemaPath = it.errSchemaPath + '/patternGroups/minimum'; }}
          {{# def.checkError:'patternGroups' }}
          {{? $pgMax !== undefined }}
            else
          {{?}}
        {{?}}

        {{? $pgMax !== undefined }}
          {{ var $limit = $pgMax, $reason = 'maximum', $moreOrLess = 'more'; }}
          {{=$valid}} = pgPropCount{{=$lvl}} <= {{=$pgMax}};
          {{ $errSchemaPath = it.errSchemaPath + '/patternGroups/maximum'; }}
          {{# def.checkError:'patternGroups' }}
        {{?}}

        {{ $errSchemaPath = $currErrSchemaPath; }}

        {{# def.ifValid }}
      {{?}}
    {{?}} {{ /* def.nonEmptySchema */ }}
  {{~}}
{{?}}


{{? $breakOnError }}
  {{= $closingBraces }}
  if ({{=$errs}} == errors) {
{{?}}

{{# def.cleanUp }}