Changeset 487 for Dev/trunk/src/node_modules/tv4/tv4.js
- Timestamp:
- 03/05/14 22:44:48 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/node_modules/tv4/tv4.js
r484 r487 133 133 this.scannedFrozen = []; 134 134 this.scannedFrozenSchemas = []; 135 this.key = 'tv4_validation_id'; 135 this.scannedFrozenValidationErrors = []; 136 this.validatedSchemasKey = 'tv4_validation_id'; 137 this.validationErrorsKey = 'tv4_validation_errors_id'; 136 138 } 137 139 if (trackUnknownProperties) { … … 240 242 }; 241 243 ValidatorContext.prototype.searchSchemas = function (schema, url) { 242 if ( typeof schema.id === "string") {243 if ( isTrustedUrl(url, schema.id)) {244 if ( this.schemas[schema.id] === undefined) {245 this.schemas[schema.id] = schema;246 }247 }248 }249 if (typeof schema === "object") {244 if (schema && typeof schema === "object") { 245 if (typeof schema.id === "string") { 246 if (isTrustedUrl(url, schema.id)) { 247 if (this.schemas[schema.id] === undefined) { 248 this.schemas[schema.id] = schema; 249 } 250 } 251 } 250 252 for (var key in schema) { 251 253 if (key !== "enum") { … … 264 266 ValidatorContext.prototype.addSchema = function (url, schema) { 265 267 //overload 266 if (typeof schema === 'undefined') {268 if (typeof url !== 'string' || typeof schema === 'undefined') { 267 269 if (typeof url === 'object' && typeof url.id === 'string') { 268 270 schema = url; … … 331 333 } 332 334 333 if (this.checkRecursive && (typeof data) === 'object') { 335 var startErrorCount = this.errors.length; 336 var frozenIndex, scannedFrozenSchemaIndex = null, scannedSchemasIndex = null; 337 if (this.checkRecursive && data && typeof data === 'object') { 334 338 topLevel = !this.scanned.length; 335 if (data[this.key] && data[this.key].indexOf(schema) !== -1) { return null; } 336 var frozenIndex; 339 if (data[this.validatedSchemasKey]) { 340 var schemaIndex = data[this.validatedSchemasKey].indexOf(schema); 341 if (schemaIndex !== -1) { 342 this.errors = this.errors.concat(data[this.validationErrorsKey][schemaIndex]); 343 return null; 344 } 345 } 337 346 if (Object.isFrozen(data)) { 338 347 frozenIndex = this.scannedFrozen.indexOf(data); 339 if (frozenIndex !== -1 && this.scannedFrozenSchemas[frozenIndex].indexOf(schema) !== -1) { return null; } 348 if (frozenIndex !== -1) { 349 var frozenSchemaIndex = this.scannedFrozenSchemas[frozenIndex].indexOf(schema); 350 if (frozenSchemaIndex !== -1) { 351 this.errors = this.errors.concat(this.scannedFrozenValidationErrors[frozenIndex][frozenSchemaIndex]); 352 return null; 353 } 354 } 340 355 } 341 356 this.scanned.push(data); … … 346 361 this.scannedFrozenSchemas.push([]); 347 362 } 348 this.scannedFrozenSchemas[frozenIndex].push(schema); 363 scannedFrozenSchemaIndex = this.scannedFrozenSchemas[frozenIndex].length; 364 this.scannedFrozenSchemas[frozenIndex][scannedFrozenSchemaIndex] = schema; 365 this.scannedFrozenValidationErrors[frozenIndex][scannedFrozenSchemaIndex] = []; 349 366 } else { 350 if (!data[this. key]) {367 if (!data[this.validatedSchemasKey]) { 351 368 try { 352 Object.defineProperty(data, this.key, { 369 Object.defineProperty(data, this.validatedSchemasKey, { 370 value: [], 371 configurable: true 372 }); 373 Object.defineProperty(data, this.validationErrorsKey, { 353 374 value: [], 354 375 configurable: true … … 356 377 } catch (e) { 357 378 //IE 7/8 workaround 358 data[this.key] = []; 359 } 360 } 361 data[this.key].push(schema); 379 data[this.validatedSchemasKey] = []; 380 data[this.validationErrorsKey] = []; 381 } 382 } 383 scannedSchemasIndex = data[this.validatedSchemasKey].length; 384 data[this.validatedSchemasKey][scannedSchemasIndex] = schema; 385 data[this.validationErrorsKey][scannedSchemasIndex] = []; 362 386 } 363 387 } … … 376 400 while (this.scanned.length) { 377 401 var item = this.scanned.pop(); 378 delete item[this. key];402 delete item[this.validatedSchemasKey]; 379 403 } 380 404 this.scannedFrozen = []; … … 391 415 this.prefixErrors(errorCount, dataPart, schemaPart); 392 416 } 417 } 418 419 if (scannedFrozenSchemaIndex !== null) { 420 this.scannedFrozenValidationErrors[frozenIndex][scannedFrozenSchemaIndex] = this.errors.slice(startErrorCount); 421 } else if (scannedSchemasIndex !== null) { 422 data[this.validationErrorsKey][scannedSchemasIndex] = this.errors.slice(startErrorCount); 393 423 } 394 424 … … 996 1026 } 997 1027 function normSchema(schema, baseUri) { 998 if ( baseUri === undefined) {999 baseUri = schema.id;1000 } else if (typeof schema.id === "string") {1001 baseUri = resolveUrl(baseUri, schema.id);1002 schema.id = baseUri;1003 }1004 if (typeof schema === "object") {1028 if (schema && typeof schema === "object") { 1029 if (baseUri === undefined) { 1030 baseUri = schema.id; 1031 } else if (typeof schema.id === "string") { 1032 baseUri = resolveUrl(baseUri, schema.id); 1033 schema.id = baseUri; 1034 } 1005 1035 if (Array.isArray(schema)) { 1006 1036 for (var i = 0; i < schema.length; i++) { … … 1050 1080 FORMAT_CUSTOM: 500, 1051 1081 // Schema structure 1052 CIRCULAR_REFERENCE: 500,1082 CIRCULAR_REFERENCE: 600, 1053 1083 // Non-standard validation options 1054 1084 UNKNOWN_PROPERTY: 1000 … … 1091 1121 1092 1122 function ValidationError(code, message, dataPath, schemaPath, subErrors) { 1123 Error.call(this); 1093 1124 if (code === undefined) { 1094 1125 throw new Error ("No code supplied for error: "+ message); 1095 1126 } 1127 this.message = message; 1096 1128 this.code = code; 1097 this.message = message;1098 1129 this.dataPath = dataPath || ""; 1099 1130 this.schemaPath = schemaPath || ""; 1100 1131 this.subErrors = subErrors || null; 1101 } 1102 ValidationError.prototype = new Error(); 1132 1133 var err = new Error(this.message); 1134 this.stack = err.stack || err.stacktrace; 1135 if (!this.stack) { 1136 try { 1137 throw err; 1138 } 1139 catch(err) { 1140 this.stack = err.stack || err.stacktrace; 1141 } 1142 } 1143 } 1144 ValidationError.prototype = Object.create(Error.prototype); 1145 ValidationError.prototype.constructor = ValidationError; 1146 ValidationError.prototype.name = 'ValidationError'; 1147 1103 1148 ValidationError.prototype.prefixWith = function (dataPrefix, schemaPrefix) { 1104 1149 if (dataPrefix !== null) { … … 1265 1310 1266 1311 })(this); 1267 1268 //@ sourceMappingURL=tv4.js.map
Note: See TracChangeset
for help on using the changeset viewer.