protected function validValue(Entity $entity)
    {
        foreach ($entity->getFields() as $field => $detail) {
            $value = $entity[$field];

            // Convert value type
            if ($value instanceof \DateTimeInterface) {
                $value = $value->format($this->db->getDateFormat());
            }

            if ($value instanceof JsonSerializer) {
                $value = json_encode($value);
            }

            if (\is_object($value) && \is_callable([$value, '__toString'])) {
                $value = (string) $value;
            }

            // Start prepare default value
            if (is_array($value) || is_object($value)) {
                $value = null;
            }

            if ($value === null) {
                // This field is null and the db column is not nullable, use db default value.
                if (strtolower($detail->Null) === 'no') {
                    $entity[$field] = $detail->Default;
                }
            } else {
                // Convert to correct type.
                $dataType = DataType::getInstance($this->db->getName());

                $type = explode(' ', $detail->Type)[0];
                $type = explode('(', $type)[0];

                settype($value, $dataType::getPhpType($type));

                $entity[$field] = $value;
            }
        }

        return $entity;
    }