Skip to content

Commit 237b077

Browse files
authored
Merge pull request #390 from justinrainbow/6.0.0-dev
2 parents 9225c96 + dcff3ad commit 237b077

File tree

84 files changed

+2712
-1004
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2712
-1004
lines changed

.php_cs.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ $config
1111
'@PSR2' => true,
1212
'@Symfony' => true,
1313
// additionally
14+
'align_multiline_comment' => array('comment_type' => 'phpdocs_like'),
1415
'array_syntax' => array('syntax' => 'long'),
1516
'binary_operator_spaces' => false,
1617
'concat_space' => array('spacing' => 'one'),
@@ -24,6 +25,8 @@ $config
2425
'pre_increment' => false,
2526
'trailing_comma_in_multiline_array' => false,
2627
'simplified_null_return' => false,
28+
'yoda_style' => null,
29+
'increment_style' => false,
2730
))
2831
->setFinder($finder)
2932
;

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ matrix:
1010
fast_finish: true
1111
include:
1212
- php: 5.3
13+
dist: precise
1314
- php: 5.4
1415
- php: 5.5
1516
- php: 5.6
@@ -18,6 +19,7 @@ matrix:
1819
- php: 7.1
1920
- php: 'nightly'
2021
- php: hhvm
22+
dist: trusty
2123
allow_failures:
2224
- php: 'nightly'
2325

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,19 @@ third argument to `Validator::validate()`, or can be provided as the third argum
186186
| `Constraint::CHECK_MODE_NORMAL` | Validate in 'normal' mode - this is the default |
187187
| `Constraint::CHECK_MODE_TYPE_CAST` | Enable fuzzy type checking for associative arrays and objects |
188188
| `Constraint::CHECK_MODE_COERCE_TYPES` | Convert data types to match the schema where possible |
189+
| `Constraint::CHECK_MODE_EARLY_COERCE` | Apply type coercion as soon as possible |
189190
| `Constraint::CHECK_MODE_APPLY_DEFAULTS` | Apply default values from the schema if not set |
191+
| `Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS` | When applying defaults, only set values that are required |
190192
| `Constraint::CHECK_MODE_EXCEPTIONS` | Throw an exception immediately if validation fails |
193+
| `Constraint::CHECK_MODE_DISABLE_FORMAT` | Do not validate "format" constraints |
194+
| `Constraint::CHECK_MODE_VALIDATE_SCHEMA` | Validate the schema as well as the provided document |
191195

192-
Please note that using `Constraint::CHECK_MODE_COERCE_TYPES` or `Constraint::CHECK_MODE_APPLY_DEFAULTS`
193-
will modify your original data.
196+
Please note that using `CHECK_MODE_COERCE_TYPES` or `CHECK_MODE_APPLY_DEFAULTS` will modify your
197+
original data.
198+
199+
`CHECK_MODE_EARLY_COERCE` has no effect unless used in combination with `CHECK_MODE_COERCE_TYPES`. If
200+
enabled, the validator will use (and coerce) the first compatible type it encounters, even if the
201+
schema defines another type that matches directly and does not require coercion.
194202

195203
## Running the tests
196204

bin/validate-json

Lines changed: 75 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,58 @@
66
* @author Christian Weiske <[email protected]>
77
*/
88

