Ignore:
Timestamp:
03/05/14 22:44:48 (11 years ago)
Author:
hendrikvanantwerpen
Message:

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/node_modules/tv4/tv4.js

    r484 r487  
    133133                this.scannedFrozen = [];
    134134                this.scannedFrozenSchemas = [];
    135                 this.key = 'tv4_validation_id';
     135                this.scannedFrozenValidationErrors = [];
     136                this.validatedSchemasKey = 'tv4_validation_id';
     137                this.validationErrorsKey = 'tv4_validation_errors_id';
    136138        }
    137139        if (trackUnknownProperties) {
     
    240242};
    241243ValidatorContext.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                }
    250252                for (var key in schema) {
    251253                        if (key !== "enum") {
     
    264266ValidatorContext.prototype.addSchema = function (url, schema) {
    265267        //overload
    266         if (typeof schema === 'undefined') {
     268        if (typeof url !== 'string' || typeof schema === 'undefined') {
    267269                if (typeof url === 'object' && typeof url.id === 'string') {
    268270                        schema = url;
     
    331333        }
    332334
    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') {
    334338                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                }
    337346                if (Object.isFrozen(data)) {
    338347                        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                        }
    340355                }
    341356                this.scanned.push(data);
     
    346361                                this.scannedFrozenSchemas.push([]);
    347362                        }
    348                         this.scannedFrozenSchemas[frozenIndex].push(schema);
     363                        scannedFrozenSchemaIndex = this.scannedFrozenSchemas[frozenIndex].length;
     364                        this.scannedFrozenSchemas[frozenIndex][scannedFrozenSchemaIndex] = schema;
     365                        this.scannedFrozenValidationErrors[frozenIndex][scannedFrozenSchemaIndex] = [];
    349366                } else {
    350                         if (!data[this.key]) {
     367                        if (!data[this.validatedSchemasKey]) {
    351368                                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, {
    353374                                                value: [],
    354375                                                configurable: true
     
    356377                                } catch (e) {
    357378                                        //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] = [];
    362386                }
    363387        }
     
    376400                while (this.scanned.length) {
    377401                        var item = this.scanned.pop();
    378                         delete item[this.key];
     402                        delete item[this.validatedSchemasKey];
    379403                }
    380404                this.scannedFrozen = [];
     
    391415                        this.prefixErrors(errorCount, dataPart, schemaPart);
    392416                }
     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);
    393423        }
    394424
     
    9961026}
    9971027function 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                }
    10051035                if (Array.isArray(schema)) {
    10061036                        for (var i = 0; i < schema.length; i++) {
     
    10501080        FORMAT_CUSTOM: 500,
    10511081        // Schema structure
    1052         CIRCULAR_REFERENCE: 500,
     1082        CIRCULAR_REFERENCE: 600,
    10531083        // Non-standard validation options
    10541084        UNKNOWN_PROPERTY: 1000
     
    10911121
    10921122function ValidationError(code, message, dataPath, schemaPath, subErrors) {
     1123        Error.call(this);
    10931124        if (code === undefined) {
    10941125                throw new Error ("No code supplied for error: "+ message);
    10951126        }
     1127        this.message = message;
    10961128        this.code = code;
    1097         this.message = message;
    10981129        this.dataPath = dataPath || "";
    10991130        this.schemaPath = schemaPath || "";
    11001131        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}
     1144ValidationError.prototype = Object.create(Error.prototype);
     1145ValidationError.prototype.constructor = ValidationError;
     1146ValidationError.prototype.name = 'ValidationError';
     1147
    11031148ValidationError.prototype.prefixWith = function (dataPrefix, schemaPrefix) {
    11041149        if (dataPrefix !== null) {
     
    12651310
    12661311})(this);
    1267 
    1268 //@ sourceMappingURL=tv4.js.map
Note: See TracChangeset for help on using the changeset viewer.