Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
![Supported Dialects](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Fphp-justinrainbow-json-schema%2Fsupported_versions.json)

A PHP Implementation for validating `JSON` Structures against a given `Schema` with support for `Schemas` of Draft-3,
Draft-4, Draft-6,Draft-7 or Draft 2019-09.
Draft-4, Draft-6,Draft-7, Draft 2019-09 or Draft 2020-12.

Features of newer Drafts might not be supported. See [Table of All Versions of Everything](https://json-schema.org/specification-links.html#table-of-all-versions-of-everything) to get an overview
of all existing Drafts. See [json-schema](http://json-schema.org/) for more details about the JSON Schema specification
Expand All @@ -19,6 +19,7 @@ The documentation for this library is available at [jsonrainbow.github.io/docs](
![Draft 6](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Fphp-justinrainbow-json-schema%2Fcompliance%2Fdraft6.json)
![Draft 7](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Fphp-justinrainbow-json-schema%2Fcompliance%2Fdraft7.json)
![Draft 2019-09](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Fphp-justinrainbow-json-schema%2Fcompliance%2Fdraft2019-09.json)
![Draft 2020-12](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Fphp-justinrainbow-json-schema%2Fcompliance%2Fdraft2020-12.json)

## Installation

Expand Down Expand Up @@ -211,13 +212,16 @@ third argument to `Validator::validate()`, or can be provided as the third argum
| `Constraint::CHECK_MODE_DISABLE_FORMAT` | Do not validate "format" constraints |
| `Constraint::CHECK_MODE_VALIDATE_SCHEMA` | Validate the schema as well as the provided document |
| `Constraint::CHECK_MODE_STRICT` [^3] | Validate the scheme using strict mode using the specified draft |
| `Constraint::CHECK_MODE_FORMAT_ASSERTION` [^5] | Validate "format" constraints, which are annotations by default |

[^1]: Please note that using `CHECK_MODE_COERCE_TYPES` or `CHECK_MODE_APPLY_DEFAULTS` will modify your
original data.
[^2]: `CHECK_MODE_EARLY_COERCE` has no effect unless used in combination with `CHECK_MODE_COERCE_TYPES`. If
enabled, the validator will use (and coerce) the first compatible type it encounters, even if the
schema defines another type that matches directly and does not require coercion.
[^3]: `CHECK_MODE_STRICT` only can be used for Draft-6, Draft 7 or Draft 2019-09 at this point.
[^3]: `CHECK_MODE_STRICT` only can be used for Draft-6, Draft 7, Draft 2019-09 or Draft 2020-12 at this point.
[^5]: `CHECK_MODE_FORMAT_ASSERTION` only affects Draft 2020-12, other drafts validate "format" constraints unless
`CHECK_MODE_DISABLE_FORMAT` is set. `CHECK_MODE_DISABLE_FORMAT` takes precedence when both are set.

## Running the tests

Expand Down
166 changes: 166 additions & 0 deletions dist/schema/json-schema-draft-2020-12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://json-schema.org/draft/2020-12/schema",
"title": "Core and Validation specifications meta-schema",
"type": ["object", "boolean"],
"$defs": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"nonNegativeInteger": {
"type": "integer",
"minimum": 0
},
"nonNegativeIntegerDefault0": {
"allOf": [
{ "$ref": "#/$defs/nonNegativeInteger" },
{ "default": 0 }
]
},
"anchorString": {
"type": "string",
"pattern": "^[A-Za-z_][-A-Za-z0-9._]*$"
},
"simpleTypes": {
"enum": ["array", "boolean", "integer", "null", "number", "object", "string"]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true,
"default": []
}
},
"properties": {
"$id": {
"type": "string",
"format": "uri-reference",
"$comment": "Non-empty fragments not allowed.",
"pattern": "^[^#]*#?$"
},
"$schema": { "type": "string", "format": "uri" },
"$anchor": { "$ref": "#/$defs/anchorString" },
"$ref": { "type": "string", "format": "uri-reference" },
"$dynamicRef": { "type": "string", "format": "uri-reference" },
"$dynamicAnchor": { "$ref": "#/$defs/anchorString" },
"$vocabulary": {
"type": "object",
"propertyNames": { "type": "string", "format": "uri" },
"additionalProperties": { "type": "boolean" }
},
"$comment": { "type": "string" },
"$defs": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"definitions": {
"$comment": "Retained for backwards compatibility, replaced by $defs.",
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"title": { "type": "string" },
"description": { "type": "string" },
"default": true,
"deprecated": { "type": "boolean", "default": false },
"readOnly": { "type": "boolean", "default": false },
"writeOnly": { "type": "boolean", "default": false },
"examples": { "type": "array", "items": true },
"multipleOf": { "type": "number", "exclusiveMinimum": 0 },
"maximum": { "type": "number" },
"exclusiveMaximum": { "type": "number" },
"minimum": { "type": "number" },
"exclusiveMinimum": { "type": "number" },
"maxLength": { "$ref": "#/$defs/nonNegativeInteger" },
"minLength": { "$ref": "#/$defs/nonNegativeIntegerDefault0" },
"pattern": { "type": "string", "format": "regex" },
"prefixItems": { "$ref": "#/$defs/schemaArray" },
"items": { "$ref": "#" },
"maxItems": { "$ref": "#/$defs/nonNegativeInteger" },
"minItems": { "$ref": "#/$defs/nonNegativeIntegerDefault0" },
"uniqueItems": { "type": "boolean", "default": false },
"contains": { "$ref": "#" },
"maxContains": { "$ref": "#/$defs/nonNegativeInteger" },
"minContains": { "$ref": "#/$defs/nonNegativeIntegerDefault0" },
"unevaluatedItems": { "$ref": "#" },
"maxProperties": { "$ref": "#/$defs/nonNegativeInteger" },
"minProperties": { "$ref": "#/$defs/nonNegativeIntegerDefault0" },
"required": { "$ref": "#/$defs/stringArray" },
"additionalProperties": { "$ref": "#" },
"unevaluatedProperties": { "$ref": "#" },
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"propertyNames": { "format": "regex" },
"default": {}
},
"dependentSchemas": {
"type": "object",
"additionalProperties": { "$ref": "#" }
},
"dependentRequired": {
"type": "object",
"additionalProperties": { "$ref": "#/$defs/stringArray" }
},
"$recursiveAnchor": {
"$comment": "\"$recursiveAnchor\" has been replaced by \"$dynamicAnchor\".",
"$ref": "#/$defs/anchorString",
"deprecated": true
},
"$recursiveRef": {
"$comment": "\"$recursiveRef\" has been replaced by \"$dynamicRef\".",
"type": "string",
"format": "uri-reference",
"deprecated": true
},
"dependencies": {
"$comment": "Retained for backwards compatibility, replaced by dependentSchemas/dependentRequired.",
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/$defs/stringArray" }
]
}
},
"propertyNames": { "$ref": "#" },
"const": true,
"enum": {
"type": "array",
"items": true,
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/$defs/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/$defs/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"format": { "type": "string" },
"contentEncoding": { "type": "string" },
"contentMediaType": { "type": "string" },
"contentSchema": { "$ref": "#" },
"if": { "$ref": "#" },
"then": { "$ref": "#" },
"else": { "$ref": "#" },
"allOf": { "$ref": "#/$defs/schemaArray" },
"anyOf": { "$ref": "#/$defs/schemaArray" },
"oneOf": { "$ref": "#/$defs/schemaArray" },
"not": { "$ref": "#" }
},
"default": true
}
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ parameters:
path: src/JsonSchema/Constraints/Factory.php

