Skip to content

Commit 39aa0db

Browse files
committed
implementing draft-06, draft-07, ajv tests passed
1 parent 8da5f1c commit 39aa0db

28 files changed

+2301
-125
lines changed

.gitmodules

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "spec/JSON-Schema-Test-Suite"]
22
path = spec/JSON-Schema-Test-Suite
3-
url = https://github.com/swaggest/JSON-Schema-Test-Suite.git
3+
url = https://github.com/swaggest/JSON-Schema-Test-Suite.git
4+
[submodule "spec/ajv"]
5+
path = spec/ajv
6+
url = https://github.com/epoberezkin/ajv.git

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
High definition PHP structures with JSON-schema based validation.
99

10+
Supported schemas:
11+
[JSON Schema Draft 7](http://json-schema.org/specification-links.html#draft-7)
12+
[JSON Schema Draft 6](http://json-schema.org/specification-links.html#draft-6)
13+
[JSON Schema Draft 4](http://json-schema.org/specification-links.html#draft-4)
14+
1015
## Installation
1116

1217
```
@@ -86,6 +91,17 @@ JSON
8691
)); // Exception: Required property missing: id at #->properties:orders->items[1]->#/definitions/order
8792
```
8893

94+
You can also call `Schema::import` on string `uri` to schema json data.
95+
```php
96+
$schema = Schema::import('http://localhost:1234/my_schema.json');
97+
```
98+
99+
Or with boolean argument.
100+
```php
101+
$schema = Schema::import(true); // permissive schema, always validates
102+
$schema = Schema::import(false); // restrictive schema, always invalidates
103+
```
104+
89105
### PHP structured classes with validation
90106

91107
```php
@@ -403,4 +419,14 @@ $schema = SwaggerSchema::schema()->in(json_decode(
403419
file_get_contents(__DIR__ . '/../../../../spec/petstore-swagger.json')
404420
), $context);
405421
$this->assertInstanceOf(CustomSchema::className(), $schema->definitions['User']);
406-
```
422+
```
423+
424+
## Code quality and test coverage
425+
426+
Some code quality best practices are intentionally violated here
427+
(see [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/swaggest/php-json-schema/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/swaggest/php-json-schema/?branch=master)
428+
) to allow best performance at maintenance cost.
429+
430+
Those violations are secured by comprehensive test coverage:
431+
* draft-04, draft-06, draft-07 of [JSON-Schema-Test-Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite)
432+
* test cases (excluding `$data` and few tests) of [epoberezkin/ajv](https://github.com/epoberezkin/ajv/tree/master/spec) (a mature js implementation)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"type": "library",
55
"require": {
66
"php": ">=5.4",
7-
"phplang/scope-exit": "^1.0"
7+
"phplang/scope-exit": "^1.0",
8+
"swaggest/json-diff": "^1.0"
89
},
910
"require-dev": {
1011
"phpunit/phpunit": "4.8.23",

spec/JSON-Schema-Test-Suite

spec/ajv

Submodule ajv added at ef40fbb

spec/json-schema-draft6.json

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-06/schema#",
3+
"$id": "http://json-schema.org/draft-06/schema#",
4+
"title": "Core schema meta-schema",
5+
"definitions": {
6+
"schemaArray": {
7+
"type": "array",
8+
"minItems": 1,
9+
"items": { "$ref": "#" }
10+
},
11+
"nonNegativeInteger": {
12+
"type": "integer",
13+
"minimum": 0
14+
},
15+
"nonNegativeIntegerDefault0": {
16+
"allOf": [
17+
{ "$ref": "#/definitions/nonNegativeInteger" },
18+
{ "default": 0 }
19+
]
20+
},
21+
"simpleTypes": {
22+
"enum": [
23+
"array",
24+
"boolean",
25+
"integer",
26+
"null",
27+
"number",
28+
"object",
29+
"string"
30+
]
31+
},
32+
"stringArray": {
33+
"type": "array",
34+
"items": { "type": "string" },
35+
"uniqueItems": true,
36+
"default": []
37+
}
38+
},
39+
"type": ["object", "boolean"],
40+
"properties": {
41+
"$id": {
42+
"type": "string",
43+
"format": "uri-reference"
44+
},
45+
"$schema": {
46+
"type": "string",
47+
"format": "uri"
48+
},
49+
"$ref": {
50+
"type": "string",
51+
"format": "uri-reference"
52+
},
53+
"title": {
54+
"type": "string"
55+
},
56+
"description": {
57+
"type": "string"
58+
},
59+
"default": {},
60+
"examples": {
61+
"type": "array",
62+
"items": {}
63+
},
64+
"multipleOf": {
65+
"type": "number",
66+
"exclusiveMinimum": 0
67+
},
68+
"maximum": {
69+
"type": "number"
70+
},
71+
"exclusiveMaximum": {
72+
"type": "number"
73+
},
74+
"minimum": {
75+
"type": "number"
76+
},
77+
"exclusiveMinimum": {
78+
"type": "number"
79+
},
80+
"maxLength": { "$ref": "#/definitions/nonNegativeInteger" },
81+
"minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
82+
"pattern": {
83+
"type": "string",
84+
"format": "regex"
85+
},
86+
"additionalItems": { "$ref": "#" },
87+
"items": {
88+
"anyOf": [
89+
{ "$ref": "#" },
90+
{ "$ref": "#/definitions/schemaArray" }
91+
],
92+
"default": {}
93+
},
94+
"maxItems": { "$ref": "#/definitions/nonNegativeInteger" },
95+
"minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
96+
"uniqueItems": {
97+
"type": "boolean",
98+
"default": false
99+
},
100+
"contains": { "$ref": "#" },
101+
"maxProperties": { "$ref": "#/definitions/nonNegativeInteger" },
102+
"minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
103+
"required": { "$ref": "#/definitions/stringArray" },
104+
"additionalProperties": { "$ref": "#" },
105+
"definitions": {
106+
"type": "object",
107+
"additionalProperties": { "$ref": "#" },
108+
"default": {}
109+
},
110+
"properties": {
111+
"type": "object",
112+
"additionalProperties": { "$ref": "#" },
113+
"default": {}
114+
},
115+
"patternProperties": {
116+
"type": "object",
117+
"additionalProperties": { "$ref": "#" },
118+
"default": {}
119+
},
120+
"dependencies": {
121+
"type": "object",
122+
"additionalProperties": {
123+
"anyOf": [
124+
{ "$ref": "#" },
125+
{ "$ref": "#/definitions/stringArray" }
126+
]
127+
}
128+
},
129+
"propertyNames": { "$ref": "#" },
130+
"const": {},
131+
"enum": {
132+
"type": "array",
133+
"minItems": 1,
134+
"uniqueItems": true
135+
},
136+
"type": {
137+
"anyOf": [
138+
{ "$ref": "#/definitions/simpleTypes" },
139+
{
140+
"type": "array",
141+
"items": { "$ref": "#/definitions/simpleTypes" },
142+
"minItems": 1,
143+
"uniqueItems": true
144+
}
145+
]
146+
},
147+
"format": { "type": "string" },
148+
"allOf": { "$ref": "#/definitions/schemaArray" },
149+
"anyOf": { "$ref": "#/definitions/schemaArray" },
150+
"oneOf": { "$ref": "#/definitions/schemaArray" },
151+
"not": { "$ref": "#" }
152+
},
153+
"default": {}
154+
}

spec/json-schema-draft7.json

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "http://json-schema.org/draft-07/schema#",
4+
"title": "Core schema meta-schema",
5+
"definitions": {
6+
"schemaArray": {
7+
"type": "array",
8+
"minItems": 1,
9+
"items": { "$ref": "#" }
10+
},
11+
"nonNegativeInteger": {
12+
"type": "integer",
13+
"minimum": 0
14+
},
15+
"nonNegativeIntegerDefault0": {
16+
"allOf": [
17+
{ "$ref": "#/definitions/nonNegativeInteger" },
18+
{ "default": 0 }
19+
]
20+
},
21+
"simpleTypes": {
22+
"enum": [
23+
"array",
24+
"boolean",
25+
"integer",
26+
"null",
27+
"number",
28+
"object",
29+
"string"
30+
]
31+
},
32+
"stringArray": {
33+
"type": "array",
34+
"items": { "type": "string" },
35+
"uniqueItems": true,
36+
"default": []
37+
}
38+
},
39+
"type": ["object", "boolean"],
40+
"properties": {
41+
"$id": {
42+
"type": "string",
43+
"format": "uri-reference"
44+
},
45+
"$schema": {
46+
"type": "string",
47+
"format": "uri"
48+
},
49+
"$ref": {
50+
"type": "string",
51+
"format": "uri-reference"
52+
},
53+
"$comment": {
54+
"type": "string"
55+
},
56+
"title": {
57+
"type": "string"
58+
},
59+
"description": {
60+
"type": "string"
61+
},
62+
"default": true,
63+
"readOnly": {
64+
"type": "boolean",
65+
"default": false
66+
},
67+
"examples": {
68+
"type": "array",
69+
"items": true
70+
},
71+
"multipleOf": {
72+
"type": "number",
73+
"exclusiveMinimum": 0
74+
},
75+
"maximum": {
76+
"type": "number"
77+
},
78+
"exclusiveMaximum": {
79+
"type": "number"
80+
},
81+
"minimum": {
82+
"type": "number"
83+
},
84+
"exclusiveMinimum": {
85+
"type": "number"
86+
},
87+
"maxLength": { "$ref": "#/definitions/nonNegativeInteger" },
88+
"minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
89+
"pattern": {
90+
"type": "string",
91+
"format": "regex"
92+
},
93+
"additionalItems": { "$ref": "#" },
94+
"items": {
95+
"anyOf": [
96+
{ "$ref": "#" },
97+
{ "$ref": "#/definitions/schemaArray" }
98+
],
99+
"default": true
100+
},
101+
"maxItems": { "$ref": "#/definitions/nonNegativeInteger" },
102+
"minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
103+
"uniqueItems": {
104+
"type": "boolean",
105+
"default": false
106+
},
107+
"contains": { "$ref": "#" },
108+
"maxProperties": { "$ref": "#/definitions/nonNegativeInteger" },
109+
"minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
110+
"required": { "$ref": "#/definitions/stringArray" },
111+
"additionalProperties": { "$ref": "#" },
112+
"definitions": {
113+
"type": "object",
114+
"additionalProperties": { "$ref": "#" },
115+
"default": {}
116+
},
117+
"properties": {
118+
"type": "object",
119+
"additionalProperties": { "$ref": "#" },
120+
"default": {}
121+
},
122+
"patternProperties": {
123+
"type": "object",
124+
"additionalProperties": { "$ref": "#" },
125+
"propertyNames": { "format": "regex" },
126+
"default": {}
127+
},
128+
"dependencies": {
129+
"type": "object",
130+
"additionalProperties": {
131+
"anyOf": [
132+
{ "$ref": "#" },
133+
{ "$ref": "#/definitions/stringArray" }
134+
]
135+
}
136+
},
137+
"propertyNames": { "$ref": "#" },
138+
"const": true,
139+
"enum": {
140+
"type": "array",
141+
"items": true,
142+
"minItems": 1,
143+
"uniqueItems": true
144+
},
145+
"type": {
146+
"anyOf": [
147+
{ "$ref": "#/definitions/simpleTypes" },
148+
{
149+
"type": "array",
150+
"items": { "$ref": "#/definitions/simpleTypes" },
151+
"minItems": 1,
152+
"uniqueItems": true
153+
}
154+
]
155+
},
156+
"format": { "type": "string" },
157+
"contentMediaType": { "type": "string" },
158+
"contentEncoding": { "type": "string" },
159+
"if": {"$ref": "#"},
160+
"then": {"$ref": "#"},
161+
"else": {"$ref": "#"},
162+
"allOf": { "$ref": "#/definitions/schemaArray" },
163+
"anyOf": { "$ref": "#/definitions/schemaArray" },
164+
"oneOf": { "$ref": "#/definitions/schemaArray" },
165+
"not": { "$ref": "#" }
166+
},
167+
"default": true
168+
}

0 commit comments

Comments
 (0)