public function validate($data, $schema = null)
    {
        if (null === $schema && isset($data->{'$schema'})) {
            $schema = $data->{'$schema'};
        }

        if (is_string($schema)) {
            $schema = $this->loadSchema($schema);
        } elseif (is_object($schema)) {
            $this->assertSchemaValid($schema);
        } else {
            throw new InvalidSchemaException(sprintf(
                'The schema must be given as string, object or in the "$schema" '.
                'property of the JSON data. Got: %s',
                is_object($schema) ? get_class($schema) : gettype($schema)
            ));
        }

        $this->validator->reset();

        try {
            $this->validator->check($data, $schema);
        } catch (InvalidArgumentException $e) {
            throw new InvalidSchemaException(sprintf(
                'The schema is invalid: %s',
                $e->getMessage()
            ), 0, $e);
        }

        $errors = array();

        if (!$this->validator->isValid()) {
            $errors = (array) $this->validator->getErrors();

            foreach ($errors as $key => $error) {
                $prefix = $error['property'] ? $error['property'].': ' : '';
                $errors[$key] = $prefix.$error['message'];
            }
        }

        return $errors;
    }