Code Monkey home page Code Monkey logo

php-json-schema's Introduction

This library provides JSON schema validation using the schema found at http://json-schema.org. Note that it is not yet feature complete, but does support basic validation. The JSON schema draft can be found at http://tools.ietf.org/html/draft-zyp-json-schema-03

Requirements

  • PHP 5.3 or greater (requires namespace and closure support)

Usage

$someJson = '{"foo":"bar"}';
$jsonObject = json_decode($someJson);

$validator = new JsonValidator('/path/to/yourschema.json');

$validator->validate($jsonObject);

Supported Types

Types may be defined as either a single string type name, or an array of allowable type names.

  • string
  • number
  • integer
  • boolean
  • object
  • array
  • null
  • any

Supported Definitions

Not all definitions are yet supported, but here is a list of those which are:

  • properties (object)
  • additionalProperties (object)
  • required (all)
  • pattern (string)
  • minLength (string)
  • maxLength (string)
  • format (string, number, integer)
  • minimum (number, integer)
  • maximum (number, integer)
  • exclusiveMinimum (number, integer)
  • exclusiveMaximum (number, integer)
  • divisibleBy (number, integer)
  • enum (array)
  • minItems (array)
  • maxItems (array)
  • uniqueItems (array)
  • items (array)
  • disallow (all)

The following definitions are not yet supported:

  • patternProperties
  • dependencies
  • extends
  • id
  • $ref
  • $schema

php-json-schema's People

Contributors

czhang-ck avatar hasbridge avatar mtdowling avatar slopjong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-json-schema's Issues

Load a schema from string rather than file

The constructor only has one method of loading a schema, via a file path determined by $schemaFile. Can we allow for this to be some JSON string and not use an actual file?

Issue with additionalProperties

Hi

First of all nice job on the validator
However, I have stumbled upon an issue with the additioalProperties
Consider the below schema and data, which I tried to validate online at
https://json-schema-validator.herokuapp.com/
and it does not pass validation (as it should not)

Schema
{
"type": "object",
"properties": {
"class": {
"type": "string"
},
"meta_keywords": {
"type": "string"
},
"meta_description": {
"type": "string"
},
"header": {
"type": "string"
},
"footer": {
"type": "string"
},
"page_title": {
"type": "string"
},
"containers": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"template": {
"type": "string"
},
"columns": {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"type": "integer"
},
"uniqueItems": true
}
}
}
}
}
},
"required": [
"header",
"footer",
"page_title",
"containers"
]
}

data
{"page_title":"wefewfw","meta_keywords":"","meta_description":"","containers":[{"id":1412867034986,"template":"6-6","columns":{"column1":["13"],"column2":["14"]}}],"header":"10","footer":"9"}

However, when I use your class it passes.

Regards

Array of objects

Hi!

Thanks for this project! Really cool!
I'm facing some problems with array of objects(looks like the property is ignored in this situation). Does php-json-schema support this feature?

Regards,
Matheus

Merge with justinrainbow/json-schema

I just found this on packagist. I have contributed to justinrainbow/json-schema in the past because we needed it for composer.

I want to propose merging the two projects.

I'm not against competition, but if we can agree and join forces it's a huge win imo. Especially for projects that are as similar as these. If you could bring your expertise into the json-schema project, that would be huge imo.

It would also show the PHP community standing together, just saying.

Think about it. :)

Poke @justinrainbow @digitalkaoz

PS: Please do not take offense in this, I have great respect for you creating a json schema validator in the first place!

Please tag 0.2.1

The following code causes a bug where the minimum is not enforced if the value is 0.

if (isset($schema->minimum) && $schema->minimum) {

It has been fixed in a later commit but the latest version composer can use is 0.2.0. Please tag a 0.2.1 on master.

Nested required properties

Hi,
When I'm validating a json against a json schema which has nested required properties it's not working properly (it passes the validation test even if required properties are missing). For example this json:
{
"users": {
"name": "Roland",
"surname": "Gaski",
"ages": 100
}
}
is being validated by this json schema:
{
"type": "object",
"properties": {
"users": {
"id": "users",
"type": "object",
"properties": {
"name": {
"id": "name",
"type": "string"
},
"surname": {
"id": "surname",
"type": "string"
},
"age": {
"id": "age",
"type": "integer"
}
},
"required": [
"name",
"surname",
"age"
]
}
},
"required": [
"users"
]
}
And it passes the test, however "age" property is required so it shouldn't, is there an easy way to check nested required properties?
Thanks for help

Bug: Unique Array when array items are object fails

Please,

I think there is a bug, comparing unique array, when items are objects.

Consider:
protected function checkUniqueItems($entity, $schema, $entityName) {
if (isset($schema->uniqueItems) && $schema->uniqueItems) {
if (count(array_unique($entity)) != count($entity)) {

With this:
protected function checkUniqueItems($entity, $schema, $entityName) {
if (isset($schema->uniqueItems) && $schema->uniqueItems) {
if (count(array_unique($entity,SORT_REGULAR)) != count($entity)) {

Adding SORT_REGULAR (or perhaps depending on type of ites) flag on array_unique items

Thanks

Place exceptions into their own files

Placing the definitions of "ValidationException" and "SchemaException" inside the Validator file is no good for autoloading through Composer.

If one has not loaded instantiated the Validator class, Composer will believe it hasn't loaded the class "ValidationException" and it will attempt to find it by file. Ka boom.

Suggestion: Place the exception classes into their own files.

I can do a pull request if you want.

required

Hi
I have the validation working for the types
but when I remove a node like firstname that is required I don't get an Exception ?
isn't this supported ?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.