9-
/**
10-
* Dead simple autoloader
11-
*
12-
* @param string $className Name of class to load
13-
*
14-
* @return void
15-
*/
16-
function __autoload($className)
17-
{
18-
$className = ltrim($className, '\\');
19-
$fileName = '';
20-
$namespace = '';
21-
if ($lastNsPos = strrpos($className, '\\')) {
22-
$namespace = substr($className, 0, $lastNsPos);
23-
$className = substr($className, $lastNsPos + 1);
24-
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
25-
}
26-
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
27-
if (stream_resolve_include_path($fileName)) {
28-
require_once $fileName;
9+
// support running this tool from git checkout
10+
$projectDirectory = dirname(__DIR__);
11+
if (is_dir($projectDirectory . DIRECTORY_SEPARATOR . 'vendor')) {
12+
set_include_path($projectDirectory . PATH_SEPARATOR . get_include_path());
13+
}
14+
15+
// autoload composer classes
16+
$composerAutoload = stream_resolve_include_path('vendor/autoload.php');
17+
if (!$composerAutoload) {
18+
echo("Cannot load json-schema library\n");
19+
exit(1);
20+
}
21+
require($composerAutoload);
22+
23+
$arOptions = array();
24+
$arArgs = array();
25+
array_shift($argv);//script itself
26+
foreach ($argv as $arg) {
27+
if ($arg{0} == '-') {
28+
$arOptions[$arg] = true;
29+
} else {
30+
$arArgs[] = $arg;
2931
}
3032
}
3133

34+
if (count($arArgs) == 0
35+
|| isset($arOptions['--help']) || isset($arOptions['-h'])
36+
) {
37+
echo <<<HLP
38+
Validate schema
39+
Usage: validate-json data.json
40+
or: validate-json data.json schema.json
41+
42+
Options:
43+
--dump-schema Output full schema and exit
44+
--dump-schema-url Output URL of schema
45+
--verbose Show additional output
46+
--quiet Suppress all output
47+
-h --help Show this help
48+
49+
HLP;
50+
exit(1);
51+
}
52+
53+
if (count($arArgs) == 1) {
54+
$pathData = $arArgs[0];
55+
$pathSchema = null;
56+
} else {
57+
$pathData = $arArgs[0];
58+
$pathSchema = getUrlFromPath($arArgs[1]);
59+
}
60+
3261
/**
3362
* Show the json parse error that happened last
3463
*
@@ -44,7 +73,7 @@ function showJsonError()
4473
}
4574
}
4675

47-
echo 'JSON parse error: ' . $json_errors[json_last_error()] . "\n";
76+
output('JSON parse error: ' . $json_errors[json_last_error()] . "\n");
4877
}
4978

5079
function getUrlFromPath($path)
@@ -84,48 +113,18 @@ function parseHeaderValue($headerValue)
84113
return $arData;
85114
}
86115

87-
88-
// support running this tool from git checkout
89-
if (is_dir(__DIR__ . '/../src/JsonSchema')) {
90-
set_include_path(__DIR__ . '/../src' . PATH_SEPARATOR . get_include_path());
91-
}
92-
93-
$arOptions = array();
94-
$arArgs = array();
95-
array_shift($argv);//script itself
96-
foreach ($argv as $arg) {
97-
if ($arg{0} == '-') {
98-
$arOptions[$arg] = true;
99-
} else {
100-
$arArgs[] = $arg;
116+
/**
117+
* Send a string to the output stream, but only if --quiet is not enabled
118+
*
119+
* @param $str A string output
120+
*/
121+
function output($str) {
122+
global $arOptions;
123+
if (!isset($arOptions['--quiet'])) {
124+
echo $str;
101125
}
102126
}
103127

104-
if (count($arArgs) == 0
105-
|| isset($arOptions['--help']) || isset($arOptions['-h'])
106-
) {
107-
echo <<<HLP
108-
Validate schema
109-
Usage: validate-json data.json
110-
or: validate-json data.json schema.json
111-
112-
Options:
113-
--dump-schema Output full schema and exit
114-
--dump-schema-url Output URL of schema
115-
-h --help Show this help
116-
117-
HLP;
118-
exit(1);
119-
}
120-
121-
if (count($arArgs) == 1) {
122-
$pathData = $arArgs[0];
123-
$pathSchema = null;
124-
} else {
125-
$pathData = $arArgs[0];
126-
$pathSchema = getUrlFromPath($arArgs[1]);
127-
}
128-
129128
$urlData = getUrlFromPath($pathData);
130129

131130
$context = stream_context_create(
@@ -141,14 +140,14 @@ $context = stream_context_create(
141140
);
142141
$dataString = file_get_contents($pathData, false, $context);
143142
if ($dataString == '') {
144-
echo "Data file is not readable or empty.\n";
143+
output("Data file is not readable or empty.\n");
145144
exit(3);
146145
}
147146

148147
$data = json_decode($dataString);
149148
unset($dataString);
150149
if ($data === null) {
151-
echo "Error loading JSON data file\n";
150+
output("Error loading JSON data file\n");
152151
showJsonError();
153152
exit(5);
154153
}
@@ -182,9 +181,9 @@ if ($pathSchema === null) {
182181

183182
//autodetect schema
184183
if ($pathSchema === null) {
185-
echo "JSON data must be an object and have a \$schema property.\n";
186-
echo "You can pass the schema file on the command line as well.\n";
187-
echo "Schema autodetection failed.\n";
184+
output("JSON data must be an object and have a \$schema property.\n");
185+
output("You can pass the schema file on the command line as well.\n");
186+
output("Schema autodetection failed.\n");
188187
exit(6);
189188
}
190189
}
@@ -202,9 +201,9 @@ try {
202201
exit();
203202
}
204203
} catch (Exception $e) {
205-
echo "Error loading JSON schema file\n";
206-
echo $urlSchema . "\n";
207-
echo $e->getMessage() . "\n";
204+
output("Error loading JSON schema file\n");
205+
output($urlSchema . "\n");
206+
output($e->getMessage() . "\n");
208207
exit(2);
209208
}
210209
$refResolver = new JsonSchema\SchemaStorage($retriever, $resolver);
@@ -221,17 +220,19 @@ try {
221220
$validator->check($data, $schema);
222221

223222
if ($validator->isValid()) {
224-
echo "OK. The supplied JSON validates against the schema.\n";
223+
if(isset($arOptions['--verbose'])) {
224+
output("OK. The supplied JSON validates against the schema.\n");
225+
}
225226
} else {
226-
echo "JSON does not validate. Violations:\n";
227+
output("JSON does not validate. Violations:\n");
227228
foreach ($validator->getErrors() as $error) {
228-
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
229+
output(sprintf("[%s] %s\n", $error['property'], $error['message']));
229230
}
230231
exit(23);
231232
}
232233
} catch (Exception $e) {
233-
echo "JSON does not validate. Error:\n";
234-
echo $e->getMessage() . "\n";
235-
echo "Error code: " . $e->getCode() . "\n";
234+
output("JSON does not validate. Error:\n");
235+
output($e->getMessage() . "\n");
236+
output("Error code: " . $e->getCode() . "\n");
236237
exit(24);
237238
}

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
}
3737
}],
3838
"require": {
39-
"php": ">=5.3.3"
39+
"php": ">=5.3.3",
40+
"marc-mabe/php-enum":"2.3.1"
4041
},
4142
"require-dev": {
4243
"json-schema/JSON-Schema-Test-Suite": "1.2.0",
43-
"phpunit/phpunit": "^4.8.22",
4444
"friendsofphp/php-cs-fixer": "^2.1",
45-
"phpdocumentor/phpdocumentor": "~2"
45+
"phpunit/phpunit": "^4.8.22"
4646
},
4747
"autoload": {
4848
"psr-4": { "JsonSchema\\": "src/JsonSchema/" }

0 commit comments

Comments
 (0)