Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v6.0.0 #390

Merged
merged 36 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
72b94c1
Add URI translation, package:// URI scheme & bundle spec schemas (#362)
erayd Mar 7, 2017
3dba977
centralize errors (#364)
shmax Mar 7, 2017
a4da6ba
Add use line for InvalidArgumentException (#370)
erayd Mar 17, 2017
14a04dc
Add use line for InvalidArgumentException & adjust scope (#372)
erayd Mar 17, 2017
5de03d4
Revert "An email is a string, not much else." (#373)
erayd Mar 17, 2017
b1ee0e8
[BUGFIX] Add provided schema under a dummy / internal URI (fixes #376…
erayd Mar 17, 2017
d3f7740
add quiet option (#382)
shmax Mar 17, 2017
b8d2611
Add option to disable validation of "format" constraint (#383)
erayd Mar 17, 2017
705dcbd
Add more unit tests (#366)
erayd Mar 17, 2017
cf8e886
Reset errors prior to validation (#386)
erayd Mar 17, 2017
5bc0d34
Allow the schema to be an associative array (#389)
erayd Mar 21, 2017
1567cb6
Enable FILTER_FLAG_EMAIL_UNICODE for email format if present (#398)
erayd Mar 21, 2017
c44609a
Don't throw exceptions until after checking anyOf / oneOf (#394)
erayd Mar 21, 2017
af14372
add enum wrapper (#375)
shmax Mar 21, 2017
46d10ac
Fix infinite recursion on some schemas when setting defaults (#359) (…
erayd Mar 21, 2017
e8259a0
Add option to also validate the schema (#357)
erayd Mar 21, 2017
f3bfb47
Improvements to type coercion (#384)
erayd Mar 21, 2017
2553ebd
Remove stale files from #357 (obviated by #362) (#400)
erayd Mar 21, 2017
7ec8f1b
Split $objectDefinition into $schema and $properties (#411)
erayd Apr 11, 2017
992e1c3
Issue-414: Allow The Option of T or space for Date time. (#415)
HunterEl Apr 12, 2017
af48180
Testcase for minProperties with properties defined (#416)
flolivaud Apr 12, 2017
d7fcdcb
Tweak phpdocumentor dependency to avoid install conflicts (#421)
erayd May 4, 2017
549b4af
[BUGFIX] Cast empty schema arrays to object (#409)
erayd May 5, 2017
331d41b
fix bug when applying defaults for array items when the schema is for…
mathroc May 5, 2017
9fe7822
[BUGFIX] Fix autoload to work properly with composer dependencies (#401)
erayd May 16, 2017
2d404b8
[BUGFIX] Split "uri" format into "uri" & "uri-reference", fix meta-sc…
erayd May 16, 2017
6407baa
Remove PHPDocumentor & change travis platform for hhvm tests (#429)
erayd Jun 5, 2017
5dcbe1d
Bugfix for #424 - add '?' for query string (#425)
erayd Jun 5, 2017
024f3d8
Added property name in draft-3 required error (#432)
sunspikes Jun 6, 2017
3948abb
[BUGFIX] Ignore $ref siblings & abort on infinite-loop references (#437)
erayd Jun 23, 2017
90d340b
Update php-csfixer rules to address problem in 2.7 & new multiline ru…
erayd Oct 2, 2017
0a6210e
Add proper recursive handling for $ref resolution base (#448)
erayd Oct 2, 2017
c59e7b2
[BUGFIX] Don't resolve a schema id against itself (#452)
erayd Oct 4, 2017
bbdb443
Don't add a `file://` prefix to URI that already have a scheme (#455)
erayd Oct 9, 2017
1da0f92
Add a path to the default internal URI (#459)
erayd Oct 17, 2017
dcff3ad
override new phpcs rule (#465)
mathroc Nov 10, 2017
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 .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $config
'@PSR2' => true,
'@Symfony' => true,
// additionally
'align_multiline_comment' => array('comment_type' => 'phpdocs_like'),
'array_syntax' => array('syntax' => 'long'),
'binary_operator_spaces' => false,
'concat_space' => array('spacing' => 'one'),
Expand All @@ -24,6 +25,8 @@ $config
'pre_increment' => false,
'trailing_comma_in_multiline_array' => false,
'simplified_null_return' => false,
'yoda_style' => null,
'increment_style' => false,
))
->setFinder($finder)
;
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ matrix:
fast_finish: true
include:
- php: 5.3
dist: precise
- php: 5.4
- php: 5.5
- php: 5.6
Expand All @@ -18,6 +19,7 @@ matrix:
- php: 7.1
- php: 'nightly'
- php: hhvm
dist: trusty
allow_failures:
- php: 'nightly'

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

Please note that using `Constraint::CHECK_MODE_COERCE_TYPES` or `Constraint::CHECK_MODE_APPLY_DEFAULTS`
will modify your original data.
Please note that using `CHECK_MODE_COERCE_TYPES` or `CHECK_MODE_APPLY_DEFAULTS` will modify your
original data.

`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.

## Running the tests

Expand Down
149 changes: 75 additions & 74 deletions bin/validate-json
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,58 @@
* @author Christian Weiske <[email protected]>
*/

/**
* Dead simple autoloader
*
* @param string $className Name of class to load
*
* @return void
*/
function __autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
if (stream_resolve_include_path($fileName)) {
require_once $fileName;
// support running this tool from git checkout
$projectDirectory = dirname(__DIR__);
if (is_dir($projectDirectory . DIRECTORY_SEPARATOR . 'vendor')) {
set_include_path($projectDirectory . PATH_SEPARATOR . get_include_path());
}

// autoload composer classes
$composerAutoload = stream_resolve_include_path('vendor/autoload.php');
if (!$composerAutoload) {
echo("Cannot load json-schema library\n");
exit(1);
}
require($composerAutoload);

$arOptions = array();
$arArgs = array();
array_shift($argv);//script itself
foreach ($argv as $arg) {
if ($arg{0} == '-') {
$arOptions[$arg] = true;
} else {
$arArgs[] = $arg;
}
}

if (count($arArgs) == 0
|| isset($arOptions['--help']) || isset($arOptions['-h'])
) {
echo <<<HLP
Validate schema
Usage: validate-json data.json
or: validate-json data.json schema.json

Options:
--dump-schema Output full schema and exit
--dump-schema-url Output URL of schema
--verbose Show additional output
--quiet Suppress all output
-h --help Show this help

HLP;
exit(1);
}

if (count($arArgs) == 1) {
$pathData = $arArgs[0];
$pathSchema = null;
} else {
$pathData = $arArgs[0];
$pathSchema = getUrlFromPath($arArgs[1]);
}

/**
* Show the json parse error that happened last
*
Expand All @@ -44,7 +73,7 @@ function showJsonError()
}
}

echo 'JSON parse error: ' . $json_errors[json_last_error()] . "\n";
output('JSON parse error: ' . $json_errors[json_last_error()] . "\n");
}

function getUrlFromPath($path)
Expand Down Expand Up @@ -84,48 +113,18 @@ function parseHeaderValue($headerValue)
return $arData;
}


// support running this tool from git checkout
if (is_dir(__DIR__ . '/../src/JsonSchema')) {
set_include_path(__DIR__ . '/../src' . PATH_SEPARATOR . get_include_path());
}

$arOptions = array();
$arArgs = array();
array_shift($argv);//script itself
foreach ($argv as $arg) {
if ($arg{0} == '-') {
$arOptions[$arg] = true;
} else {
$arArgs[] = $arg;
/**
* Send a string to the output stream, but only if --quiet is not enabled
*
* @param $str A string output
*/
function output($str) {
global $arOptions;
if (!isset($arOptions['--quiet'])) {
echo $str;
}
}

if (count($arArgs) == 0
|| isset($arOptions['--help']) || isset($arOptions['-h'])
) {
echo <<<HLP
Validate schema
Usage: validate-json data.json
or: validate-json data.json schema.json

Options:
--dump-schema Output full schema and exit
--dump-schema-url Output URL of schema
-h --help Show this help

HLP;
exit(1);
}

if (count($arArgs) == 1) {
$pathData = $arArgs[0];
$pathSchema = null;
} else {
$pathData = $arArgs[0];
$pathSchema = getUrlFromPath($arArgs[1]);
}

$urlData = getUrlFromPath($pathData);

$context = stream_context_create(
Expand All @@ -141,14 +140,14 @@ $context = stream_context_create(
);
$dataString = file_get_contents($pathData, false, $context);
if ($dataString == '') {
echo "Data file is not readable or empty.\n";
output("Data file is not readable or empty.\n");
exit(3);
}

$data = json_decode($dataString);
unset($dataString);
if ($data === null) {
echo "Error loading JSON data file\n";
output("Error loading JSON data file\n");
showJsonError();
exit(5);
}
Expand Down Expand Up @@ -182,9 +181,9 @@ if ($pathSchema === null) {

//autodetect schema
if ($pathSchema === null) {
echo "JSON data must be an object and have a \$schema property.\n";
echo "You can pass the schema file on the command line as well.\n";
echo "Schema autodetection failed.\n";
output("JSON data must be an object and have a \$schema property.\n");
output("You can pass the schema file on the command line as well.\n");
output("Schema autodetection failed.\n");
exit(6);
}
}
Expand All @@ -202,9 +201,9 @@ try {
exit();
}
} catch (Exception $e) {
echo "Error loading JSON schema file\n";
echo $urlSchema . "\n";
echo $e->getMessage() . "\n";
output("Error loading JSON schema file\n");
output($urlSchema . "\n");
output($e->getMessage() . "\n");
exit(2);
}
$refResolver = new JsonSchema\SchemaStorage($retriever, $resolver);
Expand All @@ -221,17 +220,19 @@ try {
$validator->check($data, $schema);

if ($validator->isValid()) {
echo "OK. The supplied JSON validates against the schema.\n";
if(isset($arOptions['--verbose'])) {
output("OK. The supplied JSON validates against the schema.\n");
}
} else {
echo "JSON does not validate. Violations:\n";
output("JSON does not validate. Violations:\n");
foreach ($validator->getErrors() as $error) {
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
output(sprintf("[%s] %s\n", $error['property'], $error['message']));
}
exit(23);
}
} catch (Exception $e) {
echo "JSON does not validate. Error:\n";
echo $e->getMessage() . "\n";
echo "Error code: " . $e->getCode() . "\n";
output("JSON does not validate. Error:\n");
output($e->getMessage() . "\n");
output("Error code: " . $e->getCode() . "\n");
exit(24);
}
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
}
}],
"require": {
"php": ">=5.3.3"
"php": ">=5.3.3",
"marc-mabe/php-enum":"2.3.1"
},
"require-dev": {
"json-schema/JSON-Schema-Test-Suite": "1.2.0",
"phpunit/phpunit": "^4.8.22",
"friendsofphp/php-cs-fixer": "^2.1",
"phpdocumentor/phpdocumentor": "~2"
"phpunit/phpunit": "^4.8.22"
},
"autoload": {
"psr-4": { "JsonSchema\\": "src/JsonSchema/" }
Expand Down
Loading