-
Notifications
You must be signed in to change notification settings - Fork 8k
RFC - json_validate() #9399
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
Merged
Merged
RFC - json_validate() #9399
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0405a86
json_validate()
8f86a67
retrigger pipelines
344687f
005 performance test
cfbee85
Add new line test 001
juan-morales 85dcc6a
Add new line at the end of tests
6688e20
Add PHP_JSON_VALIDATE_ONLY constant instead of validate_only
47756e9
Add test comparing results with json_decode() on different types of i…
ae041be
group together function hooks for the json parser used by validation …
8e97349
Remove usage of flag PHP_JSON_THROW_ON_ERROR
52ab14e
Add test cases for invalid types on test 006
1b1b4f3
Stop propagation of parser option PHP_JSON_VALIDATE_ONLY to the scann…
baf8ec6
get_validate_methods() usage instead of options constant PHP_JSON_VAL…
d038d1f
fix qualifier error
722303b
remove get_validate_methods() from PHP API
f02c89c
remove test 005 and rename 006
70467bc
fix typeO in comment
8b528bd
use RETURN_BOOL macro
juan-morales 7331b47
Fix return type to bool
juan-morales 2e92c03
Optimizations usage of ZVAL_NULL instead of empty array. Return valid…
juan-morales d71b16f
dependencies
juan-morales 9223115
adjust indentation in test from 2 to 4 spaces
juan-morales a08d066
Remove test of passing NULL as parameter
juan-morales File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
json_validate() - General usage | ||
--FILE-- | ||
<?php | ||
|
||
var_dump( | ||
json_validate(""), | ||
json_validate("."), | ||
json_validate("<?>"), | ||
json_validate(";"), | ||
json_validate("руссиш"), | ||
json_validate("blah"), | ||
json_validate('{ "": "": "" } }'), | ||
json_validate('{ "": { "": "" }'), | ||
json_validate('{ "test": {} "foo": "bar" }, "test2": {"foo" : "bar" }, "test2": {"foo" : "bar" } }'), | ||
|
||
json_validate('{ "test": { "foo": "bar" } }'), | ||
juan-morales marked this conversation as resolved.
Show resolved
Hide resolved
|
||
json_validate('{ "test": { "foo": "" } }'), | ||
json_validate('{ "": { "foo": "" } }'), | ||
json_validate('{ "": { "": "" } }'), | ||
json_validate('{ "test": {"foo": "bar"}, "test2": {"foo" : "bar" }, "test2": {"foo" : "bar" } }'), | ||
json_validate('{ "test": {"foo": "bar"}, "test2": {"foo" : "bar" }, "test3": {"foo" : "bar" } }'), | ||
); | ||
|
||
?> | ||
--EXPECT-- | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--TEST-- | ||
json_validate() - Error handling | ||
--FILE-- | ||
<?php | ||
|
||
require_once("json_validate_requires.inc"); | ||
|
||
json_validate_trycatchdump(""); | ||
json_validate_trycatchdump("-"); | ||
json_validate_trycatchdump("", -1); | ||
json_validate_trycatchdump('{"key1":"value1", "key2":"value2"}', 1); | ||
json_validate_trycatchdump('{"key1":"value1", "key2":"value2"}', 2); | ||
json_validate_trycatchdump("-", 0); | ||
json_validate_trycatchdump("-", 512, JSON_BIGINT_AS_STRING); | ||
json_validate_trycatchdump("-", 512, JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE); | ||
json_validate_trycatchdump("-", 512, JSON_INVALID_UTF8_IGNORE); | ||
json_validate_trycatchdump("{}", 512, JSON_INVALID_UTF8_IGNORE); | ||
|
||
?> | ||
--EXPECTF-- | ||
bool(false) | ||
int(4) | ||
string(12) "Syntax error" | ||
bool(false) | ||
int(4) | ||
string(12) "Syntax error" | ||
bool(false) | ||
int(4) | ||
string(12) "Syntax error" | ||
bool(false) | ||
int(1) | ||
string(28) "Maximum stack depth exceeded" | ||
bool(true) | ||
int(0) | ||
string(8) "No error" | ||
Error: 0 json_validate(): Argument #2 ($depth) must be greater than 0 | ||
int(0) | ||
string(8) "No error" | ||
Error: 0 json_validate(): Argument #3 ($flags) must be a valid flag (allowed flags: JSON_INVALID_UTF8_IGNORE) | ||
int(0) | ||
string(8) "No error" | ||
Error: 0 json_validate(): Argument #3 ($flags) must be a valid flag (allowed flags: JSON_INVALID_UTF8_IGNORE) | ||
int(0) | ||
string(8) "No error" | ||
bool(false) | ||
int(4) | ||
string(12) "Syntax error" | ||
bool(true) | ||
int(0) | ||
string(8) "No error" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
json_validate() - Error handling for max depth | ||
--SKIPIF-- | ||
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?> | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
var_dump(json_validate("-", PHP_INT_MAX)); | ||
} catch (ValueError $error) { | ||
echo $error->getMessage() . PHP_EOL; | ||
var_dump(json_last_error(), json_last_error_msg()); | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
json_validate(): Argument #2 ($depth) must be less than %d | ||
int(0) | ||
string(8) "No error" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--TEST-- | ||
json_validate() - Invalid UTF-8's | ||
--FILE-- | ||
<?php | ||
|
||
require_once("json_validate_requires.inc"); | ||
|
||
echo "Testing Invalid UTF-8" . PHP_EOL; | ||
|
||
|
||
json_validate_trycatchdump("\"a\xb0b\""); | ||
json_validate_trycatchdump("\"a\xd0\xf2b\""); | ||
json_validate_trycatchdump("\"\x61\xf0\x80\x80\x41\""); | ||
json_validate_trycatchdump("[\"\xc1\xc1\",\"a\"]"); | ||
|
||
json_validate_trycatchdump("\"a\xb0b\"", 512, JSON_INVALID_UTF8_IGNORE); | ||
json_validate_trycatchdump("\"a\xd0\xf2b\"", 512, JSON_INVALID_UTF8_IGNORE); | ||
json_validate_trycatchdump("\"\x61\xf0\x80\x80\x41\"", 512, JSON_INVALID_UTF8_IGNORE); | ||
json_validate_trycatchdump("[\"\xc1\xc1\",\"a\"]", 512, JSON_INVALID_UTF8_IGNORE); | ||
|
||
?> | ||
--EXPECT-- | ||
Testing Invalid UTF-8 | ||
bool(false) | ||
int(5) | ||
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded" | ||
bool(false) | ||
int(5) | ||
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded" | ||
bool(false) | ||
int(5) | ||
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded" | ||
bool(false) | ||
int(5) | ||
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded" | ||
bool(true) | ||
int(0) | ||
string(8) "No error" | ||
bool(true) | ||
int(0) | ||
string(8) "No error" | ||
bool(true) | ||
int(0) | ||
string(8) "No error" | ||
bool(true) | ||
int(0) | ||
string(8) "No error" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this works, but checking for any bits found in bitwise not in allowed flags is shorter and conventional - it also works and is used for combinations of multiple flags