Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src_dir: .
coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/bin
/report
/report
/build
26 changes: 21 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1

before_script:
- composer install --dev

script: phpunit --coverage-text
matrix:
allow_failures:
- php: hhvm
fast_finish: true


install:
- composer install --dev --no-interaction


script:
- mkdir -p build/logs
- composer test
- mv build/logs/clover.xml ./coverage.xml


after_success:
- bash <(curl -s https://codecov.io/bash)
164 changes: 116 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,133 @@
# PHP JSON Schema Generator
======================

[![Build Status](https://secure.travis-ci.org/solvire/php-json-schema-generator.png)](http://travis-ci.org/solvire/php-json-schema-generator)
![Build Status](https://travis-ci.org/evaisse/php-json-schema-generator.svg?branch=master#)
[![codecov](https://codecov.io/gh/evaisse/php-json-schema-generator/branch/master/graph/badge.svg)](https://codecov.io/gh/evaisse/php-json-schema-generator)

Package: php-json-schema-generator
Originaly forked from [solvire/php-json-schema-generator](https://github.com/solvire/php-json-schema-generator)

Introduction to json schema below (and tools) :

- http://json-schema.org — reference
- http://www.jsonschemavalidator.net - validator (not 100% valid)
- https://www.openapis.org - use json schema to define REST API docs
- https://jsonschema.net/#/editor - convenient editor for json schema

To validate your structure against a given schema, you can use :

PHP JSON Schema Generator
- http://json-guard.thephpleague.com

## About JSON Schema

JSON has become a mainstay in the vast HTTP toolbox. In order to make JSON more stable and to increase the longevity of the data structure there must be some standards put into place. These standards will help to define the structure in a way that the industry can rely on. A uniform way to parse the data and interpret the meaning of the data elements can be found in building a schema that represents it.
## Quickstart

Due to the flexible nature of JSON a solid Schema definition has been slow coming. At this time there is an internet draft being worked on.
(http://tools.ietf.org/html/draft-zyp-json-schema-04)
Install using composer

The uses/pros/cons and other discussions related to what a schema can and cannot do for you are beyond the scope of this document. Seek your creativity for possible scenarios.

## About This Tool

It is a bit tedious to have to manually write out a JSON Schema every time a new REST point or Data object is created. Because JSON can be used to represent a variety of data objects it can be helpful to have a dynamic way to map from one object to a JSON Schema. A php array is considered an object here for the sake of ease of communication.

The goal of the tool is to provide an trivial implement for generating a JSON Schema off a wide range of objects. Some objects provide more options for rich schema generation and some do not. JSON itself is very light on metadata so there is a requirement to infer certain meanings based on the structure of the objects.

### Parser Objects Supported
* JSON string
* Defined [RFC 4627](http://tools.ietf.org/html/rfc4627)
* No validation yet
composer require evaisse/php-json-schema-generator

Most simple case

$output = JSONSchemaGenerator\Generator::fromJson('{"a":{"b":2}');

// $output ==> json string
// {
// "$schema": "http://json-schema.org/draft-04/schema#",
// "type": "object",
// "properties": {
// "a": {
// "type": "object",
// "properties": {
// "b": {
// "type": "integer"
// }
// },
// "required": ["b"]
// }
// },
// "required": ["a"]
// }

Default configuration values

[
'schema_id' => null,
'properties_required_by_default' => true,
'schema_uri' => 'http://json-schema.org/draft-04/schema#',
'schema_title' => null,
'schema_description' => null,
'schema_type' => null,
"items_schema_collect_mode" => 0,
'schema_required_field_names' => []
]

Advanced usage

$result = Generator::fromJson($this->addressJson1, [
'schema_id' => 'http://foo.bar/schema'
]);

/*

{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://foo.bar/schema",
"type": "object",
"properties": {
"a": {
"type": "object",
"id": "http://foo.bar/schema/a",
"properties": {
"b": {
"id": "http://foo.bar/schema/a/b",
"type": "integer"
}
}
}
}

*/


// if you want items as strict lists instead of "anyOf" type
$result = Generator::fromJson($this->addressJson1, [
'schema_id' => 'http://bar.foo/schema2',
'schema_title' => 'coucouc',
'schema_description' => 'desc',
"items_schema_collect_mode" => Definition::ITEMS_AS_LIST,
]);

/*
{
"$schema":"http:\/\/json-schema.org\/draft-04\/schema#",
...
"properties": {
"phoneNumber":{
"id":"http:\/\/bar.foo\/schema2\/phoneNumber",
"type":"array",
"items": [
{"id":"http:\/\/bar.foo\/schema2\/0",...},
{"id":"http:\/\/bar.foo\/schema2\/1",...}}
*/


For more advanced usage, see `tests/JSONSchemaGenerator/Tests/GeneratorTest.php`


### Parsers To Be Supported
* JSON/Schema Object
* Loading the Schema + Properties manually
* Can be built up easier with an API
* Array
* Simple hash
* API to load an array
* Will validate the array structure
* ArrayObject
* Similar to array hash
* Doctrine Entity
* Use the Doctrine 2 infrastructure
* Generate the schema off the doctrine metadata
* Map to DB settings
* Allow map overrides
* Extensible Objects
* Load user defined parsers
* Inherit major functionality

## Installation
Simple, assuming you use composer. Add the below lines to your composer.json and run composer update.
## Testing

just run phpunit through

"require": {
"solvire/php-json-schema-generator": "dev-master"
}
composer test

## Testing
PHPUnit should be included with the composer require-dev configuration. The installation will put an
executable in the vendor/bin directly so it can be run from there.
debug with

DEBUG=true composer test -- --filter="SearchWord" # for filtering *SearchWord* test case with output debugging

Run:

$ vendor/bin/phpunit
## Roadmap

- Adjust schema comparison using re-ordering of properties to compare two schema against
their semantic values instead of just comparing their JSON form. For example `{ a: 1, b: 2 }`, and `{ b: 2, a: 1 }`
should result in the same schema.

- provide an option to allow null values in most fields `("type": ["string", "null"]}`
10 changes: 0 additions & 10 deletions build/artifacts/logs/junit.xml

This file was deleted.

40 changes: 29 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
{
"name": "solvire/php-json-schema-generator",
"name": "evaisse/php-json-schema-generator",
"type": "library",
"description": "A JSON Schema Generator written in PHP. The implementation is based on current internet drafts.",
"keywords": ["PHP", "JSON", "Schema", "generator", "validation", "parser"],
"homepage": "https://github.com/solvire/php-json-schema-generator",
"description": "A JSON Schema Generator.",
"keywords": [
"PHP",
"JSON",
"Schema",
"generator",
"validation",
"parser"
],
"homepage": "https://github.com/evaisse/php-json-schema-generator",
"license": "MIT",
"authors": [
{
"name": "Steven Scott",
"email": "steve@openfoc.us"
},
{
"name": "Emmanuel VAÏSSE",
"email": "evaisse@gmail.com"
}
],
"require": {
"php": ">=5.4.0"
"php": ">=5.6.0",
"ext-json": "*"
},
"require-dev": {
"justinrainbow/json-schema" : "*",
"phpunit/phpunit": "3.7.*",
"justinrainbow/json-schema": "*"
"phpunit/phpunit": "5.7.*",
"league/json-guard": "1.*",
"league/json-reference": "1.*",
"php-coveralls/php-coveralls": "^2.0.0"
},
"autoload": {
"psr-0": {
"JSONSchema\\Tests": "tests/",
"JSONSchema": "src/"
"psr-4": {
"JSONSchemaGenerator\\Tests\\": "tests/JSONSchemaGenerator/Tests/",
"JSONSchemaGenerator\\": "src/JSONSchemaGenerator/"
}
},
"scripts": {
"tests": [
"php vendor/phpunit/phpunit/phpunit"
]
}
}
Loading