-
message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:getConfig\\(\\) should return int\\<0, 1023\\> but returns int\\.$#"
message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:getConfig\\(\\) should return int\\<0, 3071\\> but returns int\\.$#"
count: 1
path: src/JsonSchema/Constraints/Factory.php

-
message: "#^Property JsonSchema\\\\Constraints\\\\Factory\\:\\:\\$checkMode \\(int\\<0, 1023\\>\\) does not accept int\\.$#"
message: "#^Property JsonSchema\\\\Constraints\\\\Factory\\:\\:\\$checkMode \\(int\\<0, 3071\\>\\) does not accept int\\.$#"
count: 2
path: src/JsonSchema/Constraints/Factory.php

Expand Down
1 change: 1 addition & 0 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface
public const CHECK_MODE_ONLY_REQUIRED_DEFAULTS = 0x00000080;
public const CHECK_MODE_VALIDATE_SCHEMA = 0x00000100;
public const CHECK_MODE_STRICT = 0x00000200;
public const CHECK_MODE_FORMAT_ASSERTION = 0x00000800;

/**
* Bubble down the path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use JsonSchema\Entity\ErrorBagProxy;
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Rfc3339;
use JsonSchema\Tool\Validator\EmailValidator;
use JsonSchema\Tool\Validator\RelativeReferenceValidator;
use JsonSchema\Tool\Validator\UriValidator;

Expand Down Expand Up @@ -108,7 +109,7 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
break;

case 'email':
if (filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE | FILTER_FLAG_EMAIL_UNICODE) === null) {
if (!EmailValidator::isValid($value)) {
$this->addError(ConstraintError::FORMAT_EMAIL(), $path, ['format' => $schema->format]);
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Exception\RuntimeException;
use JsonSchema\Rfc3339;
use JsonSchema\Tool\Validator\EmailValidator;
use JsonSchema\Tool\Validator\RelativeReferenceValidator;
use JsonSchema\Tool\Validator\UriValidator;

Expand Down Expand Up @@ -124,7 +125,7 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
break;

case 'email':
if (filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE | FILTER_FLAG_EMAIL_UNICODE) === null) {
if (!EmailValidator::isValid($value)) {
$this->addError(ConstraintError::FORMAT_EMAIL(), $path, ['format' => $schema->format]);
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Exception\RuntimeException;
use JsonSchema\Rfc3339;
use JsonSchema\Tool\Validator\EmailValidator;
use JsonSchema\Tool\Validator\RelativeReferenceValidator;
use JsonSchema\Tool\Validator\UriValidator;

Expand Down Expand Up @@ -133,7 +134,7 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
}
break;
case 'email':
if (filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE | FILTER_FLAG_EMAIL_UNICODE) === null) {
if (!EmailValidator::isValid($value)) {
$this->addError(ConstraintError::FORMAT_EMAIL(), $path, ['format' => $schema->format]);
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace JsonSchema\Constraints\Drafts\Draft2020;

use JsonSchema\ConstraintError;
use JsonSchema\Constraints\ConstraintInterface;
use JsonSchema\Entity\ErrorBagProxy;
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Tool\EcmaPatternConverter;

class AdditionalPropertiesConstraint implements ConstraintInterface
{
use ErrorBagProxy;

/** @var Factory */
private $factory;

