[6a3a178] | 1 | {{# def.definitions }}
|
---|
| 2 | {{# def.errors }}
|
---|
| 3 | {{# def.setupKeyword }}
|
---|
| 4 |
|
---|
| 5 | {{## def.em_errorMatch:
|
---|
| 6 | {{# def._em_commonErrorMatch }}
|
---|
| 7 | && ({{=$err}}.dataPath == {{=$dataPath}} ||
|
---|
| 8 | ({{=$err}}.dataPath.indexOf({{=$dataPath}}) == 0 &&
|
---|
| 9 | {{=$err}}.dataPath[{{=$dataPath}}.length] == '/'))
|
---|
| 10 | && {{=$err}}.schemaPath.indexOf({{=$errSchemaPathString}}) == 0
|
---|
| 11 | && {{=$err}}.schemaPath[{{=it.errSchemaPath.length}}] == '/'
|
---|
| 12 | #}}
|
---|
| 13 |
|
---|
| 14 | {{## def.em_keywordErrorMatch:
|
---|
| 15 | {{# def._em_commonErrorMatch }}
|
---|
| 16 | && {{=$err}}.keyword in {{=$errors}}
|
---|
| 17 | && {{=$err}}.dataPath == {{=$dataPath}}
|
---|
| 18 | && {{=$err}}.schemaPath.indexOf({{=$errSchemaPathString}}) == 0
|
---|
| 19 | && /^\/[^\/]*$/.test({{=$err}}.schemaPath.slice({{=it.errSchemaPath.length}}))
|
---|
| 20 | #}}
|
---|
| 21 |
|
---|
| 22 | {{## def.em_childErrorMatch:
|
---|
| 23 | {{# def._em_commonErrorMatch }}
|
---|
| 24 | && {{=$err}}.dataPath.indexOf({{=$dataPath}}) == 0
|
---|
| 25 | && ({{=$matches}} = {{=$err}}.dataPath.slice({{=$dataPath}}.length).match(/^\/([^\/]*)(?:\/|$)/),
|
---|
| 26 | {{=$child}} = {{=$matches}} && {{=$matches}}[1].replace(/~1/g, '/').replace(/~0/g, '~')
|
---|
| 27 | ) !== undefined
|
---|
| 28 | && {{=$child}} in {{=$errors}}
|
---|
| 29 | #}}
|
---|
| 30 |
|
---|
| 31 | {{## def._em_commonErrorMatch:
|
---|
| 32 | {{=$err}}.keyword != '{{=$keyword}}'
|
---|
| 33 | {{? $config.options.keepErrors }}
|
---|
| 34 | && !{{=$err}}.emUsed
|
---|
| 35 | {{?}}
|
---|
| 36 | #}}
|
---|
| 37 |
|
---|
| 38 | {{## def.em_useError:
|
---|
| 39 | {{? $config.options.keepErrors }}
|
---|
| 40 | {{=$err}}.emUsed = true;
|
---|
| 41 | {{??}}
|
---|
| 42 | vErrors.splice({{=$i}}, 1);
|
---|
| 43 | errors--;
|
---|
| 44 | {{?}}
|
---|
| 45 | #}}
|
---|
| 46 |
|
---|
| 47 | {{## def.em_compileTemplates: _keysArray:
|
---|
| 48 | var {{=$templates}} = {
|
---|
| 49 | {{ var $comma = false; }}
|
---|
| 50 | {{~ _keysArray:$k }}
|
---|
| 51 | {{? INTERPOLATION.test($schema[$k]) }}
|
---|
| 52 | {{?$comma}},{{?}}{{= it.util.toQuotedString($k) }}: {{= templateFunc($schema[$k]) }}
|
---|
| 53 | {{ $comma = true; }}
|
---|
| 54 | {{?}}
|
---|
| 55 | {{~}}
|
---|
| 56 | };
|
---|
| 57 | #}}
|
---|
| 58 |
|
---|
| 59 | {{## def.em_compilePropsTemplates: _keywordProps:
|
---|
| 60 | var {{=$templates}} = {
|
---|
| 61 | {{ var $comma = false; }}
|
---|
| 62 | {{~ Object.keys(_keywordProps):$k }}
|
---|
| 63 | {{ var $keywordMsgs = $schema[$k]; }}
|
---|
| 64 | {{?$comma}},{{?}}{{= it.util.toQuotedString($k) }}: {
|
---|
| 65 | {{ $comma = true; var $innerComma = false; }}
|
---|
| 66 | {{~ Object.keys($keywordMsgs):$prop }}
|
---|
| 67 | {{? INTERPOLATION.test($keywordMsgs[$prop]) }}
|
---|
| 68 | {{?$innerComma}},{{?}}{{= it.util.toQuotedString($prop) }}: {{= templateFunc($keywordMsgs[$prop]) }}
|
---|
| 69 | {{ $innerComma = true; }}
|
---|
| 70 | {{?}}
|
---|
| 71 | {{~}}
|
---|
| 72 | }
|
---|
| 73 | {{~}}
|
---|
| 74 | };
|
---|
| 75 | #}}
|
---|
| 76 |
|
---|
| 77 | {{## def.em_compileChildTemplates: _children:
|
---|
| 78 | {{ var _keysArray = Object.keys($childErrors._children); }}
|
---|
| 79 | var {{=$templates}} = {
|
---|
| 80 | {{ var $comma = false; }}
|
---|
| 81 | {{~ _keysArray:$k }}
|
---|
| 82 | {{? INTERPOLATION.test($schema._children[$k]) }}
|
---|
| 83 | {{?$comma}},{{?}}{{= it.util.toQuotedString($k) }}: {{= templateFunc($schema._children[$k]) }}
|
---|
| 84 | {{ $comma = true; }}
|
---|
| 85 | {{?}}
|
---|
| 86 | {{~}}
|
---|
| 87 | };
|
---|
| 88 | #}}
|
---|
| 89 |
|
---|
| 90 | {{## def.em_errorMessage:
|
---|
| 91 | {{=$key}} in {{=$templates}}
|
---|
| 92 | ? {{=$templates}}[{{=$key}}] ()
|
---|
| 93 | : validate.schema{{=$schemaPath}}[{{=$key}}]
|
---|
| 94 | #}}
|
---|
| 95 |
|
---|
| 96 | {{## def.em_keywordError:
|
---|
| 97 | var err = {
|
---|
| 98 | keyword: '{{=$keyword}}'
|
---|
| 99 | , dataPath: {{=$dataPath}}
|
---|
| 100 | , schemaPath: {{=$errSchemaPathString}} + '/{{=$keyword}}'
|
---|
| 101 | , params: { errors: {{=$paramsErrors}} }
|
---|
| 102 | , message: {{=$message}}
|
---|
| 103 | {{? it.opts.verbose }}
|
---|
| 104 | , schema: validate.schema{{=$schemaPath}}
|
---|
| 105 | , parentSchema: validate.schema{{=it.schemaPath}}
|
---|
| 106 | , data: {{=$data}}
|
---|
| 107 | {{?}}
|
---|
| 108 | };
|
---|
| 109 | {{# def._addError:'custom' }}
|
---|
| 110 | #}}
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | {{? it.createErrors !== false }}
|
---|
| 114 | {{
|
---|
| 115 | var INTERPOLATION = /\$\{[^\}]+\}/;
|
---|
| 116 | var INTERPOLATION_REPLACE = /\$\{([^\}]+)\}/g;
|
---|
| 117 | var EMPTY_STR = /^\'\'\s*\+\s*|\s*\+\s*\'\'$/g;
|
---|
| 118 |
|
---|
| 119 | var $config = it.self.getKeyword($keyword).config
|
---|
| 120 | , $dataPath = '_em_dataPath' + $lvl
|
---|
| 121 | , $i = '_em_i' + $lvl
|
---|
| 122 | , $key = '_em_key' + $lvl
|
---|
| 123 | , $keyProp = '_em_keyProp' + $lvl
|
---|
| 124 | , $err = '_em_err' + $lvl
|
---|
| 125 | , $child = '_em_child' + $lvl
|
---|
| 126 | , $childKeyword = '_em_childKeyword' + $lvl
|
---|
| 127 | , $matches = '_em_matches' + $lvl
|
---|
| 128 | , $isArray = '_em_isArray' + $lvl
|
---|
| 129 | , $errors = '_em_errors' + $lvl
|
---|
| 130 | , $message = '_em_message' + $lvl
|
---|
| 131 | , $paramsErrors = '_em_paramsErrors' + $lvl
|
---|
| 132 | , $propParam = '_em_propParam' + $lvl
|
---|
| 133 | , $keywordPropParams = '_em_keywordPropParams' + $lvl
|
---|
| 134 | , $templates = '_em_templates' + $lvl
|
---|
| 135 | , $errSchemaPathString = it.util.toQuotedString(it.errSchemaPath);
|
---|
| 136 | }}
|
---|
| 137 |
|
---|
| 138 | if (errors > 0) {
|
---|
| 139 | var {{=$dataPath}} = (dataPath || '') + {{= it.errorPath }};
|
---|
| 140 | var {{=$i}}, {{=$err}}, {{=$errors}};
|
---|
| 141 |
|
---|
| 142 | {{? typeof $schema == 'object' }}
|
---|
| 143 | {{
|
---|
| 144 | var $keywordErrors = {}
|
---|
| 145 | , $keywordPropErrors = {}
|
---|
| 146 | , $childErrors = { properties: {}, items: {} }
|
---|
| 147 | , $hasKeywordProps = false
|
---|
| 148 | , $hasProperties = false
|
---|
| 149 | , $hasItems = false;
|
---|
| 150 |
|
---|
| 151 | for (var $k in $schema) {
|
---|
| 152 | switch ($k) {
|
---|
| 153 | case 'properties':
|
---|
| 154 | for (var $prop in $schema.properties) {
|
---|
| 155 | $hasProperties = true;
|
---|
| 156 | $childErrors.properties[$prop] = [];
|
---|
| 157 | }
|
---|
| 158 | break;
|
---|
| 159 | case 'items':
|
---|
| 160 | for (var $item=0; $item<$schema.items.length; $item++) {
|
---|
| 161 | $hasItems = true;
|
---|
| 162 | $childErrors.items[$item] = [];
|
---|
| 163 | }
|
---|
| 164 | break;
|
---|
| 165 | default:
|
---|
| 166 | if (typeof $schema[$k] == 'object') {
|
---|
| 167 | $hasKeywordProps = true;
|
---|
| 168 | $keywordPropErrors[$k] = {};
|
---|
| 169 | for (var $prop in $schema[$k]) {
|
---|
| 170 | $keywordPropErrors[$k][$prop] = [];
|
---|
| 171 | }
|
---|
| 172 | } else {
|
---|
| 173 | $keywordErrors[$k] = [];
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }}
|
---|
| 178 |
|
---|
| 179 | {{ var $keywordErrorsArr = Object.keys($keywordErrors); }}
|
---|
| 180 | {{? $keywordErrorsArr.length }}
|
---|
| 181 | {{=$i}} = 0;
|
---|
| 182 | {{=$errors}} = {{= JSON.stringify($keywordErrors) }};
|
---|
| 183 | {{# def.em_compileTemplates:$keywordErrorsArr }}
|
---|
| 184 | while ({{=$i}} < errors) {
|
---|
| 185 | {{=$err}} = vErrors[{{=$i}}];
|
---|
| 186 | if ({{# def.em_keywordErrorMatch}}) {
|
---|
| 187 | {{=$errors}}[{{=$err}}.keyword].push({{=$err}});
|
---|
| 188 | {{# def.em_useError }}
|
---|
| 189 | } else {
|
---|
| 190 | {{=$i}}++;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | {{? $config.options.singleError }}
|
---|
| 195 | var {{=$message}} = '';
|
---|
| 196 | var {{=$paramsErrors}} = [];
|
---|
| 197 | {{?}}
|
---|
| 198 |
|
---|
| 199 | for (var {{=$key}} in {{=$errors}}) {
|
---|
| 200 | if ({{=$errors}}[{{=$key}}].length) {
|
---|
| 201 |
|
---|
| 202 | {{? $config.options.singleError }}
|
---|
| 203 | if ({{=$message}}) {
|
---|
| 204 | {{=$message}} += {{? typeof $config.options.singleError == 'string' }}
|
---|
| 205 | {{= it.util.toQuotedString($config.options.singleError) }}
|
---|
| 206 | {{??}}
|
---|
| 207 | '; '
|
---|
| 208 | {{?}};
|
---|
| 209 | }
|
---|
| 210 | {{=$message}} += {{# def.em_errorMessage }};
|
---|
| 211 | {{=$paramsErrors}} = {{=$paramsErrors}}.concat({{=$errors}}[{{=$key}}]);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | {{??}}
|
---|
| 215 | var {{=$message}} = {{# def.em_errorMessage }};
|
---|
| 216 | var {{=$paramsErrors}} = {{=$errors}}[{{=$key}}];
|
---|
| 217 | {{?}}
|
---|
| 218 |
|
---|
| 219 | {{# def.em_keywordError}}
|
---|
| 220 |
|
---|
| 221 | {{? !$config.options.singleError }}
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | {{?}}
|
---|
| 225 | {{?}} /* $keywordErrorsArr */
|
---|
| 226 |
|
---|
| 227 | {{? $hasKeywordProps }}
|
---|
| 228 | {{=$i}} = 0;
|
---|
| 229 | {{=$errors}} = {{= JSON.stringify($keywordPropErrors) }};
|
---|
| 230 | var {{=$paramsErrors}}, {{=$propParam}};
|
---|
| 231 | var {{=$keywordPropParams}} = {{= JSON.stringify($config.KEYWORD_PROPERTY_PARAMS) }};
|
---|
| 232 | {{# def.em_compilePropsTemplates:$keywordPropErrors }}
|
---|
| 233 |
|
---|
| 234 | while ({{=$i}} < errors) {
|
---|
| 235 | {{=$err}} = vErrors[{{=$i}}];
|
---|
| 236 | if ({{# def.em_keywordErrorMatch}}) {
|
---|
| 237 | {{=$propParam}} = {{=$keywordPropParams}}[{{=$err}}.keyword];
|
---|
| 238 | {{=$paramsErrors}} = {{=$errors}}[{{=$err}}.keyword][{{=$err}}.params[{{=$propParam}}]];
|
---|
| 239 | if ({{=$paramsErrors}}) {
|
---|
| 240 | {{=$paramsErrors}}.push({{=$err}});
|
---|
| 241 | {{# def.em_useError }}
|
---|
| 242 | } else {
|
---|
| 243 | {{=$i}}++;
|
---|
| 244 | }
|
---|
| 245 | } else {
|
---|
| 246 | {{=$i}}++;
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | for (var {{=$key}} in {{=$errors}}) {
|
---|
| 251 | for (var {{=$keyProp}} in {{=$errors}}[{{=$key}}]) {
|
---|
| 252 | {{=$paramsErrors}} = {{=$errors}}[{{=$key}}][{{=$keyProp}}];
|
---|
| 253 | if ({{=$paramsErrors}}.length) {
|
---|
| 254 | var {{=$message}} = {{=$key}} in {{=$templates}} && {{=$keyProp}} in {{=$templates}}[{{=$key}}]
|
---|
| 255 | ? {{=$templates}}[{{=$key}}][{{=$keyProp}}] ()
|
---|
| 256 | : validate.schema{{=$schemaPath}}[{{=$key}}][{{=$keyProp}}];
|
---|
| 257 | {{# def.em_keywordError}}
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 | {{?}} /* $hasKeywordProps */
|
---|
| 262 |
|
---|
| 263 | {{? $hasProperties || $hasItems }}
|
---|
| 264 | var {{=$isArray}} = Array.isArray({{=$data}});
|
---|
| 265 | if
|
---|
| 266 | {{? $hasProperties && $hasItems }}
|
---|
| 267 | (typeof {{=$data}} == 'object') {
|
---|
| 268 | {{ var $childProp = '[' + $childKeyword + ']'; }}
|
---|
| 269 | {{=$i}} = 0;
|
---|
| 270 | if ({{=$isArray}}) {
|
---|
| 271 | var {{=$childKeyword}} = 'items';
|
---|
| 272 | {{=$errors}} = {{= JSON.stringify($childErrors.items) }};
|
---|
| 273 | {{# def.em_compileChildTemplates: items }}
|
---|
| 274 | } else {
|
---|
| 275 | var {{=$childKeyword}} = 'properties';
|
---|
| 276 | {{=$errors}} = {{= JSON.stringify($childErrors.properties) }};
|
---|
| 277 | {{# def.em_compileChildTemplates: properties }}
|
---|
| 278 | }
|
---|
| 279 | {{?? $hasProperties }}
|
---|
| 280 | (typeof {{=$data}} == 'object' && !{{=$isArray}}) {
|
---|
| 281 | {{ var $childProp = '.properties'; }}
|
---|
| 282 | {{=$i}} = 0;
|
---|
| 283 | {{=$errors}} = {{= JSON.stringify($childErrors.properties) }};
|
---|
| 284 | {{# def.em_compileChildTemplates: properties }}
|
---|
| 285 | {{??}}
|
---|
| 286 | ({{=$isArray}}) {
|
---|
| 287 | {{ var $childProp = '.items'; }}
|
---|
| 288 | {{=$i}} = 0;
|
---|
| 289 | {{=$errors}} = {{= JSON.stringify($childErrors.items) }};
|
---|
| 290 | {{# def.em_compileChildTemplates: items }}
|
---|
| 291 | {{?}}
|
---|
| 292 |
|
---|
| 293 | var {{=$child}}, {{=$matches}};
|
---|
| 294 | while ({{=$i}} < errors) {
|
---|
| 295 | {{=$err}} = vErrors[{{=$i}}];
|
---|
| 296 | if ({{# def.em_childErrorMatch}}) {
|
---|
| 297 | {{=$errors}}[{{=$child}}].push({{=$err}});
|
---|
| 298 | {{# def.em_useError }}
|
---|
| 299 | } else {
|
---|
| 300 | {{=$i}}++;
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
| 303 | for (var {{=$key}} in {{=$errors}}) {
|
---|
| 304 | if ({{=$errors}}[{{=$key}}].length) {
|
---|
| 305 | var err = {
|
---|
| 306 | keyword: '{{=$keyword}}'
|
---|
| 307 | , dataPath: {{=$dataPath}} + '/' + {{=$key}}.replace(/~/g, '~0').replace(/\//g, '~1')
|
---|
| 308 | , schemaPath: {{=$errSchemaPathString}} + '/{{=$keyword}}'
|
---|
| 309 | , params: { errors: {{=$errors}}[{{=$key}}] }
|
---|
| 310 | , message: {{=$key}} in {{=$templates}}
|
---|
| 311 | ? {{=$templates}}[{{=$key}}] ()
|
---|
| 312 | : validate.schema{{=$schemaPath}}{{=$childProp}}[{{=$key}}]
|
---|
| 313 | {{? it.opts.verbose }}
|
---|
| 314 | , schema: validate.schema{{=$schemaPath}}
|
---|
| 315 | , parentSchema: validate.schema{{=it.schemaPath}}
|
---|
| 316 | , data: {{=$data}}
|
---|
| 317 | {{?}}
|
---|
| 318 | };
|
---|
| 319 | {{# def._addError:'custom' }}
|
---|
| 320 | }
|
---|
| 321 | } /* for */
|
---|
| 322 | } /* if */
|
---|
| 323 | {{?}} /* $hasProperties || $hasItems */
|
---|
| 324 | {{?}} /* $schema is object */
|
---|
| 325 |
|
---|
| 326 | {{ var $schemaMessage = typeof $schema == 'string' ? $schema : $schema._; }}
|
---|
| 327 | {{? $schemaMessage }}
|
---|
| 328 | {{=$i}} = 0;
|
---|
| 329 | {{=$errors}} = [];
|
---|
| 330 | while ({{=$i}} < errors) {
|
---|
| 331 | {{=$err}} = vErrors[{{=$i}}];
|
---|
| 332 | if ({{# def.em_errorMatch}}) {
|
---|
| 333 | {{=$errors}}.push({{=$err}});
|
---|
| 334 | {{# def.em_useError }}
|
---|
| 335 | } else {
|
---|
| 336 | {{=$i}}++;
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | if ({{=$errors}}.length) {
|
---|
| 340 | var err = {
|
---|
| 341 | keyword: '{{=$keyword}}'
|
---|
| 342 | , dataPath: {{=$dataPath}}
|
---|
| 343 | , schemaPath: {{=$errSchemaPathString}} + '/{{=$keyword}}'
|
---|
| 344 | , params: { errors: {{=$errors}} }
|
---|
| 345 | , message: {{=templateExpr($schemaMessage)}}
|
---|
| 346 | {{? it.opts.verbose }}
|
---|
| 347 | , schema: {{=it.util.toQuotedString($schemaMessage)}}
|
---|
| 348 | , parentSchema: validate.schema{{=it.schemaPath}}
|
---|
| 349 | , data: {{=$data}}
|
---|
| 350 | {{?}}
|
---|
| 351 | };
|
---|
| 352 | {{# def._addError:'custom' }}
|
---|
| 353 | }
|
---|
| 354 | {{?}}
|
---|
| 355 | }
|
---|
| 356 | {{?}}
|
---|
| 357 |
|
---|
| 358 |
|
---|
| 359 | {{
|
---|
| 360 | function templateExpr(str) {
|
---|
| 361 | str = it.util.escapeQuotes(str);
|
---|
| 362 | if (!INTERPOLATION.test(str)) return "'" + str + "'";
|
---|
| 363 | var expr = "'" + str.replace(INTERPOLATION_REPLACE, function ($0, $1) {
|
---|
| 364 | return "' + JSON.stringify(" + it.util.getData($1, $dataLvl, it.dataPathArr) + ") + '";
|
---|
| 365 | }) + "'";
|
---|
| 366 | return expr.replace(EMPTY_STR, '');
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | function templateFunc(str) {
|
---|
| 370 | return 'function() { return ' + templateExpr(str) + '; }';
|
---|
| 371 | }
|
---|
| 372 | }}
|
---|