/*----------------------------------------------------------------------------- The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.mozilla.org/MPL/MPL-1.1.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Fever Framework code. The Initial Developer of the Original Code is Romain Ecarnot. Portions created by Initial Developer are Copyright (C) 2006 the Initial Developer. All Rights Reserved. Contributor(s): Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -----------------------------------------------------------------------------*/ import fever.data.validators.Validator; import fever.events.ValidationResultEvent; /** * Validates arguments Array ( length of array and check for undefined * reference ). * *
Example * {@code * public function ValidationTest6() * { * _valideArguments( 800 ); * } * * private function _valideArguments( width : Number, param : Number ) :Void * { * var validator : ArgumentsValidator = new ArgumentsValidator(); * validator.minValue = 2; * validator.allowUndefined = false; * * var result : ValidationResultEvent = validator.validate( arguments ); * if(result.type == ValidationResultEvent.INVALID) * { * Logger.LOG( result.message, LogLevel.WARN, FeverDebug.channel ); * //output : 'The length of arguments is too small'. * } * else * { * Logger.LOG( "Nice !", LogLevel.WARN, FeverDebug.channel ); * } * } * } * * @see fever.data.validators.Validator * * @author Romain Ecarnot */ class fever.data.validators.ArgumentsValidator extends Validator { //------------------------------------------------------------------------- // Public properties //------------------------------------------------------------------------- /** Maximum waiting arguments. ( default {@code IsNan } ) */ public var maxValue : Number; /** Minimum waiting arguments. ( default {@code IsNan } ) */ public var minValue : Number; /** Indicates if {@code undefined} arguments are checked. ( default {@code true} ) */ public var allowUndefined : Boolean; //------------------------------------------------------------------------- // Public API //------------------------------------------------------------------------- /** * Constructor. */ public function ArgumentsValidator() { maxValue = Number.NaN; minValue = Number.NaN; allowUndefined = true; } //------------------------------------------------------------------------- // Private implementation //------------------------------------------------------------------------- private function _doValidation(object) : ValidationResultEvent { var toValid : Array = _buildValidationTarget( object ); if( !_testRequired( toValid ) ) { _fireEventType( ValidationResultEvent.INVALID ); return _event; } if( !_testMaxValue( toValid.length ) ) { _fireEventType( ValidationResultEvent.INVALID ); return _event; } if( !_testMinValue( toValid.length ) ) { _fireEventType( ValidationResultEvent.INVALID ); return _event; } if( !allowUndefined ) { if( !_testUndefined( toValid ) ) { _fireEventType( ValidationResultEvent.INVALID ); return _event; } } _prepareEvent( ValidationResultEvent.VALID, toValid, ""); _fireEventType( ValidationResultEvent.VALID ); return _event; } private function _testMaxValue(n : Number) : Boolean { if( !isNaN(maxValue) && n > maxValue) { _prepareEvent( ValidationResultEvent.INVALID, n, _resources.argumentes_exceed_max_error ); return false; } return true; } private function _testMinValue(n : Number) : Boolean { if( !isNaN(minValue) && n < minValue) { _prepareEvent( ValidationResultEvent.INVALID, n, _resources.arguments_lower_than_min_error ); return false; } return true; } private function _testUndefined(a : Array) : Boolean { for(var s : String in a) { if( a[s] == undefined) { _prepareEvent( ValidationResultEvent.INVALID, a, _resources.arguments_undefined_found_error ); return false; } } return true; } }