Blame view

node_modules/ajv-errors/lib/dot/errorMessage.jst 12.4 KB
aaac7fed   liuqimichale   add
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  {{# def.definitions }}
  {{# def.errors }}
  {{# def.setupKeyword }}
  
  {{## def.em_errorMatch:
    {{# def._em_commonErrorMatch }}
    && ({{=$err}}.dataPath == {{=$dataPath}} ||
        ({{=$err}}.dataPath.indexOf({{=$dataPath}}) == 0 &&
         {{=$err}}.dataPath[{{=$dataPath}}.length] == '/'))
    && {{=$err}}.schemaPath.indexOf({{=$errSchemaPathString}}) == 0
    && {{=$err}}.schemaPath[{{=it.errSchemaPath.length}}] == '/'
  #}}
  
  {{## def.em_keywordErrorMatch:
    {{# def._em_commonErrorMatch }}
    && {{=$err}}.keyword in {{=$errors}}
    && {{=$err}}.dataPath == {{=$dataPath}}
    && {{=$err}}.schemaPath.indexOf({{=$errSchemaPathString}}) == 0
    && /^\/[^\/]*$/.test({{=$err}}.schemaPath.slice({{=it.errSchemaPath.length}}))
  #}}
  
  {{## def.em_childErrorMatch:
    {{# def._em_commonErrorMatch }}
    && {{=$err}}.dataPath.indexOf({{=$dataPath}}) == 0
    && ({{=$matches}} = {{=$err}}.dataPath.slice({{=$dataPath}}.length).match(/^\/([^\/]*)(?:\/|$)/),
        {{=$child}} = {{=$matches}} && {{=$matches}}[1].replace(/~1/g, '/').replace(/~0/g, '~')
       ) !== undefined
    && {{=$child}} in {{=$errors}}
  #}}
  
  {{## def._em_commonErrorMatch:
    {{=$err}}.keyword != '{{=$keyword}}'
    {{? $config.options.keepErrors }}
      && !{{=$err}}.emUsed
    {{?}}
  #}}
  
  {{## def.em_useError:
    {{? $config.options.keepErrors }}
      {{=$err}}.emUsed = true;
    {{??}}
      vErrors.splice({{=$i}}, 1);
      errors--;
    {{?}}
  #}}
  
  {{## def.em_compileTemplates: _keysArray:
    var {{=$templates}} = {
      {{ var $comma = false; }}
      {{~ _keysArray:$k }}
        {{? INTERPOLATION.test($schema[$k]) }}
          {{?$comma}},{{?}}{{= it.util.toQuotedString($k) }}: {{= templateFunc($schema[$k]) }}
          {{ $comma = true; }}
        {{?}}
      {{~}}
    };
  #}}
  
  {{## def.em_compilePropsTemplates: _keywordProps:
    var {{=$templates}} = {
      {{ var $comma = false; }}
      {{~ Object.keys(_keywordProps):$k }}
        {{ var $keywordMsgs = $schema[$k]; }}
        {{?$comma}},{{?}}{{= it.util.toQuotedString($k) }}: {
          {{ $comma = true; var $innerComma = false; }}
          {{~ Object.keys($keywordMsgs):$prop }}
            {{? INTERPOLATION.test($keywordMsgs[$prop]) }}
              {{?$innerComma}},{{?}}{{= it.util.toQuotedString($prop) }}: {{= templateFunc($keywordMsgs[$prop]) }}
              {{ $innerComma = true; }}
            {{?}}
          {{~}}
        }
      {{~}}
    };
  #}}
  
  {{## def.em_compileChildTemplates: _children:
    {{ var _keysArray = Object.keys($childErrors._children); }}
    var {{=$templates}} = {
      {{ var $comma = false; }}
      {{~ _keysArray:$k }}
        {{? INTERPOLATION.test($schema._children[$k]) }}
          {{?$comma}},{{?}}{{= it.util.toQuotedString($k) }}: {{= templateFunc($schema._children[$k]) }}
          {{ $comma = true; }}
        {{?}}
      {{~}}
    };
  #}}
  
  {{## def.em_errorMessage:
    {{=$key}} in {{=$templates}}
    ? {{=$templates}}[{{=$key}}] ()
    : validate.schema{{=$schemaPath}}[{{=$key}}]
  #}}
  
  {{## def.em_keywordError:
    var err = {
      keyword: '{{=$keyword}}'
      , dataPath: {{=$dataPath}}
      , schemaPath: {{=$errSchemaPathString}} + '/{{=$keyword}}'
      , params: { errors: {{=$paramsErrors}} }
      , message: {{=$message}}
      {{? it.opts.verbose }}
        , schema: validate.schema{{=$schemaPath}}
        , parentSchema: validate.schema{{=it.schemaPath}}
        , data: {{=$data}}
      {{?}}
    };
    {{# def._addError:'custom' }}
  #}}
  
  
  {{? it.createErrors !== false }}
    {{
      var INTERPOLATION = /\$\{[^\}]+\}/;
      var INTERPOLATION_REPLACE = /\$\{([^\}]+)\}/g;
      var EMPTY_STR = /^\'\'\s*\+\s*|\s*\+\s*\'\'$/g;
  
      var $config = it.self.getKeyword($keyword).config
        , $dataPath = '_em_dataPath' + $lvl
        , $i = '_em_i' + $lvl
        , $key = '_em_key' + $lvl
        , $keyProp = '_em_keyProp' + $lvl
        , $err = '_em_err' + $lvl
        , $child = '_em_child' + $lvl
        , $childKeyword = '_em_childKeyword' + $lvl
        , $matches = '_em_matches' + $lvl
        , $isArray = '_em_isArray' + $lvl
        , $errors = '_em_errors' + $lvl
        , $message = '_em_message' + $lvl
        , $paramsErrors = '_em_paramsErrors' + $lvl
        , $propParam = '_em_propParam' + $lvl
        , $keywordPropParams = '_em_keywordPropParams' + $lvl
        , $templates = '_em_templates' + $lvl
        , $errSchemaPathString = it.util.toQuotedString(it.errSchemaPath);
    }}
  
    if (errors > 0) {
      var {{=$dataPath}} = (dataPath || '') + {{= it.errorPath }};
      var {{=$i}}, {{=$err}}, {{=$errors}};
  
      {{? typeof $schema == 'object' }}
        {{
          var $keywordErrors = {}
            , $keywordPropErrors = {}
            , $childErrors = { properties: {}, items: {} }
            , $hasKeywordProps = false
            , $hasProperties = false
            , $hasItems = false;
  
          for (var $k in $schema) {
            switch ($k) {
              case 'properties':
                for (var $prop in $schema.properties) {
                  $hasProperties = true;
                  $childErrors.properties[$prop] = [];
                }
                break;
              case 'items':
                for (var $item=0; $item<$schema.items.length; $item++) {
                  $hasItems = true;
                  $childErrors.items[$item] = [];
                }
                break;
              default:
                if (typeof $schema[$k] == 'object') {
                  $hasKeywordProps = true;
                  $keywordPropErrors[$k] = {};
                  for (var $prop in $schema[$k]) {
                    $keywordPropErrors[$k][$prop] = [];
                  }
                } else {
                  $keywordErrors[$k] = [];
                }
            }
          }
        }}
  
        {{ var $keywordErrorsArr = Object.keys($keywordErrors); }}
        {{? $keywordErrorsArr.length }}
          {{=$i}} = 0;
          {{=$errors}} = {{= JSON.stringify($keywordErrors) }};
          {{# def.em_compileTemplates:$keywordErrorsArr }}
          while ({{=$i}} < errors) {
            {{=$err}} = vErrors[{{=$i}}];
            if ({{# def.em_keywordErrorMatch}}) {
              {{=$errors}}[{{=$err}}.keyword].push({{=$err}});
              {{# def.em_useError }}
            } else {
              {{=$i}}++;
            }
          }
  
          {{? $config.options.singleError }}
            var {{=$message}} = '';
            var {{=$paramsErrors}} = [];
          {{?}}
  
            for (var {{=$key}} in {{=$errors}}) {
              if ({{=$errors}}[{{=$key}}].length) {
  
          {{? $config.options.singleError }}
                if ({{=$message}}) {
                  {{=$message}} += {{? typeof $config.options.singleError == 'string' }}
                    {{= it.util.toQuotedString($config.options.singleError) }}
                  {{??}}
                    '; '
                  {{?}};
                }
                {{=$message}} += {{# def.em_errorMessage }};
                {{=$paramsErrors}} = {{=$paramsErrors}}.concat({{=$errors}}[{{=$key}}]);
              }
            }
          {{??}}
                var {{=$message}} = {{# def.em_errorMessage }};
                var {{=$paramsErrors}} = {{=$errors}}[{{=$key}}];
          {{?}}
  
                {{# def.em_keywordError}}
  
          {{? !$config.options.singleError }}
              }
            }
          {{?}}
        {{?}} /* $keywordErrorsArr */
  
        {{? $hasKeywordProps }}
          {{=$i}} = 0;
          {{=$errors}} = {{= JSON.stringify($keywordPropErrors) }};
          var {{=$paramsErrors}}, {{=$propParam}};
          var {{=$keywordPropParams}} = {{= JSON.stringify($config.KEYWORD_PROPERTY_PARAMS) }};
          {{# def.em_compilePropsTemplates:$keywordPropErrors }}
  
          while ({{=$i}} < errors) {
            {{=$err}} = vErrors[{{=$i}}];
            if ({{# def.em_keywordErrorMatch}}) {
              {{=$propParam}} = {{=$keywordPropParams}}[{{=$err}}.keyword];
              {{=$paramsErrors}} = {{=$errors}}[{{=$err}}.keyword][{{=$err}}.params[{{=$propParam}}]];
              if ({{=$paramsErrors}}) {
                {{=$paramsErrors}}.push({{=$err}});
                {{# def.em_useError }}
              } else {
                {{=$i}}++;
              }
            } else {
              {{=$i}}++;
            }
          }
  
          for (var {{=$key}} in {{=$errors}}) {
            for (var {{=$keyProp}} in {{=$errors}}[{{=$key}}]) {
              {{=$paramsErrors}} = {{=$errors}}[{{=$key}}][{{=$keyProp}}];
              if ({{=$paramsErrors}}.length) {
                var {{=$message}} =   {{=$key}} in {{=$templates}} && {{=$keyProp}} in {{=$templates}}[{{=$key}}]
                                      ? {{=$templates}}[{{=$key}}][{{=$keyProp}}] ()
                                      : validate.schema{{=$schemaPath}}[{{=$key}}][{{=$keyProp}}];
                {{# def.em_keywordError}}
              }
            }
          }
        {{?}} /* $hasKeywordProps */
  
        {{? $hasProperties || $hasItems }}
          var {{=$isArray}} = Array.isArray({{=$data}});
          if
            {{? $hasProperties && $hasItems }}
              (typeof {{=$data}} == 'object') {
                {{ var $childProp = '[' + $childKeyword + ']'; }}
                {{=$i}} = 0;
                if ({{=$isArray}}) {
                  var {{=$childKeyword}} = 'items';
                  {{=$errors}} = {{= JSON.stringify($childErrors.items) }}; 
                  {{# def.em_compileChildTemplates: items }}
                } else {
                  var {{=$childKeyword}} = 'properties';
                  {{=$errors}} =  {{= JSON.stringify($childErrors.properties) }}; 
                  {{# def.em_compileChildTemplates: properties }}
                }
            {{?? $hasProperties }}
              (typeof {{=$data}} == 'object' && !{{=$isArray}}) {
                {{ var $childProp = '.properties'; }}
                {{=$i}} = 0;
                {{=$errors}} = {{= JSON.stringify($childErrors.properties) }};
                {{# def.em_compileChildTemplates: properties }}
            {{??}}
              ({{=$isArray}}) {
                {{ var $childProp = '.items'; }}
                {{=$i}} = 0;
                {{=$errors}} = {{= JSON.stringify($childErrors.items) }};
                {{# def.em_compileChildTemplates: items }}
            {{?}}
  
            var {{=$child}}, {{=$matches}};
            while ({{=$i}} < errors) {
              {{=$err}} = vErrors[{{=$i}}];
              if ({{# def.em_childErrorMatch}}) {
                {{=$errors}}[{{=$child}}].push({{=$err}});
                {{# def.em_useError }}
              } else {
                {{=$i}}++;
              }
            }
            for (var {{=$key}} in {{=$errors}}) {
              if ({{=$errors}}[{{=$key}}].length) {
                var err = {
                  keyword: '{{=$keyword}}'
                  , dataPath: {{=$dataPath}} + '/' + {{=$key}}.replace(/~/g, '~0').replace(/\//g, '~1')
                  , schemaPath: {{=$errSchemaPathString}} + '/{{=$keyword}}'
                  , params: { errors: {{=$errors}}[{{=$key}}] }
                  , message: {{=$key}} in {{=$templates}}
                              ? {{=$templates}}[{{=$key}}] ()
                              : validate.schema{{=$schemaPath}}{{=$childProp}}[{{=$key}}]
                  {{? it.opts.verbose }}
                    , schema: validate.schema{{=$schemaPath}}
                    , parentSchema: validate.schema{{=it.schemaPath}}
                    , data: {{=$data}}
                  {{?}}
                };
                {{# def._addError:'custom' }}
              }
            } /* for */
          } /* if */
        {{?}} /* $hasProperties || $hasItems */
      {{?}} /* $schema is object */
  
      {{ var $schemaMessage = typeof $schema == 'string' ? $schema : $schema._; }}
      {{? $schemaMessage }}
        {{=$i}} = 0;
        {{=$errors}} = [];
        while ({{=$i}} < errors) {
          {{=$err}} = vErrors[{{=$i}}];
          if ({{# def.em_errorMatch}}) {
            {{=$errors}}.push({{=$err}});
            {{# def.em_useError }}
          } else {
            {{=$i}}++;
          }
        }
        if ({{=$errors}}.length) {
          var err = {
            keyword: '{{=$keyword}}'
            , dataPath: {{=$dataPath}}
            , schemaPath: {{=$errSchemaPathString}} + '/{{=$keyword}}'
            , params: { errors: {{=$errors}} }
            , message: {{=templateExpr($schemaMessage)}}
            {{? it.opts.verbose }}
              , schema: {{=it.util.toQuotedString($schemaMessage)}}
              , parentSchema: validate.schema{{=it.schemaPath}}
              , data: {{=$data}}
            {{?}}
          };
          {{# def._addError:'custom' }}
        }
      {{?}}
    }
  {{?}}
  
  
  {{
    function templateExpr(str) {
      str = it.util.escapeQuotes(str);
      if (!INTERPOLATION.test(str)) return "'" + str + "'";
      var expr = "'" + str.replace(INTERPOLATION_REPLACE, function ($0, $1) {
        return "' + JSON.stringify(" + it.util.getData($1, $dataLvl, it.dataPathArr) + ") + '";
      }) + "'";
      return expr.replace(EMPTY_STR, '');
    }
  
    function templateFunc(str) {
      return 'function() { return ' + templateExpr(str) + '; }';
    }
  }}