public function __construct(?Factory $factory = null)
{
$this->factory = $factory ?: new Factory();
$this->initialiseErrorBag($this->factory);
}

public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
{
if (!property_exists($schema, 'additionalProperties')) {
return;
}

if ($schema->additionalProperties === true) {
return;
}

if (!is_object($value)) {
return;
}

$additionalProperties = get_object_vars($value);

if (isset($schema->properties)) {
$additionalProperties = array_diff_key($additionalProperties, (array) $schema->properties);
}

if (isset($schema->patternProperties)) {
$patterns = array_keys(get_object_vars($schema->patternProperties));

foreach ($additionalProperties as $key => $_) {
foreach ($patterns as $pattern) {
if (preg_match(EcmaPatternConverter::toPcre($pattern), (string) $key)) {
unset($additionalProperties[$key]);
break;
}
}
}
}

if (is_object($schema->additionalProperties)) {
foreach ($additionalProperties as $key => $additionalPropertiesValue) {
$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($additionalPropertiesValue, $schema->additionalProperties, ($path ?? new JsonPointer(''))->withAppendedPath($key), $i);

// The property is permitted by the schema, so it is never an "additional property"
// error; what it failed is the sub-schema, and those errors carry the reason.
if (!$schemaConstraint->isValid()) {
$this->addErrors($schemaConstraint->getErrors());
}

unset($additionalProperties[$key]);
}
}

foreach ($additionalProperties as $key => $additionalPropertiesValue) {
$this->addError(ConstraintError::ADDITIONAL_PROPERTIES(), ($path ?? new JsonPointer(''))->withAppendedPath($key), ['found' => $key]);
}
}
}
42 changes: 42 additions & 0 deletions src/JsonSchema/Constraints/Drafts/Draft2020/AllOfConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace JsonSchema\Constraints\Drafts\Draft2020;

use JsonSchema\ConstraintError;
use JsonSchema\Constraints\ConstraintInterface;
use JsonSchema\Entity\ErrorBagProxy;
use JsonSchema\Entity\JsonPointer;

class AllOfConstraint implements ConstraintInterface
{
use ErrorBagProxy;

/** @var Factory */
private $factory;

public function __construct(?Factory $factory = null)
{
$this->factory = $factory ?: new Factory();
$this->initialiseErrorBag($this->factory);
}

public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
{
if (!property_exists($schema, 'allOf')) {
return;
}

foreach ($schema->allOf as $allOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $allOfSchema, $path, $i);

if ($schemaConstraint->isValid()) {
continue;
}
$this->addError(ConstraintError::ALL_OF(), $path);
$this->addErrors($schemaConstraint->getErrors());
}
}
}
Loading