Esempio n. 1
0
 /**
  * I cover:
  * ParameterConfig->maximum
  * ParameterConfig->minimum
  * ParameterConfig->multipleOf
  */
 protected function _buildValidators()
 {
     parent::_buildValidators();
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterMaximum
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor17
     if (isset($this->_data->maximum)) {
         $maximum = $this->_data->maximum;
         $exclusiveMaximum = isset($this->_data->exclusiveMaximum) && $this->_data->exclusiveMaximum;
         $this->_validators['maximum'] = $this->ValidatorFactory->buildValidator('Callback', ['message' => 'expected to be' . ($exclusiveMaximum ? ' exclusively' : '') . 'less than ' . $maximum, 'callback' => function ($value) use($maximum, $exclusiveMaximum) {
             return $exclusiveMaximum ? $value < $maximum : $value <= $maximum;
         }]);
     }
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterMinimum
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor21
     if (isset($this->_data->minimum)) {
         $minimum = $this->_data->minimum;
         $exclusiveMinimum = isset($this->_data->exclusiveMinimum) && $this->_data->exclusiveMinimum;
         $this->_validators['minimum'] = $this->ValidatorFactory->buildValidator('Callback', ['message' => 'expected to be' . ($exclusiveMinimum ? ' exclusively' : '') . 'more than ' . $minimum, 'callback' => function ($value) use($minimum, $exclusiveMinimum) {
             return $exclusiveMinimum ? $value > $minimum : $value >= $minimum;
         }]);
     }
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterMultipleOf
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor14
     if (isset($this->_data->multipleOf)) {
         $multiplerOf = $this->_data->multipleOf;
         $this->_validators['multipleOf'] = $this->ValidatorFactory->buildValidator('Callback', ['message' => 'expected to be multiple of ' . $multiplerOf, 'callback' => function ($value) use($multiplerOf) {
             return $value % $multiplerOf == 0;
         }]);
     }
 }
Esempio n. 2
0
 protected function _buildValidators()
 {
     parent::_buildValidators();
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterMaxItems
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor42
     if (isset($this->_data->maxItems)) {
         $maxItems = $this->_data->maxItems;
         $this->_validators['maxItems'] = $this->ValidatorFactory->buildValidator('Callback', ['message' => 'expected to have maximum ' . $maxItems . ' items', 'callback' => function ($value) use($maxItems) {
             return !empty($value) && count($value) > $maxItems ? false : true;
         }]);
     }
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterMinItems
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor45
     if (isset($this->_data->minItems)) {
         $minItems = $this->_data->minItems;
         $this->_validators['minItems'] = $this->ValidatorFactory->buildValidator('Callback', ['message' => 'expected to have minimum ' . $minItems . ' items', 'callback' => function ($value) use($minItems) {
             return !empty($value) && count($value) < $minItems ? false : true;
         }]);
     }
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterUniqueItems
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor49
     if (isset($this->_data->uniqueItems) && $this->_data->uniqueItems) {
         $this->_validators['uniqueItems'] = $this->ValidatorFactory->buildValidator('Callback', ['message' => 'expected to have unique items', 'callback' => function ($value) {
             if (!empty($value)) {
                 if (count($value) != count(array_unique($value))) {
                     return false;
                 }
             }
             return true;
         }]);
     }
     // NOTE: this will overwrite parent's non multivalue enum validator
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterEnum
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor76
     if (isset($this->_data->enum)) {
         $enum = $this->_data->enum;
         $this->_validators['enum'] = $this->ValidatorFactory->buildValidator('MultiEnum', ['message' => 'expected values to be in', 'enum' => (array) $enum]);
     }
 }
Esempio n. 3
0
 /**
  * I cover:
  * Config->format
  * Config->patter
  * Config->minlength
  * Config->maxlength
  * @throws \Exception
  */
 protected function _buildValidators()
 {
     parent::_buildValidators();
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterFormat
     if (isset($this->_data->format)) {
         switch ($this->_data->format) {
             case 'date':
                 $this->_validators['date'] = $this->ValidatorFactory->buildValidator('Regex', ['message' => 'expected date eg YYYY-MM-DD', 'pattern' => '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/']);
                 break;
             case 'date-time':
                 $this->_validators['date-time'] = $this->ValidatorFactory->buildValidator('Regex', ['message' => 'expected date eg YYYY-MM-DD', 'pattern' => '/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}$/']);
                 break;
             case 'password':
                 break;
             default:
                 throw new \Exception('unknown format');
         }
     }
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterPattern
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor33
     if (isset($this->_data->pattern)) {
         $pattern = $this->_data->pattern;
         $this->_validators['pattern'] = $this->ValidatorFactory->buildValidator('Regex', ['message' => 'expected format: ' . $pattern, 'pattern' => $pattern]);
     }
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterMinLength
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor29
     if (isset($this->_data->minLength)) {
         $minLength = $this->_data->minLength;
         $this->_validators['minLength'] = $this->ValidatorFactory->buildValidator('StringLength', ['min' => $minLength, 'messageMinumum' => 'expected not to be shorter than ' . $minLength]);
     }
     // @see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-parameterMaxLength
     // @see http://json-schema.org/latest/json-schema-validation.html#anchor26
     if (isset($this->_data->maxLength)) {
         $maxLength = $this->_data->maxLength;
         $this->_validators['maxLength'] = $this->ValidatorFactory->buildValidator('StringLength', ['max' => $maxLength, 'messageMaxumum' => 'expected not to be longar than ' . $maxLength]);
     }
 }