YUKI Hiroshi
null+****@clear*****
Thu Nov 15 17:53:23 JST 2012
YUKI Hiroshi 2012-11-15 17:53:23 +0900 (Thu, 15 Nov 2012) New Revision: 3aaba7d30153028c4b54a9a45a01032d5c23c98f https://github.com/groonga/gcs/commit/3aaba7d30153028c4b54a9a45a01032d5c23c98f Log: Raise validation error for invalid index field type Modified files: lib/api/2011-02-01/configuration.js lib/database/index-field.js Modified: lib/api/2011-02-01/configuration.js (+5 -6) =================================================================== --- lib/api/2011-02-01/configuration.js 2012-11-15 17:41:09 +0900 (7648db0) +++ lib/api/2011-02-01/configuration.js 2012-11-15 17:53:23 +0900 (7f4ab2c) @@ -294,17 +294,16 @@ handlers.DefineIndexField = function(context, request, response, config) { return new Domain(request.query.DomainName || '', context); }); + var createdAt = new Date(); var fieldName = request.query['IndexField.IndexFieldName'] || ''; var fieldType = request.query['IndexField.IndexFieldType'] || 'text'; var field = handleIndexFieldValidationError(function() { - return domain.getIndexField(fieldName); + var field = domain.getIndexField(fieldName); + if (!field.exists()) + field.type = fieldType; + return field; }); - var createdAt = new Date(); - - if (!field.exists()) - field.type = fieldType; - var facetEnabled = getFieldOption('FacetEnabled', request, fieldType); if (facetEnabled !== undefined) field.facetEnabled = facetEnabled.toLowerCase() == 'true'; Modified: lib/database/index-field.js (+46 -8) =================================================================== --- lib/database/index-field.js 2012-11-15 17:41:09 +0900 (3b19cb5) +++ lib/database/index-field.js 2012-11-15 17:53:23 +0900 (4ae50e7) @@ -17,14 +17,26 @@ var INVALID_COLUMN_NAME_CHARACTERS_MATCHER = exports.INVALID_COLUMN_NAME_CHARACTERS_MATCHER = IndexField.INVALID_COLUMN_NAME_CHARACTERS_MATCHER = /[^_a-z0-9]/g; -exports.RESERVED_NAMES = [ - 'body', - 'docid', - 'text_relevance' -]; -exports.RESERVED_COLUMN_NAMES = [ - '_key' -]; +var VALID_FIELD_TYPES = + IndexField.VALID_FIELD_TYPES ~ + exports.VALID_FIELD_TYPES = [ + 'text', + 'literal', + 'uint' + ]; + +var RESERVED_NAMES = + IndexField.RESERVED_NAMES = + exports.RESERVED_NAMES = [ + 'body', + 'docid', + 'text_relevance' + ]; +var RESERVED_COLUMN_NAMES = + IndexField.RESERVED_COLUMN_NAMES = + exports.RESERVED_COLUMN_NAMES = [ + '_key' + ]; function assertValidFieldName(field) { if (typeof field != 'string') { @@ -75,6 +87,31 @@ function assertValidFieldName(field) { } } +function assertValidFieldType(type) { + if (typeof type != 'string') { + var error = new Error('field type must be a string'); + error.isValidationError = true; + throw error; + } + + var errors = []; + var commonPrefix = 'Value \'' + type + '\' at \'%TYPE_FIELD%\' failed ' + + 'to satisfy constraint: '; + + if (VALID_FIELD_TYPES.indexOf(type) < 0) + errors.push(commonPrefix + 'Member must satisfy enum value set: [' + + VALID_FIELD_TYPES.join(', ') + ']'); + + if (errors.length) { + var prefix = errors.length > 1 ? + errors.length + ' validation errors detected: ' : + '1 validation error detected: '; + var error = new Error(prefix + errors.join('; ')); + error.isValidationError = true; + throw error; + } +} + function IndexField(name, domain) { this.domain = domain; this.context = domain.context; @@ -169,6 +206,7 @@ IndexField.prototype = { throw new Error('unknown unfixed column ' + this.columnName); }, set type(type) { + assertValidFieldType(type); return this._type = type }, setType: function(type) { -------------- next part -------------- HTML����������������������������...Download