src/JsonValidator.php
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;
    }
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;
    }
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 ; }
Validates JSON data against a schema.

The schema may be passed as file path or as object returned from
`json_decode($schemaFile)`.

@param mixed              $data   The decoded JSON data
@param string|object|null $schema The schema file or object. If `null`,
the validator will look for a `$schema`
property

@return string[] The errors found during validation. Returns an empty
array if no errors were found

@throws InvalidSchemaException If the schema is invalid
Validates JSON data against a schema .