From d87d4eef3538ba5fb9e31919f55ff33a3109dd69 Mon Sep 17 00:00:00 2001 From: Viktor Turskyi Date: Sun, 18 Oct 2026 20:38:01 +0300 Subject: [PATCH 1/2] Update the test suite and fix the rules it checks The test suite is copied from the LIVR repository. As before, the copy keeps negative/22-not_empty_list and negative/27-any_object adjusted, as PHP decodes {} and [] to the same array. - or: pass empty values to the rule sets, so "required" and "not_empty" in them work - or: do not unwrap a list in the first argument, as a set of rules can be a list: { or: [['email', 'to_lc'], 'positive_integer'] } - list_of_different_objects, variable_object: return FORMAT_ERROR for array selector values instead of throwing a TypeError - list_of_different_objects: return the errors when an item is not an object (it was dropped from the output), and FORMAT_ERROR when the value is not a list (it became an empty list) - equal_to_field: compare values as strings, as "==" finds "10" and "1e1" equal, and do not warn when the other field is missing --- lib/Validator/LIVR/Rules/Meta.php | 63 ++++++++++++------- lib/Validator/LIVR/Rules/Special.php | 5 +- phpunit.xml.dist | 1 + tests/05-rules_edge_cases.php | 30 +++++++++ .../aliases_negative/02-address/aliases.json | 4 +- .../aliases_negative/02-address/input.json | 4 +- .../aliases_positive/01-adult_age/input.json | 1 + .../aliases_positive/01-adult_age/output.json | 1 + .../aliases_positive/01-adult_age/rules.json | 2 + .../aliases_positive/02-address/aliases.json | 4 +- .../aliases_positive/02-address/input.json | 2 +- .../aliases_positive/02-address/output.json | 2 +- .../negative/01-required/errors.json | 5 +- .../negative/01-required/input.json | 1 + .../negative/01-required/rules.json | 6 +- .../test_suite/negative/03-one_of/errors.json | 2 + .../test_suite/negative/03-one_of/input.json | 2 + .../test_suite/negative/03-one_of/rules.json | 16 ++--- .../negative/12-positive_decimal/errors.json | 1 + .../negative/12-positive_decimal/input.json | 1 + .../negative/12-positive_decimal/rules.json | 1 + .../negative/14-min_number/errors.json | 1 + .../negative/14-min_number/input.json | 1 + .../negative/14-min_number/rules.json | 1 + .../negative/15-number_beetween/errors.json | 2 + .../negative/15-number_beetween/input.json | 2 + .../negative/15-number_beetween/rules.json | 2 + .../test_suite/negative/16-email/errors.json | 22 +++++++ tests/test_suite/negative/16-email/input.json | 23 +++++++ tests/test_suite/negative/16-email/rules.json | 23 +++++++ .../negative/17-equal_to_field/errors.json | 4 ++ .../negative/17-equal_to_field/input.json | 5 ++ .../negative/17-equal_to_field/rules.json | 4 ++ .../negative/18-nested_object/input.json | 2 +- .../21-list_of_different_objects/errors.json | 7 ++- .../21-list_of_different_objects/input.json | 24 ++++++- .../21-list_of_different_objects/rules.json | 37 ++++++++++- tests/test_suite/negative/23-url/errors.json | 2 + tests/test_suite/negative/23-url/input.json | 2 + tests/test_suite/negative/23-url/rules.json | 2 + .../negative/24-iso_date/errors.json | 9 ++- .../negative/24-iso_date/input.json | 9 ++- .../negative/24-iso_date/rules.json | 9 ++- tests/test_suite/negative/25-eq/errors.json | 10 +-- tests/test_suite/negative/25-eq/input.json | 4 +- tests/test_suite/negative/25-eq/rules.json | 18 +++--- .../negative/28-variable_object/errors.json | 10 ++- .../negative/28-variable_object/input.json | 10 ++- .../negative/28-variable_object/rules.json | 58 ++++++++++++++++- tests/test_suite/negative/29-or/errors.json | 3 + tests/test_suite/negative/29-or/input.json | 1 + tests/test_suite/negative/29-or/rules.json | 3 + .../positive/01-required/input.json | 6 +- .../positive/01-required/output.json | 5 +- .../positive/01-required/rules.json | 5 +- .../test_suite/positive/03-one_of/input.json | 11 +++- .../test_suite/positive/03-one_of/output.json | 11 +++- .../test_suite/positive/03-one_of/rules.json | 13 ++-- .../positive/05-max_length/input.json | 1 + .../positive/05-max_length/output.json | 1 + .../positive/05-max_length/rules.json | 1 + .../positive/07-length_between/input.json | 1 + .../positive/07-length_between/output.json | 1 + .../positive/07-length_between/rules.json | 1 + .../test_suite/positive/09-integer/input.json | 3 +- .../positive/09-integer/output.json | 1 - .../test_suite/positive/09-integer/rules.json | 1 - .../test_suite/positive/11-decimal/input.json | 4 +- .../positive/11-decimal/output.json | 4 +- .../test_suite/positive/11-decimal/rules.json | 6 +- .../positive/15-number_between/input.json | 1 + .../positive/15-number_between/output.json | 1 + .../positive/15-number_between/rules.json | 3 +- tests/test_suite/positive/16-email/input.json | 29 +++++++++ .../test_suite/positive/16-email/output.json | 28 +++++++++ tests/test_suite/positive/16-email/rules.json | 28 +++++++++ .../positive/17-equal_to_field/input.json | 4 +- .../positive/17-equal_to_field/output.json | 3 +- .../positive/17-equal_to_field/rules.json | 3 +- .../positive/18-nested_object/input.json | 10 ++- .../positive/18-nested_object/output.json | 5 ++ .../positive/18-nested_object/rules.json | 11 +++- .../positive/20-list_of_objects/input.json | 4 +- .../positive/20-list_of_objects/rules.json | 2 +- .../positive/24-iso_date/input.json | 2 + .../positive/24-iso_date/output.json | 2 + .../positive/24-iso_date/rules.json | 2 + tests/test_suite/positive/25-eq/input.json | 7 ++- tests/test_suite/positive/25-eq/output.json | 7 ++- tests/test_suite/positive/25-eq/rules.json | 9 +-- .../test_suite/positive/26-string/input.json | 4 +- .../test_suite/positive/26-string/output.json | 4 +- .../positive/27-any_object/input.json | 5 +- .../positive/27-any_object/output.json | 5 +- .../positive/27-any_object/rules.json | 3 +- tests/test_suite/positive/29-or/input.json | 2 + tests/test_suite/positive/29-or/output.json | 2 + tests/test_suite/positive/29-or/rules.json | 3 + tests/test_suite/positive/30-trim/input.json | 4 +- tests/test_suite/positive/30-trim/output.json | 4 +- tests/test_suite/positive/30-trim/rules.json | 4 +- tests/test_suite/positive/31-to_lc/input.json | 4 +- .../test_suite/positive/31-to_lc/output.json | 4 +- tests/test_suite/positive/31-to_lc/rules.json | 4 +- tests/test_suite/positive/32-to_uc/input.json | 4 +- .../test_suite/positive/32-to_uc/output.json | 4 +- tests/test_suite/positive/32-to_uc/rules.json | 4 +- .../test_suite/positive/35-default/input.json | 2 +- .../positive/35-default/output.json | 4 +- .../test_suite/positive/35-default/rules.json | 4 +- 110 files changed, 644 insertions(+), 131 deletions(-) create mode 100644 tests/05-rules_edge_cases.php diff --git a/lib/Validator/LIVR/Rules/Meta.php b/lib/Validator/LIVR/Rules/Meta.php index a597454..947202a 100644 --- a/lib/Validator/LIVR/Rules/Meta.php +++ b/lib/Validator/LIVR/Rules/Meta.php @@ -131,20 +131,27 @@ public static function listOfDifferentObjects($selectorField, $livrs, $ruleBuild } return function ($objects, $params, &$outputArr) use ($validators, $selectorField) { + if (!isset($objects) || $objects === '') { + return; + } + + if (!\Validator\LIVR\Util::isList($objects)) { + return 'FORMAT_ERROR'; + } + $results = array(); $errors = array(); $hasErrors = false; foreach ($objects as $object) { - if (!is_array($object) - || !isset($object[$selectorField]) - || !isset($validators[$object[$selectorField]]) - || !$validators[$object[$selectorField]]) { - $errors[] = 'FORMAT_ERROR'; + $validator = self::selectValidator($validators, $object, $selectorField); + + if (!$validator) { + $errors[] = 'FORMAT_ERROR'; + $hasErrors = true; continue; } - $validator = $validators[ $object[$selectorField] ]; $result = $validator->validate($object); if ($result) { @@ -180,13 +187,12 @@ public static function variableObject($selectorField, $livrs, $ruleBuilders) return ''; } - if (!is_array($object) - || !isset($object[$selectorField]) - || !isset($validators[$object[$selectorField]])) { + $validator = self::selectValidator($validators, $object, $selectorField); + + if (!$validator) { return 'FORMAT_ERROR'; } - $validator = $validators[ $object[$selectorField] ]; $result = $validator->validate($object); if ($result !== false && $result !== null) { @@ -200,15 +206,10 @@ public static function variableObject($selectorField, $livrs, $ruleBuilders) public static function __or() { - $first_arg = func_get_arg(0); - - if (is_array($first_arg) && !\Validator\LIVR\Util::isAssocArray($first_arg)) { - $livrs = func_get_arg(0); - $ruleBuilders = func_get_arg(1); - } else { - $livrs = func_get_args(); - $ruleBuilders = array_pop($livrs); - } + // Each argument is a set of rules. Unlike list_of, a list in the first argument + // is not unwrapped, as a set of rules can be a list: { or: [['email', 'to_lc'], 'positive_integer'] } + $livrs = func_get_args(); + $ruleBuilders = array_pop($livrs); $validators = array(); @@ -219,10 +220,7 @@ public static function __or() } return function ($value, $params, &$outputArr) use ($validators) { - if (!isset($value) || $value === '') { - return; - } - + // Empty values go to the rule sets too, so "required" and "not_empty" in them work $lastError = null; foreach ($validators as $validator) { $result = $validator->validate(array('field' => $value)); @@ -241,4 +239,23 @@ public static function __or() } }; } + + /** + * Returns the validator for the selector value of the object, + * or null if the object or its selector value is wrong + */ + private static function selectValidator($validators, $object, $selectorField) + { + if (!is_array($object) || !isset($object[$selectorField])) { + return null; + } + + // Only strings and integers can be array keys + $selectorValue = $object[$selectorField]; + if (!is_string($selectorValue) && !is_int($selectorValue)) { + return null; + } + + return isset($validators[$selectorValue]) ? $validators[$selectorValue] : null; + } } diff --git a/lib/Validator/LIVR/Rules/Special.php b/lib/Validator/LIVR/Rules/Special.php index f130415..b73e4d1 100644 --- a/lib/Validator/LIVR/Rules/Special.php +++ b/lib/Validator/LIVR/Rules/Special.php @@ -35,7 +35,10 @@ public static function equalToField($field) return 'FORMAT_ERROR'; } - if ($value != $params[$field]) { + // Compared as strings: "==" finds numeric strings like "10" and "1e1" equal + $fieldValue = isset($params[$field]) ? $params[$field] : null; + + if (!\Validator\LIVR\Util::isStringOrNumber($fieldValue) || (string) $value !== (string) $fieldValue) { return 'FIELDS_NOT_EQUAL'; } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 467f030..4b3ddea 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,6 +10,7 @@ tests/02-livr_test_suite_aliased_rules.php tests/03-auto_trim.php tests/04-custom_filters.php + tests/05-rules_edge_cases.php diff --git a/tests/05-rules_edge_cases.php b/tests/05-rules_edge_cases.php new file mode 100644 index 0000000..f536396 --- /dev/null +++ b/tests/05-rules_edge_cases.php @@ -0,0 +1,30 @@ + array('list_of_different_objects' => array('product_type', array( + 'material' => array('product_type' => 'required', 'material_id' => 'positive_integer'), + ))), + )); + + $this->assertEquals(array(), $validator->validate(array()), 'Should skip a missing value'); + $this->assertEquals( + array('products' => ''), + $validator->validate(array('products' => '')), + 'Should skip an empty value' + ); + + foreach (array('not an array', 10, array('product_type' => 'material')) as $products) { + $this->assertFalse($validator->validate(array('products' => $products)), 'Should return false'); + $this->assertEquals(array('products' => 'FORMAT_ERROR'), $validator->getErrors(), 'Should return FORMAT_ERROR'); + } + } +} diff --git a/tests/test_suite/aliases_negative/02-address/aliases.json b/tests/test_suite/aliases_negative/02-address/aliases.json index 6903fde..33d6609 100644 --- a/tests/test_suite/aliases_negative/02-address/aliases.json +++ b/tests/test_suite/aliases_negative/02-address/aliases.json @@ -4,7 +4,7 @@ "rules": { "nested_object":{ "street": "required", "zip": ["required", "positive_integer"], - "city": {"one_of": ["Kiev", "Kharkiv"] } + "city": {"one_of": ["Kyiv", "Kharkiv"] } }} }, @@ -13,7 +13,7 @@ "rules": { "nested_object":{ "street": "required", "zip": ["required", "positive_integer"], - "city": {"one_of": ["Kiev", "Kharkiv"] } + "city": {"one_of": ["Kyiv", "Kharkiv"] } }}, "error": "WRONG_ADDRESS" } diff --git a/tests/test_suite/aliases_negative/02-address/input.json b/tests/test_suite/aliases_negative/02-address/input.json index 29a1615..55a58dd 100644 --- a/tests/test_suite/aliases_negative/02-address/input.json +++ b/tests/test_suite/aliases_negative/02-address/input.json @@ -2,12 +2,12 @@ "address": { "street": "", "zip": -1232131, - "city": "Moscow" + "city": "Lviv" }, "address_custom_error": { "street": "My street", "zip": "12312", - "city": "Moscow" + "city": "Lviv" }, "extra_field": 12 } \ No newline at end of file diff --git a/tests/test_suite/aliases_positive/01-adult_age/input.json b/tests/test_suite/aliases_positive/01-adult_age/input.json index 78f9603..0ef049d 100644 --- a/tests/test_suite/aliases_positive/01-adult_age/input.json +++ b/tests/test_suite/aliases_positive/01-adult_age/input.json @@ -3,6 +3,7 @@ "age2": 33, "age3": 35, "age4": 36, + "empty_age": "", "age1_custom_error": 37, "age2_custom_error": 38, "age3_custom_error": 100, diff --git a/tests/test_suite/aliases_positive/01-adult_age/output.json b/tests/test_suite/aliases_positive/01-adult_age/output.json index 7555a83..3be49e1 100644 --- a/tests/test_suite/aliases_positive/01-adult_age/output.json +++ b/tests/test_suite/aliases_positive/01-adult_age/output.json @@ -3,6 +3,7 @@ "age2": 33, "age3": 35, "age4": 36, + "empty_age": "", "age1_custom_error": 37, "age2_custom_error": 38, "age3_custom_error": 100, diff --git a/tests/test_suite/aliases_positive/01-adult_age/rules.json b/tests/test_suite/aliases_positive/01-adult_age/rules.json index f760384..a7c8fa8 100644 --- a/tests/test_suite/aliases_positive/01-adult_age/rules.json +++ b/tests/test_suite/aliases_positive/01-adult_age/rules.json @@ -3,6 +3,8 @@ "age2": ["adult_age"], "age3": [ { "adult_age": [] } ], "age4": { "adult_age": [] }, + "empty_age": "adult_age", + "missed_age": "adult_age", "age1_custom_error": "adult_age_with_custom_error", "age2_custom_error": ["adult_age_with_custom_error"], "age3_custom_error": [ { "adult_age_with_custom_error": [] } ], diff --git a/tests/test_suite/aliases_positive/02-address/aliases.json b/tests/test_suite/aliases_positive/02-address/aliases.json index 6903fde..33d6609 100644 --- a/tests/test_suite/aliases_positive/02-address/aliases.json +++ b/tests/test_suite/aliases_positive/02-address/aliases.json @@ -4,7 +4,7 @@ "rules": { "nested_object":{ "street": "required", "zip": ["required", "positive_integer"], - "city": {"one_of": ["Kiev", "Kharkiv"] } + "city": {"one_of": ["Kyiv", "Kharkiv"] } }} }, @@ -13,7 +13,7 @@ "rules": { "nested_object":{ "street": "required", "zip": ["required", "positive_integer"], - "city": {"one_of": ["Kiev", "Kharkiv"] } + "city": {"one_of": ["Kyiv", "Kharkiv"] } }}, "error": "WRONG_ADDRESS" } diff --git a/tests/test_suite/aliases_positive/02-address/input.json b/tests/test_suite/aliases_positive/02-address/input.json index 7b019b7..09499ca 100644 --- a/tests/test_suite/aliases_positive/02-address/input.json +++ b/tests/test_suite/aliases_positive/02-address/input.json @@ -2,7 +2,7 @@ "address": { "street": "My street", "zip": 1232131, - "city": "Kiev" + "city": "Kyiv" }, "address_custom_error": { "street": "My street", diff --git a/tests/test_suite/aliases_positive/02-address/output.json b/tests/test_suite/aliases_positive/02-address/output.json index 8435705..2d9b107 100644 --- a/tests/test_suite/aliases_positive/02-address/output.json +++ b/tests/test_suite/aliases_positive/02-address/output.json @@ -2,7 +2,7 @@ "address": { "street": "My street", "zip": 1232131, - "city": "Kiev" + "city": "Kyiv" }, "address_custom_error": { "street": "My street", diff --git a/tests/test_suite/negative/01-required/errors.json b/tests/test_suite/negative/01-required/errors.json index ac736ea..6d8e9b2 100644 --- a/tests/test_suite/negative/01-required/errors.json +++ b/tests/test_suite/negative/01-required/errors.json @@ -1,5 +1,8 @@ { "first_name": "REQUIRED", "last_name": "REQUIRED", - "middle_name": "REQUIRED" + "middle_name": "REQUIRED", + "constructor": "REQUIRED", + "toString": "REQUIRED", + "hasOwnProperty": "REQUIRED" } \ No newline at end of file diff --git a/tests/test_suite/negative/01-required/input.json b/tests/test_suite/negative/01-required/input.json index 7923c3c..1218083 100644 --- a/tests/test_suite/negative/01-required/input.json +++ b/tests/test_suite/negative/01-required/input.json @@ -2,6 +2,7 @@ "first_name": "", "last_name": null, "age": "25", + "__proto__": { "middle_name": "Some" }, "value_is_hash": {"test": 1}, "value_is_empty_hash": {}, diff --git a/tests/test_suite/negative/01-required/rules.json b/tests/test_suite/negative/01-required/rules.json index 9327efc..6123e87 100644 --- a/tests/test_suite/negative/01-required/rules.json +++ b/tests/test_suite/negative/01-required/rules.json @@ -6,5 +6,9 @@ "value_is_hash": "required", "value_is_empty_hash": "required", "value_is_array": "required", - "value_is_empty_array": "required" + "value_is_empty_array": "required", + + "constructor": "required", + "toString": "required", + "hasOwnProperty": "required" } \ No newline at end of file diff --git a/tests/test_suite/negative/03-one_of/errors.json b/tests/test_suite/negative/03-one_of/errors.json index 0b4e560..8c2c5e4 100644 --- a/tests/test_suite/negative/03-one_of/errors.json +++ b/tests/test_suite/negative/03-one_of/errors.json @@ -2,7 +2,9 @@ "city1": "NOT_ALLOWED_VALUE", "city2": "NOT_ALLOWED_VALUE", "city3": "NOT_ALLOWED_VALUE", + "city4": "NOT_ALLOWED_VALUE", "number1": "NOT_ALLOWED_VALUE", + "number2": "NOT_ALLOWED_VALUE", "value_is_hash": "FORMAT_ERROR", "value_is_empty_hash": "FORMAT_ERROR", diff --git a/tests/test_suite/negative/03-one_of/input.json b/tests/test_suite/negative/03-one_of/input.json index 2f0a049..2685ed6 100644 --- a/tests/test_suite/negative/03-one_of/input.json +++ b/tests/test_suite/negative/03-one_of/input.json @@ -2,7 +2,9 @@ "city1": "New York", "city2": "New York", "city3": "New York", + "city4": "New York", "number1": "1.0", + "number2": 40, "extra_city": "New York", "value_is_hash": {"test": 1}, diff --git a/tests/test_suite/negative/03-one_of/rules.json b/tests/test_suite/negative/03-one_of/rules.json index e1043e3..a6773ec 100644 --- a/tests/test_suite/negative/03-one_of/rules.json +++ b/tests/test_suite/negative/03-one_of/rules.json @@ -1,11 +1,13 @@ { - "city1": { "one_of": [["Moscow", "Kiev" ]] }, - "city2": [{ "one_of": [["Moscow", "Kiev" ]] }], - "city3": { "one_of": ["Moscow", "Kiev"] }, + "city1": { "one_of": [["Lviv", "Kyiv" ]] }, + "city2": [{ "one_of": [["Lviv", "Kyiv" ]] }], + "city3": { "one_of": ["Lviv", "Kyiv"] }, + "city4": { "one_of": ["Kyiv", "Lviv", "Odesa", "Kharkiv"] }, "number1": { "one_of": [1, "1.00", "1"] }, + "number2": { "one_of": [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]] }, - "value_is_hash": { "one_of": [["Moscow", "Kiev" ]] }, - "value_is_empty_hash": { "one_of": [["Moscow", "Kiev" ]] }, - "value_is_array": { "one_of": [["Moscow", "Kiev" ]] }, - "value_is_empty_array": { "one_of": [["Moscow", "Kiev" ]] } + "value_is_hash": { "one_of": [["Lviv", "Kyiv" ]] }, + "value_is_empty_hash": { "one_of": [["Lviv", "Kyiv" ]] }, + "value_is_array": { "one_of": [["Lviv", "Kyiv" ]] }, + "value_is_empty_array": { "one_of": [["Lviv", "Kyiv" ]] } } diff --git a/tests/test_suite/negative/12-positive_decimal/errors.json b/tests/test_suite/negative/12-positive_decimal/errors.json index 0f72bd7..2f8aa93 100644 --- a/tests/test_suite/negative/12-positive_decimal/errors.json +++ b/tests/test_suite/negative/12-positive_decimal/errors.json @@ -2,6 +2,7 @@ "number1": "NOT_POSITIVE_DECIMAL", "number2": "NOT_POSITIVE_DECIMAL", "number3": "NOT_POSITIVE_DECIMAL", + "number4": "NOT_POSITIVE_DECIMAL", "value_is_string": "NOT_POSITIVE_DECIMAL", "value_is_hash": "FORMAT_ERROR", diff --git a/tests/test_suite/negative/12-positive_decimal/input.json b/tests/test_suite/negative/12-positive_decimal/input.json index 1e1eb4d..1592047 100644 --- a/tests/test_suite/negative/12-positive_decimal/input.json +++ b/tests/test_suite/negative/12-positive_decimal/input.json @@ -2,6 +2,7 @@ "number1": "10.10.10", "number2": "0", "number3": -1.12, + "number4": "0.0", "empty_field": "", "extra_field": "100", diff --git a/tests/test_suite/negative/12-positive_decimal/rules.json b/tests/test_suite/negative/12-positive_decimal/rules.json index 4586e35..6f63394 100644 --- a/tests/test_suite/negative/12-positive_decimal/rules.json +++ b/tests/test_suite/negative/12-positive_decimal/rules.json @@ -2,6 +2,7 @@ "number1": "positive_decimal", "number2": ["positive_decimal"], "number3": [ { "positive_decimal": [] } ], + "number4": ["positive_decimal"], "empty_field": "positive_decimal", "value_is_string": "positive_decimal", diff --git a/tests/test_suite/negative/14-min_number/errors.json b/tests/test_suite/negative/14-min_number/errors.json index 995bdd7..5bb6710 100644 --- a/tests/test_suite/negative/14-min_number/errors.json +++ b/tests/test_suite/negative/14-min_number/errors.json @@ -1,6 +1,7 @@ { "number1": "TOO_LOW", "number2": "TOO_LOW", + "number3": "TOO_LOW", "value_is_string": "NOT_NUMBER", "value_is_hash": "FORMAT_ERROR", diff --git a/tests/test_suite/negative/14-min_number/input.json b/tests/test_suite/negative/14-min_number/input.json index d98ed3a..18c26bb 100644 --- a/tests/test_suite/negative/14-min_number/input.json +++ b/tests/test_suite/negative/14-min_number/input.json @@ -1,6 +1,7 @@ { "number1": "5", "number2": "-10", + "number3": 1.2, "empty_field": "", "extra_field": "100", diff --git a/tests/test_suite/negative/14-min_number/rules.json b/tests/test_suite/negative/14-min_number/rules.json index 10e0b1f..6216cff 100644 --- a/tests/test_suite/negative/14-min_number/rules.json +++ b/tests/test_suite/negative/14-min_number/rules.json @@ -1,6 +1,7 @@ { "number1": { "min_number" : 10 }, "number2": [{ "min_number" : 20 }], + "number3": [{ "min_number" : 5 }], "empty_field": { "min_number" : 10 }, "value_is_string": { "min_number": 10 }, diff --git a/tests/test_suite/negative/15-number_beetween/errors.json b/tests/test_suite/negative/15-number_beetween/errors.json index 5eecc1c..cbad1f5 100644 --- a/tests/test_suite/negative/15-number_beetween/errors.json +++ b/tests/test_suite/negative/15-number_beetween/errors.json @@ -1,6 +1,8 @@ { "number1": "TOO_LOW", "number2": "TOO_HIGH", + "number3": "TOO_LOW", + "number4": "TOO_HIGH", "value_is_string": "NOT_NUMBER", "value_is_hash": "FORMAT_ERROR", diff --git a/tests/test_suite/negative/15-number_beetween/input.json b/tests/test_suite/negative/15-number_beetween/input.json index 5c995e2..5260811 100644 --- a/tests/test_suite/negative/15-number_beetween/input.json +++ b/tests/test_suite/negative/15-number_beetween/input.json @@ -1,6 +1,8 @@ { "number1": "5", "number2": "40", + "number3": -40, + "number4": 40, "empty_field": "", "extra_field": "100", diff --git a/tests/test_suite/negative/15-number_beetween/rules.json b/tests/test_suite/negative/15-number_beetween/rules.json index 1c58053..ef94f93 100644 --- a/tests/test_suite/negative/15-number_beetween/rules.json +++ b/tests/test_suite/negative/15-number_beetween/rules.json @@ -1,6 +1,8 @@ { "number1": { "number_between": [10,20] }, "number2": [ { "number_between": [10,20] } ], + "number3": [ { "number_between": [-10,10] } ], + "number4": [ { "number_between": [-10,10] } ], "empty_field": { "number_between": [10,20] }, "value_is_string": { "number_between": [10,20] }, diff --git a/tests/test_suite/negative/16-email/errors.json b/tests/test_suite/negative/16-email/errors.json index 7fe51c9..9db6812 100644 --- a/tests/test_suite/negative/16-email/errors.json +++ b/tests/test_suite/negative/16-email/errors.json @@ -4,6 +4,28 @@ "email3": "WRONG_EMAIL", "email4": "WRONG_EMAIL", "email5": "WRONG_EMAIL", + "email6": "WRONG_EMAIL", + "email7": "WRONG_EMAIL", + "email8": "WRONG_EMAIL", + "email9": "WRONG_EMAIL", + "email10": "WRONG_EMAIL", + "email11": "WRONG_EMAIL", + "email12": "WRONG_EMAIL", + "email13": "WRONG_EMAIL", + "email14": "WRONG_EMAIL", + "email15": "WRONG_EMAIL", + "email16": "WRONG_EMAIL", + "email17": "WRONG_EMAIL", + "email18": "WRONG_EMAIL", + "email19": "WRONG_EMAIL", + "email20": "WRONG_EMAIL", + "email21": "WRONG_EMAIL", + "email22": "WRONG_EMAIL", + "email23": "WRONG_EMAIL", + "email24": "WRONG_EMAIL", + "email25": "WRONG_EMAIL", + "email26": "WRONG_EMAIL", + "email27": "WRONG_EMAIL", "value_is_hash": "FORMAT_ERROR", "value_is_empty_hash": "FORMAT_ERROR", diff --git a/tests/test_suite/negative/16-email/input.json b/tests/test_suite/negative/16-email/input.json index b8c84f4..74adcf1 100644 --- a/tests/test_suite/negative/16-email/input.json +++ b/tests/test_suite/negative/16-email/input.json @@ -4,6 +4,29 @@ "email3": "test+test.t_e_s_t@ma_il.in.ua", "email4": "user name@gmail.com", "email5": "username@mail.c om", + "email6": "en ny@enny.devoffice.com", + "email7": "enny..tester@devoffice.com", + "email8": ".enny@enny.devoffice.com", + "email9": "enny.devoffice.com", + "email10": "test@test", + "email11": "tester@enny..devoffice.com", + "email12": "en(ny@enny.devoffice.com", + "email13": "en)ny@enny.devoffice.com", + "email14": "en[enny@devoffice.com", + "email15": "en]ny@devoffice.com", + "email16": "ho:rhe@tester.devoffice.com", + "email17": "enny;tester@devoffice.com", + "email18": "te,st@devoffice.com", + "email19": "testter@devoffice.com", + "email21": "tester@@devoffice.com", + "email22": "tester@tes ter.devoffice.com", + "email23": "@enny tm.devoffice.com", + "email24": "тестувальник@пошта.укр ", + "email25": "test@mail.com\n", + "email26": "te\nst@mail.com", + "email27": "test@ma\nil.com", + "empty_field": "", "extra_field": "aaaa", diff --git a/tests/test_suite/negative/16-email/rules.json b/tests/test_suite/negative/16-email/rules.json index 5b11ca4..28acd52 100644 --- a/tests/test_suite/negative/16-email/rules.json +++ b/tests/test_suite/negative/16-email/rules.json @@ -4,6 +4,29 @@ "email3": [ { "email": [] } ], "email4": "email", "email5": "email", + "email6": "email", + "email7": "email", + "email8": "email", + "email9": "email", + "email10": "email", + "email11": "email", + "email12": "email", + "email13": "email", + "email14": "email", + "email15": "email", + "email16": "email", + "email17": "email", + "email18": "email", + "email19": "email", + "email20": "email", + "email21": "email", + "email22": "email", + "email23": "email", + "email24": "email", + "email25": "email", + "email26": "email", + "email27": "email", + "empty_field": "email", "value_is_hash": "email", diff --git a/tests/test_suite/negative/17-equal_to_field/errors.json b/tests/test_suite/negative/17-equal_to_field/errors.json index da9d30a..1de7186 100644 --- a/tests/test_suite/negative/17-equal_to_field/errors.json +++ b/tests/test_suite/negative/17-equal_to_field/errors.json @@ -2,6 +2,10 @@ "field1": "FIELDS_NOT_EQUAL", "field2": "FIELDS_NOT_EQUAL", "field3": "FIELDS_NOT_EQUAL", + "same_number_exponent": "FIELDS_NOT_EQUAL", + "same_number_leading_zero": "FIELDS_NOT_EQUAL", + "same_number_space": "FIELDS_NOT_EQUAL", + "missing_other_field": "FIELDS_NOT_EQUAL", "value_is_hash": "FORMAT_ERROR", "value_is_empty_hash": "FORMAT_ERROR", diff --git a/tests/test_suite/negative/17-equal_to_field/input.json b/tests/test_suite/negative/17-equal_to_field/input.json index 26d319e..0343cc9 100644 --- a/tests/test_suite/negative/17-equal_to_field/input.json +++ b/tests/test_suite/negative/17-equal_to_field/input.json @@ -4,6 +4,11 @@ "field3": "some value", "empty_field": "", "extra_field": "some value", + "number": "10", + "same_number_exponent": "1e1", + "same_number_leading_zero": "010", + "same_number_space": " 10", + "missing_other_field": "some value", "value_is_hash": {"test": 1}, "value_is_empty_hash": {}, diff --git a/tests/test_suite/negative/17-equal_to_field/rules.json b/tests/test_suite/negative/17-equal_to_field/rules.json index d62223c..9f40b52 100644 --- a/tests/test_suite/negative/17-equal_to_field/rules.json +++ b/tests/test_suite/negative/17-equal_to_field/rules.json @@ -3,6 +3,10 @@ "field2": { "equal_to_field": ["field1"] }, "field3": { "equal_to_field": ["field1"] }, "empty_field": { "equal_to_field": "field2" }, + "same_number_exponent": { "equal_to_field": "number" }, + "same_number_leading_zero": { "equal_to_field": "number" }, + "same_number_space": { "equal_to_field": "number" }, + "missing_other_field": { "equal_to_field": "missing_field" }, "value_is_hash": { "equal_to_field": "field2" }, "value_is_empty_hash": { "equal_to_field": "field2" }, diff --git a/tests/test_suite/negative/18-nested_object/input.json b/tests/test_suite/negative/18-nested_object/input.json index d6c07b4..2b155a4 100644 --- a/tests/test_suite/negative/18-nested_object/input.json +++ b/tests/test_suite/negative/18-nested_object/input.json @@ -1,6 +1,6 @@ { "address": { - "country": "Russia", + "country": "Poland", "zip": "AAA", "street": "", "building": "-10" diff --git a/tests/test_suite/negative/21-list_of_different_objects/errors.json b/tests/test_suite/negative/21-list_of_different_objects/errors.json index 50ca1d8..57f6228 100644 --- a/tests/test_suite/negative/21-list_of_different_objects/errors.json +++ b/tests/test_suite/negative/21-list_of_different_objects/errors.json @@ -13,5 +13,10 @@ }, "FORMAT_ERROR", "FORMAT_ERROR" - ] + ], + "only_wrong_items": ["FORMAT_ERROR", "FORMAT_ERROR", "FORMAT_ERROR", "FORMAT_ERROR", "FORMAT_ERROR"], + "valid_and_wrong_items": [null, "FORMAT_ERROR"], + "null_item": ["FORMAT_ERROR"], + "selector_is_not_string": ["FORMAT_ERROR", "FORMAT_ERROR"], + "selector_is_builtin_property": ["FORMAT_ERROR", "FORMAT_ERROR", "FORMAT_ERROR", "FORMAT_ERROR"] } \ No newline at end of file diff --git a/tests/test_suite/negative/21-list_of_different_objects/input.json b/tests/test_suite/negative/21-list_of_different_objects/input.json index 1cd2137..81e4472 100644 --- a/tests/test_suite/negative/21-list_of_different_objects/input.json +++ b/tests/test_suite/negative/21-list_of_different_objects/input.json @@ -22,5 +22,27 @@ }, "Some Wrong Data" ], - "users": "not an array" + "users": "not an array", + "only_wrong_items": [ + "Some Wrong Data", + 10, + {"product_type": "wrongtype"}, + {"name": "No product type"}, + ["material"] + ], + "valid_and_wrong_items": [ + {"product_type": "service", "name": "Service"}, + "Some Wrong Data" + ], + "null_item": [null], + "selector_is_not_string": [ + {"product_type": ["material"], "material_id": 1}, + {"product_type": {"material": 1}, "material_id": 1} + ], + "selector_is_builtin_property": [ + {"product_type": "constructor"}, + {"product_type": "toString"}, + {"product_type": "hasOwnProperty"}, + {"product_type": "__proto__"} + ] } \ No newline at end of file diff --git a/tests/test_suite/negative/21-list_of_different_objects/rules.json b/tests/test_suite/negative/21-list_of_different_objects/rules.json index 64ea92a..2219c49 100644 --- a/tests/test_suite/negative/21-list_of_different_objects/rules.json +++ b/tests/test_suite/negative/21-list_of_different_objects/rules.json @@ -13,5 +13,40 @@ "name": ["required", {"max_length": 10} ] } } - ]}] + ]}], + + "only_wrong_items": { "list_of_different_objects": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "valid_and_wrong_items": { "list_of_different_objects": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "null_item": { "list_of_different_objects": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "selector_is_not_string": { "list_of_different_objects": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "selector_is_builtin_property": { "list_of_different_objects": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]} } \ No newline at end of file diff --git a/tests/test_suite/negative/23-url/errors.json b/tests/test_suite/negative/23-url/errors.json index 69efdb1..827048e 100644 --- a/tests/test_suite/negative/23-url/errors.json +++ b/tests/test_suite/negative/23-url/errors.json @@ -2,6 +2,8 @@ "url1": "WRONG_URL", "url2": "WRONG_URL", "url3": "WRONG_URL", + "url4": "WRONG_URL", + "url5": "WRONG_URL", "value_is_hash": "FORMAT_ERROR", "value_is_empty_hash": "FORMAT_ERROR", diff --git a/tests/test_suite/negative/23-url/input.json b/tests/test_suite/negative/23-url/input.json index 8ad6371..13b4501 100644 --- a/tests/test_suite/negative/23-url/input.json +++ b/tests/test_suite/negative/23-url/input.json @@ -2,6 +2,8 @@ "url1": "www.google.com", "url2": "ftp://facebook.com", "url3": "http://g_oogle.com", + "url4": "https://google.com\n", + "url5": "http://webbylab.com/?param_1=123\n", "empty_field": "", "extra_field": "aaaa", diff --git a/tests/test_suite/negative/23-url/rules.json b/tests/test_suite/negative/23-url/rules.json index b9779ba..eed2210 100644 --- a/tests/test_suite/negative/23-url/rules.json +++ b/tests/test_suite/negative/23-url/rules.json @@ -2,6 +2,8 @@ "url1": "url", "url2": { "url": [] }, "url3": [ { "url": [] } ], + "url4": "url", + "url5": "url", "empty_field": "url", "value_is_hash": "url", diff --git a/tests/test_suite/negative/24-iso_date/errors.json b/tests/test_suite/negative/24-iso_date/errors.json index 751e3fc..e7fb2cd 100644 --- a/tests/test_suite/negative/24-iso_date/errors.json +++ b/tests/test_suite/negative/24-iso_date/errors.json @@ -2,9 +2,16 @@ "iso_date1": "WRONG_DATE", "iso_date2": "WRONG_DATE", "iso_date3": "WRONG_DATE", + "iso_date4": "WRONG_DATE", + "iso_date5": "WRONG_DATE", + "iso_date6": "WRONG_DATE", + "iso_date7": "WRONG_DATE", + "iso_date8": "WRONG_DATE", + "iso_date9": "WRONG_DATE", + "like_iso_date": "WRONG_DATE", "value_is_hash": "FORMAT_ERROR", "value_is_empty_hash": "FORMAT_ERROR", "value_is_array": "FORMAT_ERROR", "value_is_empty_array": "FORMAT_ERROR" -} \ No newline at end of file +} diff --git a/tests/test_suite/negative/24-iso_date/input.json b/tests/test_suite/negative/24-iso_date/input.json index 004e5e2..ac0d129 100644 --- a/tests/test_suite/negative/24-iso_date/input.json +++ b/tests/test_suite/negative/24-iso_date/input.json @@ -2,6 +2,13 @@ "iso_date1": "2014-13-10", "iso_date2": "2011-02-29", "iso_date3": "2014-10-10T22:22", + "iso_date4": "1900-02-29", + "iso_date5": "2024-00-10", + "iso_date6": "2024-01-00", + "iso_date7": "2024-04-31", + "iso_date8": "2024-1-01", + "iso_date9": "2024-01-01\n", + "like_iso_date":"aaaa-bb-cc", "empty_field": "", "extra_field": "aaaa", @@ -9,4 +16,4 @@ "value_is_empty_hash": {}, "value_is_array": ["test", 1], "value_is_empty_array": [] -} \ No newline at end of file +} diff --git a/tests/test_suite/negative/24-iso_date/rules.json b/tests/test_suite/negative/24-iso_date/rules.json index 23bde5a..db4dc43 100644 --- a/tests/test_suite/negative/24-iso_date/rules.json +++ b/tests/test_suite/negative/24-iso_date/rules.json @@ -2,10 +2,17 @@ "iso_date1": "iso_date", "iso_date2": { "iso_date": [] }, "iso_date3": [ { "iso_date": [] } ], + "iso_date4": "iso_date", + "iso_date5": "iso_date", + "iso_date6": "iso_date", + "iso_date7": "iso_date", + "iso_date8": "iso_date", + "iso_date9": "iso_date", + "like_iso_date": [ { "iso_date": [] } ], "empty_field": "iso_date", "value_is_hash": "iso_date", "value_is_empty_hash": "iso_date", "value_is_array": "iso_date", "value_is_empty_array": "iso_date" -} \ No newline at end of file +} diff --git a/tests/test_suite/negative/25-eq/errors.json b/tests/test_suite/negative/25-eq/errors.json index 0b4e560..f994587 100644 --- a/tests/test_suite/negative/25-eq/errors.json +++ b/tests/test_suite/negative/25-eq/errors.json @@ -3,9 +3,11 @@ "city2": "NOT_ALLOWED_VALUE", "city3": "NOT_ALLOWED_VALUE", "number1": "NOT_ALLOWED_VALUE", - - "value_is_hash": "FORMAT_ERROR", - "value_is_empty_hash": "FORMAT_ERROR", - "value_is_array": "FORMAT_ERROR", + "number2": "NOT_ALLOWED_VALUE", + "number3": "NOT_ALLOWED_VALUE", + "empty_string": "NOT_ALLOWED_VALUE", + "value_is_hash": "FORMAT_ERROR", + "value_is_empty_hash": "FORMAT_ERROR", + "value_is_array": "FORMAT_ERROR", "value_is_empty_array": "FORMAT_ERROR" } diff --git a/tests/test_suite/negative/25-eq/input.json b/tests/test_suite/negative/25-eq/input.json index 2f0a049..e8f4068 100644 --- a/tests/test_suite/negative/25-eq/input.json +++ b/tests/test_suite/negative/25-eq/input.json @@ -3,8 +3,10 @@ "city2": "New York", "city3": "New York", "number1": "1.0", + "number2": "asdasd", + "number3": " _", + "empty_string": " ", "extra_city": "New York", - "value_is_hash": {"test": 1}, "value_is_empty_hash": {}, "value_is_array": ["test", 1], diff --git a/tests/test_suite/negative/25-eq/rules.json b/tests/test_suite/negative/25-eq/rules.json index 46e892c..acd8d01 100644 --- a/tests/test_suite/negative/25-eq/rules.json +++ b/tests/test_suite/negative/25-eq/rules.json @@ -1,11 +1,13 @@ { - "city1": { "eq": "Moscow" }, - "city2": [{ "eq": "Moscow" }], - "city3": { "eq": "Kiev" }, + "city1": { "eq": "Lviv" }, + "city2": [{ "eq": "Lviv" }], + "city3": { "eq": "Kyiv" }, "number1": { "eq": 1 }, - - "value_is_hash": { "eq": "Moscow" }, - "value_is_empty_hash": { "eq": "Moscow" }, - "value_is_array": { "eq": "Moscow" }, - "value_is_empty_array": { "eq": "Moscow" } + "number2": { "eq": 1 }, + "number3": { "eq": 1.2 }, + "empty_string": {"eq": 1 }, + "value_is_hash": { "eq": "Lviv" }, + "value_is_empty_hash": { "eq": "Lviv" }, + "value_is_array": { "eq": "Lviv" }, + "value_is_empty_array": { "eq": "Lviv" } } diff --git a/tests/test_suite/negative/28-variable_object/errors.json b/tests/test_suite/negative/28-variable_object/errors.json index bedaa97..596a4e8 100644 --- a/tests/test_suite/negative/28-variable_object/errors.json +++ b/tests/test_suite/negative/28-variable_object/errors.json @@ -11,5 +11,13 @@ "name": "TOO_LONG" }, "product4": "FORMAT_ERROR", - "product5": "FORMAT_ERROR" + "product5": "FORMAT_ERROR", + "product_without_selector": "FORMAT_ERROR", + "product_is_array": "FORMAT_ERROR", + "selector_is_array": "FORMAT_ERROR", + "selector_is_object": "FORMAT_ERROR", + "selector_is_constructor": "FORMAT_ERROR", + "selector_is_to_string": "FORMAT_ERROR", + "selector_is_has_own_property": "FORMAT_ERROR", + "selector_is_proto": "FORMAT_ERROR" } diff --git a/tests/test_suite/negative/28-variable_object/input.json b/tests/test_suite/negative/28-variable_object/input.json index 3aee953..2408874 100644 --- a/tests/test_suite/negative/28-variable_object/input.json +++ b/tests/test_suite/negative/28-variable_object/input.json @@ -20,5 +20,13 @@ "name": "Wrongtype" }, "product5": "Some Wrong Data", - "users": "not an array" + "users": "not an array", + "product_without_selector": {"name": "No product type"}, + "product_is_array": [{"product_type": "service", "name": "Service"}], + "selector_is_array": {"product_type": ["material"], "material_id": 1}, + "selector_is_object": {"product_type": {"material": 1}, "material_id": 1}, + "selector_is_constructor": {"product_type": "constructor"}, + "selector_is_to_string": {"product_type": "toString"}, + "selector_is_has_own_property": {"product_type": "hasOwnProperty"}, + "selector_is_proto": {"product_type": "__proto__"} } diff --git a/tests/test_suite/negative/28-variable_object/rules.json b/tests/test_suite/negative/28-variable_object/rules.json index e045bd2..bb4537f 100644 --- a/tests/test_suite/negative/28-variable_object/rules.json +++ b/tests/test_suite/negative/28-variable_object/rules.json @@ -73,5 +73,61 @@ "name": ["required", {"max_length": 10} ] } } - ]}] + ]}], + + "product_without_selector": { "variable_object": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "product_is_array": { "variable_object": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "selector_is_array": { "variable_object": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "selector_is_object": { "variable_object": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "selector_is_constructor": { "variable_object": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "selector_is_to_string": { "variable_object": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "selector_is_has_own_property": { "variable_object": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]}, + + "selector_is_proto": { "variable_object": [ + "product_type", { + "material": { "product_type": "required", "material_id": "positive_integer" }, + "service": { "product_type": "required", "name": "required" } + } + ]} } diff --git a/tests/test_suite/negative/29-or/errors.json b/tests/test_suite/negative/29-or/errors.json index 42a9fdb..dc7f9e0 100644 --- a/tests/test_suite/negative/29-or/errors.json +++ b/tests/test_suite/negative/29-or/errors.json @@ -2,6 +2,9 @@ "id1-1": "NOT_POSITIVE_INTEGER", "id2-1": "NOT_POSITIVE_INTEGER", "id3-1": "WRONG_EMAIL", + "id4-1": "CANNOT_BE_EMPTY", + "id4-2": "REQUIRED", + "products": [ { "name": "REQUIRED", diff --git a/tests/test_suite/negative/29-or/input.json b/tests/test_suite/negative/29-or/input.json index f47d7f5..3c518f7 100644 --- a/tests/test_suite/negative/29-or/input.json +++ b/tests/test_suite/negative/29-or/input.json @@ -2,6 +2,7 @@ "id1-1": "Usermail.com", "id2-1": "Usermail.com", "id3-1": "Usermail.com", + "id4-1": "", "products": [ { diff --git a/tests/test_suite/negative/29-or/rules.json b/tests/test_suite/negative/29-or/rules.json index d414fc1..12bd637 100644 --- a/tests/test_suite/negative/29-or/rules.json +++ b/tests/test_suite/negative/29-or/rules.json @@ -2,6 +2,9 @@ "id1-1": { "or": ["email", "positive_integer" ] }, "id2-1": [{ "or": ["email", "positive_integer" ] }, "to_lc"], "id3-1": { "or": [{"min_length": 15}, ["email", "to_lc"] ] }, + "id4-1": { "or": [["required", "positive_integer"], ["not_empty", "email"] ] }, + "id4-2": { "or": [["required", "positive_integer"], ["required", "email"] ] }, + "products": {"list_of": { "or": [ {"nested_object": { diff --git a/tests/test_suite/positive/01-required/input.json b/tests/test_suite/positive/01-required/input.json index e9ea6f1..ba3b3c6 100644 --- a/tests/test_suite/positive/01-required/input.json +++ b/tests/test_suite/positive/01-required/input.json @@ -3,5 +3,9 @@ "last_name": "Pupkin", "middle_name": "Some", "age": "25", - "salary": 0 + "__proto__": { "polluted": "yes" }, + "salary": 0, + "constructor": "Constructor value", + "toString": "ToString value", + "hasOwnProperty": "HasOwnProperty value" } \ No newline at end of file diff --git a/tests/test_suite/positive/01-required/output.json b/tests/test_suite/positive/01-required/output.json index 961b85f..0e0b6d0 100644 --- a/tests/test_suite/positive/01-required/output.json +++ b/tests/test_suite/positive/01-required/output.json @@ -2,5 +2,8 @@ "first_name": "Vasya", "last_name": "Pupkin", "middle_name": "Some", - "salary": 0 + "salary": 0, + "constructor": "Constructor value", + "toString": "ToString value", + "hasOwnProperty": "HasOwnProperty value" } \ No newline at end of file diff --git a/tests/test_suite/positive/01-required/rules.json b/tests/test_suite/positive/01-required/rules.json index 89b2555..d14610b 100644 --- a/tests/test_suite/positive/01-required/rules.json +++ b/tests/test_suite/positive/01-required/rules.json @@ -2,5 +2,8 @@ "first_name": "required", "last_name": ["required"], "middle_name": [ { "required": [] } ], - "salary": { "required": [] } + "salary": { "required": [] }, + "constructor": "required", + "toString": "required", + "hasOwnProperty": "required" } \ No newline at end of file diff --git a/tests/test_suite/positive/03-one_of/input.json b/tests/test_suite/positive/03-one_of/input.json index d987d0f..94dc94b 100644 --- a/tests/test_suite/positive/03-one_of/input.json +++ b/tests/test_suite/positive/03-one_of/input.json @@ -1,9 +1,14 @@ { - "city1": "Moscow", - "city2": "Kiev", - "city3": "Kiev", + "city1": "Lviv", + "city2": "Kyiv", + "city3": "Kyiv", + "city4": "Kharkiv", "number1": 2, "number2": 2, + "number3": 1.2, + "number4": 1, + "number5": "1", + "number6": "39", "boolean": true, "empty_city": "", "extra_city": "New York" diff --git a/tests/test_suite/positive/03-one_of/output.json b/tests/test_suite/positive/03-one_of/output.json index 8b5baa3..5f431c6 100644 --- a/tests/test_suite/positive/03-one_of/output.json +++ b/tests/test_suite/positive/03-one_of/output.json @@ -1,9 +1,14 @@ { - "city1": "Moscow", - "city2": "Kiev", - "city3": "Kiev", + "city1": "Lviv", + "city2": "Kyiv", + "city3": "Kyiv", + "city4": "Kharkiv", "number1": 2, "number2": "2", + "number3": 1.2, + "number4": "1", + "number5": 1, + "number6": 39, "boolean": true, "empty_city": "" } diff --git a/tests/test_suite/positive/03-one_of/rules.json b/tests/test_suite/positive/03-one_of/rules.json index b9fe743..213c9ca 100644 --- a/tests/test_suite/positive/03-one_of/rules.json +++ b/tests/test_suite/positive/03-one_of/rules.json @@ -1,9 +1,14 @@ { - "city1": { "one_of": [["Moscow", "Kiev" ]] }, - "city2": [{ "one_of": [["Moscow", "Kiev" ]] }], - "city3": { "one_of": ["Moscow", "Kiev"] }, + "city1": { "one_of": [["Lviv", "Kyiv" ]] }, + "city2": [{ "one_of": [["Lviv", "Kyiv" ]] }], + "city3": { "one_of": ["Lviv", "Kyiv"] }, + "city4": { "one_of": ["Kyiv", "Lviv", "Odesa", "Kharkiv"] }, "number1": { "one_of": [1, 2] }, "number2": { "one_of": ["1", "2"] }, + "number3": { "one_of": 1.2 }, + "number4": { "one_of": ["1", 1, "b"] }, + "number5": { "one_of": [1, "1"] }, + "number6": { "one_of": [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]] }, "boolean": { "one_of": [true, false] }, - "empty_city": { "one_of": [["Moscow", "Kiev" ]] } + "empty_city": { "one_of": [["Lviv", "Kyiv" ]] } } diff --git a/tests/test_suite/positive/05-max_length/input.json b/tests/test_suite/positive/05-max_length/input.json index 79518a0..9eb11b2 100644 --- a/tests/test_suite/positive/05-max_length/input.json +++ b/tests/test_suite/positive/05-max_length/input.json @@ -3,6 +3,7 @@ "last_name": "Pupkin", "middle_name": "Some name", "number1": 1111, + "decimal": 1.2, "empty_name": "", "extra_name": "extra_name" } diff --git a/tests/test_suite/positive/05-max_length/output.json b/tests/test_suite/positive/05-max_length/output.json index d805f90..46ca157 100644 --- a/tests/test_suite/positive/05-max_length/output.json +++ b/tests/test_suite/positive/05-max_length/output.json @@ -3,5 +3,6 @@ "last_name": "Pupkin", "middle_name": "Some name", "number1": "1111", + "decimal": "1.2", "empty_name": "" } diff --git a/tests/test_suite/positive/05-max_length/rules.json b/tests/test_suite/positive/05-max_length/rules.json index 387bb03..21af174 100644 --- a/tests/test_suite/positive/05-max_length/rules.json +++ b/tests/test_suite/positive/05-max_length/rules.json @@ -3,6 +3,7 @@ "last_name": { "max_length": [10] }, "middle_name": [{ "max_length": [10] }], "number1": { "max_length": 5 }, + "decimal": { "max_length": 3 }, "empty_name": { "max_length": 3 }, "missed_name": { "max_length": 3 } } diff --git a/tests/test_suite/positive/07-length_between/input.json b/tests/test_suite/positive/07-length_between/input.json index 9a11d4e..44c076f 100644 --- a/tests/test_suite/positive/07-length_between/input.json +++ b/tests/test_suite/positive/07-length_between/input.json @@ -2,6 +2,7 @@ "first_name": "Vasya", "last_name": "Pupkin", "middle_name": "Some name", + "nick_name": "joney_k", "number1": 1111, "empty_name": "", "extra_name": "extra_name" diff --git a/tests/test_suite/positive/07-length_between/output.json b/tests/test_suite/positive/07-length_between/output.json index 454c7a1..a28d23f 100644 --- a/tests/test_suite/positive/07-length_between/output.json +++ b/tests/test_suite/positive/07-length_between/output.json @@ -2,6 +2,7 @@ "first_name": "Vasya", "last_name": "Pupkin", "middle_name": "Some name", + "nick_name": "joney_k", "number1": "1111", "empty_name": "" } diff --git a/tests/test_suite/positive/07-length_between/rules.json b/tests/test_suite/positive/07-length_between/rules.json index 001b5a6..0311139 100644 --- a/tests/test_suite/positive/07-length_between/rules.json +++ b/tests/test_suite/positive/07-length_between/rules.json @@ -2,6 +2,7 @@ "first_name": { "length_between": [4, 6] }, "last_name": { "length_between": [6, 6] }, "middle_name": [{ "length_between": [0, 100] }], + "nick_name": [{ "length_between": [0, 100] }], "number1": { "length_between": [2, 4] }, "empty_name": { "length_between": [2, 3] }, "missed_name": { "length_between": [2, 3] } diff --git a/tests/test_suite/positive/09-integer/input.json b/tests/test_suite/positive/09-integer/input.json index bccb23e..3f82e09 100644 --- a/tests/test_suite/positive/09-integer/input.json +++ b/tests/test_suite/positive/09-integer/input.json @@ -2,7 +2,6 @@ "number1": "10", "number2": 0, "number3": -1, - "number4": "0", "empty_field": "", "extra_field": "100" -} +} \ No newline at end of file diff --git a/tests/test_suite/positive/09-integer/output.json b/tests/test_suite/positive/09-integer/output.json index 58e21b8..d78ad31 100644 --- a/tests/test_suite/positive/09-integer/output.json +++ b/tests/test_suite/positive/09-integer/output.json @@ -2,6 +2,5 @@ "number1": 10, "number2": 0, "number3": -1, - "number4": 0, "empty_field": "" } diff --git a/tests/test_suite/positive/09-integer/rules.json b/tests/test_suite/positive/09-integer/rules.json index 3998d8e..ff52490 100644 --- a/tests/test_suite/positive/09-integer/rules.json +++ b/tests/test_suite/positive/09-integer/rules.json @@ -2,7 +2,6 @@ "number1": "integer", "number2": ["integer"], "number3": [ { "integer": [] } ], - "number4": "integer", "empty_field": "integer", "missed_field": "integer" } diff --git a/tests/test_suite/positive/11-decimal/input.json b/tests/test_suite/positive/11-decimal/input.json index e149cb4..5d61130 100644 --- a/tests/test_suite/positive/11-decimal/input.json +++ b/tests/test_suite/positive/11-decimal/input.json @@ -2,9 +2,7 @@ "number1": "10.12", "number2": 0.55, "number3": -1.10, - "number4": "0.00", - "number5": 0.00, - "number6": 0, + "number4": -3.00, "empty_field": "", "extra_field": "100" } diff --git a/tests/test_suite/positive/11-decimal/output.json b/tests/test_suite/positive/11-decimal/output.json index 3108cdf..d79af1c 100644 --- a/tests/test_suite/positive/11-decimal/output.json +++ b/tests/test_suite/positive/11-decimal/output.json @@ -2,8 +2,6 @@ "number1": 10.12, "number2": 0.55, "number3": -1.10, - "number4": 0.00, - "number5": 0.00, - "number6": 0.00, + "number4": -3.00, "empty_field": "" } diff --git a/tests/test_suite/positive/11-decimal/rules.json b/tests/test_suite/positive/11-decimal/rules.json index 18dbd51..1382d25 100644 --- a/tests/test_suite/positive/11-decimal/rules.json +++ b/tests/test_suite/positive/11-decimal/rules.json @@ -2,8 +2,6 @@ "number1": "decimal", "number2": ["decimal"], "number3": [ { "decimal": [] } ], - "number4": "decimal", - "number5": "decimal", - "number6": "decimal", + "number4": "decimal", "empty_field": "decimal" -} +} \ No newline at end of file diff --git a/tests/test_suite/positive/15-number_between/input.json b/tests/test_suite/positive/15-number_between/input.json index 3fd0e3f..7442f11 100644 --- a/tests/test_suite/positive/15-number_between/input.json +++ b/tests/test_suite/positive/15-number_between/input.json @@ -2,6 +2,7 @@ "number1": "10", "number2": "25.55", "number3": "0", + "number4": 1, "empty_field": "", "extra_field": "100" } diff --git a/tests/test_suite/positive/15-number_between/output.json b/tests/test_suite/positive/15-number_between/output.json index a6b3921..960c723 100644 --- a/tests/test_suite/positive/15-number_between/output.json +++ b/tests/test_suite/positive/15-number_between/output.json @@ -2,5 +2,6 @@ "number1": 10, "number2": 25.55, "number3": 0, + "number4": 1, "empty_field": "" } diff --git a/tests/test_suite/positive/15-number_between/rules.json b/tests/test_suite/positive/15-number_between/rules.json index f38410e..c0cfc72 100644 --- a/tests/test_suite/positive/15-number_between/rules.json +++ b/tests/test_suite/positive/15-number_between/rules.json @@ -2,5 +2,6 @@ "number1": { "number_between": [10, 20] }, "number2": { "number_between": [20, 30] }, "number3": [ { "number_between": [-1, 1] } ], + "number4": [ { "number_between": [-10, 10] } ], "empty_field": { "number_between": [10, 20] } -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/16-email/input.json b/tests/test_suite/positive/16-email/input.json index 8ce8f3d..4a43d2f 100644 --- a/tests/test_suite/positive/16-email/input.json +++ b/tests/test_suite/positive/16-email/input.json @@ -2,6 +2,35 @@ "email1": "test@mail.com", "email2": "test.test@mail.com.ua", "email3": "test+test.t_e_s_t@mail.in.ua", + "email4": "user@hotline.finance", + "email5": "enny@enny.devoffice.com", + "email6": "enny.tester@gmail.com", + "email7": "enny#tester@mail.ru", + "email8": "enny!enny@templatemonster.me", + "email9": "enny@enny.devoffice.com", + "email10": "enny$@enny.devoffice.com", + "email11": "enny%tester@enny.devoffice.com", + "email12": "enny&te@enny.devoffice.com", + "email13": "enn*y@mail.ru", + "email14": "en+ny@yahoo.com", + "email15": "ennyqwerty@gmail.com", + "email16": "ennyqwerty@yandex.com", + "email17": "ennyqwerty@hotmail.com", + "email18": "enny@templatemonster.me", + "email19": "enny-newcheckout@templatemonster.me", + "email20": "enny/tester@enny.devoffice.com", + "email21": "en=ny@enny.devoffice.com", + "email22": "tes?ter@enny.devoffice.com", + "email23": "enny^enny@enny.devoffice.com", + "email24": "enny_sk@enny.devoffice.com", + "email25": "enn`y@enny.devoffice.com", + "email26": "enny|sk@enny.devoffice.com", + "email27": "er@enny.devoffice.com", + "email28": "dd@hj.ff", + "email29": "t@devoffice.com", + "email30": "ennytesttesttesttesttesttesttesttesttesttesttesttesttesttesttest@te1.com", + "email31": "er@enny.devoffice.com", + "empty_field": "", "extra_field": "aaaa" } \ No newline at end of file diff --git a/tests/test_suite/positive/16-email/output.json b/tests/test_suite/positive/16-email/output.json index 26f8b97..6643862 100644 --- a/tests/test_suite/positive/16-email/output.json +++ b/tests/test_suite/positive/16-email/output.json @@ -2,5 +2,33 @@ "email1": "test@mail.com", "email2": "test.test@mail.com.ua", "email3": "test+test.t_e_s_t@mail.in.ua", + "email4": "user@hotline.finance", + "email5": "enny@enny.devoffice.com", + "email6": "enny.tester@gmail.com", + "email7": "enny#tester@mail.ru", + "email8": "enny!enny@templatemonster.me", + "email9": "enny@enny.devoffice.com", + "email10": "enny$@enny.devoffice.com", + "email11": "enny%tester@enny.devoffice.com", + "email12": "enny&te@enny.devoffice.com", + "email13": "enn*y@mail.ru", + "email14": "en+ny@yahoo.com", + "email15": "ennyqwerty@gmail.com", + "email16": "ennyqwerty@yandex.com", + "email17": "ennyqwerty@hotmail.com", + "email18": "enny@templatemonster.me", + "email19": "enny-newcheckout@templatemonster.me", + "email20": "enny/tester@enny.devoffice.com", + "email21": "en=ny@enny.devoffice.com", + "email22": "tes?ter@enny.devoffice.com", + "email23": "enny^enny@enny.devoffice.com", + "email24": "enny_sk@enny.devoffice.com", + "email25": "enn`y@enny.devoffice.com", + "email26": "enny|sk@enny.devoffice.com", + "email27": "er@enny.devoffice.com", + "email28": "dd@hj.ff", + "email29": "t@devoffice.com", + "email30": "ennytesttesttesttesttesttesttesttesttesttesttesttesttesttesttest@te1.com", + "email31": "er@enny.devoffice.com", "empty_field": "" } \ No newline at end of file diff --git a/tests/test_suite/positive/16-email/rules.json b/tests/test_suite/positive/16-email/rules.json index 0edd5d0..16e3146 100644 --- a/tests/test_suite/positive/16-email/rules.json +++ b/tests/test_suite/positive/16-email/rules.json @@ -2,5 +2,33 @@ "email1": "email", "email2": { "email": [] }, "email3": [ { "email": [] } ], + "email4": "email", + "email5": "email", + "email6": "email", + "email7": "email", + "email8": "email", + "email9": "email", + "email10": "email", + "email11": "email", + "email12": "email", + "email13": "email", + "email14": "email", + "email15": "email", + "email16": "email", + "email17": "email", + "email18": "email", + "email19": "email", + "email20": "email", + "email21": "email", + "email22": "email", + "email23": "email", + "email24": "email", + "email25": "email", + "email26": "email", + "email27": "email", + "email28": "email", + "email29": "email", + "email30": "email", + "email31": "email", "empty_field": "email" } \ No newline at end of file diff --git a/tests/test_suite/positive/17-equal_to_field/input.json b/tests/test_suite/positive/17-equal_to_field/input.json index 7b5d8ae..42a0150 100644 --- a/tests/test_suite/positive/17-equal_to_field/input.json +++ b/tests/test_suite/positive/17-equal_to_field/input.json @@ -3,5 +3,7 @@ "field2": "some value", "field3": "some value", "empty_field": "", - "extra_field": "some value" + "extra_field": "some value", + "number_as_string": "10", + "number_as_number": 10 } \ No newline at end of file diff --git a/tests/test_suite/positive/17-equal_to_field/output.json b/tests/test_suite/positive/17-equal_to_field/output.json index 4a2d397..3c8f291 100644 --- a/tests/test_suite/positive/17-equal_to_field/output.json +++ b/tests/test_suite/positive/17-equal_to_field/output.json @@ -2,5 +2,6 @@ "field1": "some value", "field2": "some value", "field3": "some value", - "empty_field": "" + "empty_field": "", + "number_as_string": "10" } \ No newline at end of file diff --git a/tests/test_suite/positive/17-equal_to_field/rules.json b/tests/test_suite/positive/17-equal_to_field/rules.json index 64fdc14..24149df 100644 --- a/tests/test_suite/positive/17-equal_to_field/rules.json +++ b/tests/test_suite/positive/17-equal_to_field/rules.json @@ -2,5 +2,6 @@ "field1": { "equal_to_field": "field2" }, "field2": { "equal_to_field": ["field1"] }, "field3": { "equal_to_field": ["field1"] }, - "empty_field": { "equal_to_field": "field2" } + "empty_field": { "equal_to_field": "field2" }, + "number_as_string": { "equal_to_field": "number_as_number" } } \ No newline at end of file diff --git a/tests/test_suite/positive/18-nested_object/input.json b/tests/test_suite/positive/18-nested_object/input.json index 8883766..ed36cca 100644 --- a/tests/test_suite/positive/18-nested_object/input.json +++ b/tests/test_suite/positive/18-nested_object/input.json @@ -7,9 +7,17 @@ "extra_field": "will be removed" }, + "should_not_be_errored": "", + "should_be_empty": { "extra_field": "will be removed" }, - "extra_field": "will be removed" + "extra_field": "will be removed", + + "builtin_names": { + "constructor": "Constructor value", + "toString": "ToString value", + "hasOwnProperty": "will be removed" + } } diff --git a/tests/test_suite/positive/18-nested_object/output.json b/tests/test_suite/positive/18-nested_object/output.json index 31ae588..488578d 100644 --- a/tests/test_suite/positive/18-nested_object/output.json +++ b/tests/test_suite/positive/18-nested_object/output.json @@ -6,7 +6,12 @@ "building": 10 }, + "should_not_be_errored": "", "should_be_empty": { + }, + "builtin_names": { + "constructor": "Constructor value", + "toString": "ToString value" } } diff --git a/tests/test_suite/positive/18-nested_object/rules.json b/tests/test_suite/positive/18-nested_object/rules.json index 9daf9d2..19ae20c 100644 --- a/tests/test_suite/positive/18-nested_object/rules.json +++ b/tests/test_suite/positive/18-nested_object/rules.json @@ -6,8 +6,17 @@ "building": ["required", "positive_integer" ] } } ], + "should_not_be_errored": { "nested_object": { + "zip": "positive_integer" + }}, + "should_be_empty": [ "required", { "nested_object": { "country": [{ "one_of": [["Ukraine", "USA"]] } ], "zip": "positive_integer" - } } ] + } } ], + + "builtin_names": { "nested_object": { + "constructor": "required", + "toString": "required" + }} } diff --git a/tests/test_suite/positive/20-list_of_objects/input.json b/tests/test_suite/positive/20-list_of_objects/input.json index 335d71a..e168fce 100644 --- a/tests/test_suite/positive/20-list_of_objects/input.json +++ b/tests/test_suite/positive/20-list_of_objects/input.json @@ -2,7 +2,7 @@ "products": [ { "product_id": 10, - "quantity": 10 + "quantity": "10" }, { "product_id": 20, @@ -14,4 +14,4 @@ } ], "empty_list": [] -} +} \ No newline at end of file diff --git a/tests/test_suite/positive/20-list_of_objects/rules.json b/tests/test_suite/positive/20-list_of_objects/rules.json index b0cf4e7..d2392de 100644 --- a/tests/test_suite/positive/20-list_of_objects/rules.json +++ b/tests/test_suite/positive/20-list_of_objects/rules.json @@ -7,4 +7,4 @@ "empty_list": [{"list_of_objects": { "some_field": ["required"] }}] -} +} \ No newline at end of file diff --git a/tests/test_suite/positive/24-iso_date/input.json b/tests/test_suite/positive/24-iso_date/input.json index fef8415..7f16997 100644 --- a/tests/test_suite/positive/24-iso_date/input.json +++ b/tests/test_suite/positive/24-iso_date/input.json @@ -4,6 +4,8 @@ "iso_date3": "1930-01-01", "iso_date4": "1970-01-01", "iso_date5": "2015-10-10", + "iso_date6": "2024-02-29", + "iso_date7": "2000-02-29", "empty_field": "", "extra_field": "aaaa" } diff --git a/tests/test_suite/positive/24-iso_date/output.json b/tests/test_suite/positive/24-iso_date/output.json index 1beb2d1..9df4337 100644 --- a/tests/test_suite/positive/24-iso_date/output.json +++ b/tests/test_suite/positive/24-iso_date/output.json @@ -4,5 +4,7 @@ "iso_date3": "1930-01-01", "iso_date4": "1970-01-01", "iso_date5": "2015-10-10", + "iso_date6": "2024-02-29", + "iso_date7": "2000-02-29", "empty_field": "" } diff --git a/tests/test_suite/positive/24-iso_date/rules.json b/tests/test_suite/positive/24-iso_date/rules.json index f46c8e0..228d4b6 100644 --- a/tests/test_suite/positive/24-iso_date/rules.json +++ b/tests/test_suite/positive/24-iso_date/rules.json @@ -4,5 +4,7 @@ "iso_date3": [ { "iso_date": [] } ], "iso_date4": [ { "iso_date": [] } ], "iso_date5": "iso_date", + "iso_date6": "iso_date", + "iso_date7": "iso_date", "empty_field": "iso_date" } diff --git a/tests/test_suite/positive/25-eq/input.json b/tests/test_suite/positive/25-eq/input.json index 1be4884..da7bcb0 100644 --- a/tests/test_suite/positive/25-eq/input.json +++ b/tests/test_suite/positive/25-eq/input.json @@ -1,7 +1,8 @@ { - "city1": "Moscow", - "city2": "Kiev", - "city3": "Kiev", + "city1": "Lviv", + "city2": "Kyiv", + "city3": "Kyiv", + "city4": "Dnipro", "number1": "2", "number2": 2, "empty_city": "", diff --git a/tests/test_suite/positive/25-eq/output.json b/tests/test_suite/positive/25-eq/output.json index 5ab3edb..428fa63 100644 --- a/tests/test_suite/positive/25-eq/output.json +++ b/tests/test_suite/positive/25-eq/output.json @@ -1,7 +1,8 @@ { - "city1": "Moscow", - "city2": "Kiev", - "city3": "Kiev", + "city1": "Lviv", + "city2": "Kyiv", + "city3": "Kyiv", + "city4": "Dnipro", "number1": 2, "number2": "2", "empty_city": "" diff --git a/tests/test_suite/positive/25-eq/rules.json b/tests/test_suite/positive/25-eq/rules.json index a8b57d6..cf8fa19 100644 --- a/tests/test_suite/positive/25-eq/rules.json +++ b/tests/test_suite/positive/25-eq/rules.json @@ -1,8 +1,9 @@ { - "city1": { "eq": "Moscow" }, - "city2": [{ "eq": "Kiev" }], - "city3": { "eq": "Kiev" }, + "city1": { "eq": "Lviv" }, + "city2": [{ "eq": "Kyiv" }], + "city3": { "eq": "Kyiv" }, + "city4": [{ "eq": ["Dnipro"] }], "number1": { "eq": 2 }, "number2": { "eq": "2" }, - "empty_city": { "eq": "Kiev" } + "empty_city": { "eq": "Kyiv" } } diff --git a/tests/test_suite/positive/26-string/input.json b/tests/test_suite/positive/26-string/input.json index 89c9d5a..f40a5bd 100644 --- a/tests/test_suite/positive/26-string/input.json +++ b/tests/test_suite/positive/26-string/input.json @@ -1,6 +1,6 @@ { - "city1": "Moscow", - "city2": "Kiev", + "city1": "Lviv", + "city2": "Kyiv", "number1": "2", "number2": 2, "empty_city": "", diff --git a/tests/test_suite/positive/26-string/output.json b/tests/test_suite/positive/26-string/output.json index 650648a..a658bd7 100644 --- a/tests/test_suite/positive/26-string/output.json +++ b/tests/test_suite/positive/26-string/output.json @@ -1,6 +1,6 @@ { - "city1": "Moscow", - "city2": "Kiev", + "city1": "Lviv", + "city2": "Kyiv", "number1": "2", "number2": "2", "empty_city": "" diff --git a/tests/test_suite/positive/27-any_object/input.json b/tests/test_suite/positive/27-any_object/input.json index 490ccad..cec69cf 100644 --- a/tests/test_suite/positive/27-any_object/input.json +++ b/tests/test_suite/positive/27-any_object/input.json @@ -1,6 +1,7 @@ { "object1": {"email": "user@mail.com"}, - "object2": {"id": 123, "address": {"city" : "Kiev"}}, + "object2": {"id": 123, "address": {"city" : "Kyiv"}}, "object3": {}, - "empty_field": "" + "empty_field": "", + "object4": {"constructor": "Constructor value"} } diff --git a/tests/test_suite/positive/27-any_object/output.json b/tests/test_suite/positive/27-any_object/output.json index 490ccad..cec69cf 100644 --- a/tests/test_suite/positive/27-any_object/output.json +++ b/tests/test_suite/positive/27-any_object/output.json @@ -1,6 +1,7 @@ { "object1": {"email": "user@mail.com"}, - "object2": {"id": 123, "address": {"city" : "Kiev"}}, + "object2": {"id": 123, "address": {"city" : "Kyiv"}}, "object3": {}, - "empty_field": "" + "empty_field": "", + "object4": {"constructor": "Constructor value"} } diff --git a/tests/test_suite/positive/27-any_object/rules.json b/tests/test_suite/positive/27-any_object/rules.json index bed9342..6a5eee4 100644 --- a/tests/test_suite/positive/27-any_object/rules.json +++ b/tests/test_suite/positive/27-any_object/rules.json @@ -3,5 +3,6 @@ "object2": [{ "any_object": [] }], "object3": "any_object", "empty_field": "any_object", - "missed_field": "any_object" + "missed_field": "any_object", + "object4": "any_object" } diff --git a/tests/test_suite/positive/29-or/input.json b/tests/test_suite/positive/29-or/input.json index 3258b85..c9cb301 100644 --- a/tests/test_suite/positive/29-or/input.json +++ b/tests/test_suite/positive/29-or/input.json @@ -8,6 +8,8 @@ "id3-1": "User@mail.com", "id3-2": "UserUserUser@mail.com", + "id4-1": "", + "products": [ { "product_type": "material", diff --git a/tests/test_suite/positive/29-or/output.json b/tests/test_suite/positive/29-or/output.json index d01e645..41728cc 100644 --- a/tests/test_suite/positive/29-or/output.json +++ b/tests/test_suite/positive/29-or/output.json @@ -8,6 +8,8 @@ "id3-1": "user@mail.com", "id3-2": "UserUserUser@mail.com", + "id4-1": "", + "products": [ { "product_type": "material", diff --git a/tests/test_suite/positive/29-or/rules.json b/tests/test_suite/positive/29-or/rules.json index 732a7ea..9a1d44b 100644 --- a/tests/test_suite/positive/29-or/rules.json +++ b/tests/test_suite/positive/29-or/rules.json @@ -8,6 +8,9 @@ "id3-1": { "or": [{"min_length": 15}, ["email", "to_lc"] ] }, "id3-2": { "or": [{"min_length": 15}, ["email", "to_lc"] ] }, + "id4-1": { "or": ["email", "positive_integer" ] }, + "id4-2": { "or": ["email", "positive_integer" ] }, + "products": {"list_of": { "or": [ {"nested_object": { "product_type": ["required", {"eq": "material"}], diff --git a/tests/test_suite/positive/30-trim/input.json b/tests/test_suite/positive/30-trim/input.json index 431b693..0b0ed95 100644 --- a/tests/test_suite/positive/30-trim/input.json +++ b/tests/test_suite/positive/30-trim/input.json @@ -1,5 +1,7 @@ { "string_value": " value ", + "decimal_value": 1.2, + "unicode_string": " Привет ", "object": {"string_value": " some value "}, "list_of_string_values": [" value1 ", {"key": "123"}, " value2 "] -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/30-trim/output.json b/tests/test_suite/positive/30-trim/output.json index 924c5f6..020821e 100644 --- a/tests/test_suite/positive/30-trim/output.json +++ b/tests/test_suite/positive/30-trim/output.json @@ -1,5 +1,7 @@ { "string_value": "value", + "decimal_value": "1.2", + "unicode_string": "Привет", "object": {"string_value": "some value"}, "list_of_string_values": ["value1", {"key": "123"}, "value2"] -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/30-trim/rules.json b/tests/test_suite/positive/30-trim/rules.json index 7e8cbe7..915f3a5 100644 --- a/tests/test_suite/positive/30-trim/rules.json +++ b/tests/test_suite/positive/30-trim/rules.json @@ -1,5 +1,7 @@ { "string_value": "trim", + "decimal_value": ["trim"], + "unicode_string": "trim", "object": {"nested_object": {"string_value": "trim"} }, "list_of_string_values": {"list_of": "trim"} -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/31-to_lc/input.json b/tests/test_suite/positive/31-to_lc/input.json index d14eb98..89c5ae6 100644 --- a/tests/test_suite/positive/31-to_lc/input.json +++ b/tests/test_suite/positive/31-to_lc/input.json @@ -1,6 +1,8 @@ { "string_value": "Value", + "decimal_value": 1.2, + "unicode_string": "ПРИВЕТ", "object": {"string_value": "Some Value"}, "list_of_string_values": ["VALUE1", {"key": "123"}, "VALUE2"], "to_lc_with_another_rule": "Value" -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/31-to_lc/output.json b/tests/test_suite/positive/31-to_lc/output.json index a8a6ec8..9b80d2f 100644 --- a/tests/test_suite/positive/31-to_lc/output.json +++ b/tests/test_suite/positive/31-to_lc/output.json @@ -1,6 +1,8 @@ { "string_value": "value", + "decimal_value": "1.2", + "unicode_string": "привет", "object": {"string_value": "some value"}, "list_of_string_values": ["value1", {"key": "123"}, "value2"], "to_lc_with_another_rule": "value" -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/31-to_lc/rules.json b/tests/test_suite/positive/31-to_lc/rules.json index b0108d4..b130f81 100644 --- a/tests/test_suite/positive/31-to_lc/rules.json +++ b/tests/test_suite/positive/31-to_lc/rules.json @@ -1,6 +1,8 @@ { "string_value": "to_lc", + "decimal_value": ["to_lc"], + "unicode_string": "to_lc", "object": {"nested_object": {"string_value": "to_lc"} }, "list_of_string_values": {"list_of": "to_lc"}, "to_lc_with_another_rule": ["to_lc", "required"] -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/32-to_uc/input.json b/tests/test_suite/positive/32-to_uc/input.json index 8a657be..9be5631 100644 --- a/tests/test_suite/positive/32-to_uc/input.json +++ b/tests/test_suite/positive/32-to_uc/input.json @@ -1,5 +1,7 @@ { "string_value": "Value", + "unicode_string":"привет", + "decimal_value": 1.2, "object": {"string_value": "Some Value"}, "list_of_string_values": ["value1", {"key": "123"}, "value2"] -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/32-to_uc/output.json b/tests/test_suite/positive/32-to_uc/output.json index 58f6fa5..5e66ec5 100644 --- a/tests/test_suite/positive/32-to_uc/output.json +++ b/tests/test_suite/positive/32-to_uc/output.json @@ -1,5 +1,7 @@ { "string_value": "VALUE", + "unicode_string": "ПРИВЕТ", + "decimal_value": "1.2", "object": {"string_value": "SOME VALUE"}, "list_of_string_values": ["VALUE1", {"key": "123"}, "VALUE2"] -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/32-to_uc/rules.json b/tests/test_suite/positive/32-to_uc/rules.json index c4ae195..ef374f8 100644 --- a/tests/test_suite/positive/32-to_uc/rules.json +++ b/tests/test_suite/positive/32-to_uc/rules.json @@ -1,5 +1,7 @@ { "string_value": "to_uc", + "unicode_string": "to_uc", + "decimal_value": ["to_uc"], "object": {"nested_object": {"string_value": "to_uc"} }, "list_of_string_values": {"list_of": "to_uc"} -} \ No newline at end of file +} diff --git a/tests/test_suite/positive/35-default/input.json b/tests/test_suite/positive/35-default/input.json index 29ffa49..c2e8cd1 100644 --- a/tests/test_suite/positive/35-default/input.json +++ b/tests/test_suite/positive/35-default/input.json @@ -3,7 +3,7 @@ "empty_value2": "", "empty_value3": "", "zero_value": 0, - + "null_value": null, "string_value": "Value", "object": {"name": "Value"}, "list_of_string_values": ["value1", {"key": "123"}, "value2"] diff --git a/tests/test_suite/positive/35-default/output.json b/tests/test_suite/positive/35-default/output.json index a964ba9..1de02af 100644 --- a/tests/test_suite/positive/35-default/output.json +++ b/tests/test_suite/positive/35-default/output.json @@ -3,9 +3,11 @@ "empty_value2": [], "empty_value3": {}, "zero_value": 0, + "null_value": "12", "missed_value": "15", "string_value": "Value", "object": {"name": "Value"}, - "list_of_string_values": ["value1", {"key": "123"}, "value2"] + "list_of_string_values": ["value1", {"key": "123"}, "value2"], + "constructor": "16" } diff --git a/tests/test_suite/positive/35-default/rules.json b/tests/test_suite/positive/35-default/rules.json index 6d3c5df..75c612e 100644 --- a/tests/test_suite/positive/35-default/rules.json +++ b/tests/test_suite/positive/35-default/rules.json @@ -3,9 +3,11 @@ "empty_value2": {"default": [[]]}, "empty_value3": [{"default": {}}], "zero_value": {"default": 10}, + "null_value": {"default":"12"}, "missed_value": {"default": "15"}, "string_value": {"default": 10}, "object": {"default": 10}, - "list_of_string_values": {"default": 10} + "list_of_string_values": {"default": 10}, + "constructor": {"default": "16"} } From 8b8efa8475221ec7f80267b3f9f6f4d8e8ddc553 Mon Sep 17 00:00:00 2001 From: Viktor Turskyi Date: Sun, 18 Oct 2026 20:38:19 +0300 Subject: [PATCH 2/2] Trim lists, replace default rules, allow empty lists of rules Found with the tests of the JavaScript implementation that apply to PHP. - Auto trim trims the values of lists too, not only of objects - registerDefaultRules() replaces registered rules with the same names, as the README shows for max_number. "+" kept the registered rules. - An empty list of rules ('field' => [] or list_of: []) is a list without rules, not a rule without a name ("Rule [] not registered") --- lib/Validator/LIVR.php | 9 +++++--- lib/Validator/LIVR/Rules/Meta.php | 7 +++--- tests/03-auto_trim.php | 3 +++ tests/05-rules_edge_cases.php | 37 +++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/lib/Validator/LIVR.php b/lib/Validator/LIVR.php index 3ae0ef0..4a45d7b 100644 --- a/lib/Validator/LIVR.php +++ b/lib/Validator/LIVR.php @@ -65,7 +65,8 @@ class LIVR public static function registerDefaultRules($rules) { - self::$DEFAULT_RULES = self::$DEFAULT_RULES + $rules; + // Rules with the names of registered ones replace them + self::$DEFAULT_RULES = array_merge(self::$DEFAULT_RULES, $rules); return; } @@ -108,7 +109,8 @@ public function prepare() } foreach ($this->livrRules as $field => $fieldRules) { - if (!is_array($fieldRules) || \Validator\LIVR\Util::isAssocArray($fieldRules)) { + // An empty array is an empty list of rules + if (!is_array($fieldRules) || !\Validator\LIVR\Util::isList($fieldRules)) { $fieldRules = array($fieldRules); } @@ -276,7 +278,8 @@ private function autoTrim($data) { if (is_string($data)) { return trim($data); - } elseif (\Validator\LIVR\Util::isAssocArray($data)) { + } elseif (is_array($data)) { + // Objects and lists $trimmedData = array(); foreach ($data as $key => $value) { $trimmedData[$key] = $this->autoTrim($value); diff --git a/lib/Validator/LIVR/Rules/Meta.php b/lib/Validator/LIVR/Rules/Meta.php index 947202a..180f401 100644 --- a/lib/Validator/LIVR/Rules/Meta.php +++ b/lib/Validator/LIVR/Rules/Meta.php @@ -33,7 +33,7 @@ public static function listOf() { $first_arg = func_get_arg(0); - if (is_array($first_arg) && !\Validator\LIVR\Util::isAssocArray($first_arg)) { + if (\Validator\LIVR\Util::isList($first_arg)) { $livr = func_get_arg(0); $ruleBuilders = func_get_arg(1); } else { @@ -60,8 +60,9 @@ public static function listOf() foreach ($values as $value) { $result = $validator->validate(array('field' => $value)); - if ($result) { - $results[] = $result['field']; + // The result is empty without rules + if ($result !== false && $result !== null) { + $results[] = isset($result['field']) ? $result['field'] : null; $errors[] = null; } else { $results[] = null; diff --git a/tests/03-auto_trim.php b/tests/03-auto_trim.php index 4f701eb..c4f2957 100644 --- a/tests/03-auto_trim.php +++ b/tests/03-auto_trim.php @@ -9,6 +9,7 @@ class AutoTrimTest extends TestCase private $rules = array( 'code' => array('required'), 'password' => array('required', array( 'min_length' => 3 ) ), + 'list' => array('list_of' => array('string')), 'address' => array('nested_object' => array( 'street' => array( array( 'min_length'=> 5 ) ) ) @@ -22,6 +23,7 @@ public function testPositive() $input = array( 'code' => ' A ', 'password' => ' 123 ', + 'list' => array(' aaa ', ' bbb '), 'address' => array( 'street' => ' hello ' ) @@ -30,6 +32,7 @@ public function testPositive() $output = array( 'code' => 'A', 'password' => '123', + 'list' => array('aaa', 'bbb'), 'address' => array( 'street' => 'hello' ) diff --git a/tests/05-rules_edge_cases.php b/tests/05-rules_edge_cases.php index f536396..40536b5 100644 --- a/tests/05-rules_edge_cases.php +++ b/tests/05-rules_edge_cases.php @@ -27,4 +27,41 @@ public function testListOfDifferentObjectsWithNotList() $this->assertEquals(array('products' => 'FORMAT_ERROR'), $validator->getErrors(), 'Should return FORMAT_ERROR'); } } + + public function testRegisterDefaultRulesReplacesRules() + { + $defaultRules = Validator\LIVR::getDefaultRules(); + + Validator\LIVR::registerDefaultRules(array( + 'max_length' => function ($maxLength) use ($defaultRules) { + $maxLengthRule = call_user_func($defaultRules['max_length'], $maxLength); + + return function ($value) use ($maxLengthRule) { + return $maxLengthRule($value) ? 'MY_TOO_LONG' : null; + }; + }, + )); + + try { + $validator = new Validator\LIVR(array('phone' => array('max_length' => 10))); + $this->assertFalse($validator->validate(array('phone' => '123456789123456')), 'Should return false'); + $this->assertEquals(array('phone' => 'MY_TOO_LONG'), $validator->getErrors(), 'Should use the new rule'); + } finally { + Validator\LIVR::registerDefaultRules(array('max_length' => $defaultRules['max_length'])); + } + } + + public function testEmptyListsOfRules() + { + $validator = new Validator\LIVR(array( + 'no_rules' => array(), + 'list' => array('list_of' => array()), + )); + + $this->assertEquals( + array('list' => array(null, null)), + $validator->validate(array('no_rules' => 'x', 'list' => array(1, 2))), + 'Should skip fields without rules' + ); + } }