/** * {@inheritDoc} */ public function check($element, $schema = null, $path = null, $i = null) { if (!isset($schema->format)) { return; } switch ($schema->format) { case 'date': if (!($date = $this->validateDateTime($element, 'Y-m-d'))) { $errorMsg = I18n::t("constraints.format.date", ['date' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'time': if (!$this->validateDateTime($element, 'H:i:s')) { $errorMsg = I18n::t("constraints.format.time", ['time' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'date-time': if (null === Rfc3339::createFromString($element)) { $errorMsg = I18n::t("constraints.format.datetime", ['datetime' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'utc-millisec': if (!$this->validateDateTime($element, 'U')) { $errorMsg = I18n::t("constraints.format.utcmilli", ['utcmilli' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'regex': if (!$this->validateRegex($element)) { $errorMsg = I18n::t("constraints.format.regex", ['regex' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'color': if (!$this->validateColor($element)) { $errorMsg = I18n::t("constraints.format.color"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'style': if (!$this->validateStyle($element)) { $errorMsg = I18n::t("constraints.format.style"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'phone': if (!$this->validatePhone($element)) { $errorMsg = I18n::t("constraints.format.phone"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'uri': if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) { $errorMsg = I18n::t("constraints.format.url"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'email': if (null === filter_var($element, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE)) { $errorMsg = I18n::t("constraints.format.email"); $this->addError($path, "Invalid email", 'format', array('format' => $schema->format)); } break; case 'ip-address': case 'ipv4': if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4)) { $errorMsg = I18n::t("constraints.format.ipv4"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'ipv6': if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV6)) { $errorMsg = I18n::t("constraints.format.ipv6"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'host-name': case 'hostname': if (!$this->validateHostname($element)) { $errorMsg = I18n::t("constraints.format.hostname"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; default: // Empty as it should be: // The value of this keyword is called a format attribute. It MUST be a string. // A format attribute can generally only validate a given set of instance types. // If the type of the instance to validate is not in this set, validation for // this format attribute and instance SHOULD succeed. // http://json-schema.org/latest/json-schema-validation.html#anchor105 break; } }
/** * {@inheritDoc} */ public function check($element, $schema = null, $path = null, $i = null) { if (!isset($schema->format)) { return; } switch ($schema->format) { case 'date': if (!($date = $this->validateDateTime($element, 'Y-m-d'))) { $this->addError($path, sprintf('Invalid date %s, expected format YYYY-MM-DD', json_encode($element)), 'format', array('format' => $schema->format)); } break; case 'time': if (!$this->validateDateTime($element, 'H:i:s')) { $this->addError($path, sprintf('Invalid time %s, expected format hh:mm:ss', json_encode($element)), 'format', array('format' => $schema->format)); } break; case 'date-time': if (null === Rfc3339::createFromString($element)) { $this->addError($path, sprintf('Invalid date-time %s, expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm', json_encode($element)), 'format', array('format' => $schema->format)); } break; case 'utc-millisec': if (!$this->validateDateTime($element, 'U')) { $this->addError($path, sprintf('Invalid time %s, expected integer of milliseconds since Epoch', json_encode($element)), 'format', array('format' => $schema->format)); } break; case 'regex': if (!$this->validateRegex($element)) { $this->addError($path, 'Invalid regex format ' . $element, 'format', array('format' => $schema->format)); } break; case 'color': if (!$this->validateColor($element)) { $this->addError($path, "Invalid color", 'format', array('format' => $schema->format)); } break; case 'style': if (!$this->validateStyle($element)) { $this->addError($path, "Invalid style", 'format', array('format' => $schema->format)); } break; case 'phone': if (!$this->validatePhone($element)) { $this->addError($path, "Invalid phone number", 'format', array('format' => $schema->format)); } break; case 'uri': if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) { $this->addError($path, "Invalid URL format", 'format', array('format' => $schema->format)); } break; case 'email': if (null === filter_var($element, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE)) { $this->addError($path, "Invalid email", 'format', array('format' => $schema->format)); } break; case 'ip-address': case 'ipv4': if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4)) { $this->addError($path, "Invalid IP address", 'format', array('format' => $schema->format)); } break; case 'ipv6': if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV6)) { $this->addError($path, "Invalid IP address", 'format', array('format' => $schema->format)); } break; case 'host-name': case 'hostname': if (!$this->validateHostname($element)) { $this->addError($path, "Invalid hostname", 'format', array('format' => $schema->format)); } break; default: // Empty as it should be: // The value of this keyword is called a format attribute. It MUST be a string. // A format attribute can generally only validate a given set of instance types. // If the type of the instance to validate is not in this set, validation for // this format attribute and instance SHOULD succeed. // http://json-schema.org/latest/json-schema-validation.html#anchor105 break; } }