2017 © Pedro Peláez
 

library statham

A JSON Schema validator

image

phindmarsh/statham

A JSON Schema validator

  • Wednesday, February 25, 2015
  • by phindmarsh
  • Repository
  • 1 Watchers
  • 3 Stars
  • 3,181 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 13 % Grown

The README.md

statham

A JSON Schema validator written in PHP, (*1)

This is a (pretty much straight up) port of the Javascript ZSchema library for validating JSON against JSON schemas, and validating JSON Schemas themselves., (*2)

Why

Motivation for writing this was to validate swagger.io files in PHP, and none of the existing validators worked with recursive schemas, or resolved nested references properly. The Javascript one worked as I needed it to, so I ported it., (*3)

So Statham will: - Not recurse infinitely when validating a schema that references itself - Properly deal with $refs that are located in different schemas, (*4)

Usage


// schemas used during validation $schemas = [ "http://json-schema.org/draft-04/schema#" "http://swagger.io/v2/schema.json#" ]; $statham = new \Statham\Statham(); // download each schema and add to internal cache foreach($schemas as $schema_url){ $schema = json_decode(file_get_contents($schema_url)); $statham->setRemoteReference($schema_url, $schema); } // just validate the schema (no data) $statham->validateSchema($schemas[1]); // validate $json_to_validate against a given schema $statham->validate($json_to_validate, $schemas[1]);

Statham doesn't automatically download externally referenced schemas, (it totally could, but reasons), so use $statham->setRemoteReference($url, $schema_object) for any schemas that will be used during validation., (*5)

Schemas can either be passed as an object (doesn't support schemas as arrays, but again, totally could), or if you've used $statham->setRemoteReference() you can pass it as a string, being the URL of the schema., (*6)

JSON data can only be passed as objects, so just use json_decode($json) (no second true argument) and you'll be fine., (*7)

Todo

It doesn't check formats properly (so emails, dates, etc aren't validated they are anything beyond a string). This is easy enough to do, I just haven't yet., (*8)

The Versions

25/02 2015

dev-master

9999999-dev

A JSON Schema validator

  Sources   Download

MIT

25/02 2015

1.0.0

1.0.0.0

A JSON Schema validator

  Sources   Download

MIT