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

Fix Number constraints error messages and make String constraints error messages consistent #75

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/Constraints/NumberMaximumConstraint.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?hh // strict

use namespace HH\Lib\Str;
namespace Slack\Hack\JsonSchema\Constraints;

use namespace HH\Lib\Str;
use namespace Slack\Hack\JsonSchema;

class NumberMaximumConstraint {
Expand All @@ -15,7 +15,7 @@ public static function check(num $input, num $maximum, string $pointer): void {
'expected' => $maximum,
'got' => $input,
),
'message' => Str\format('must be less than %s', (string)$maximum),
'message' => Str\format('must be less than or equal to %s', (string)$maximum),
);
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Constraints/NumberMinimumConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function check(num $input, num $minimum, string $pointer): void {
'expected' => $minimum,
'got' => $input,
),
'message' => Str\format('must be greater than %s', (string)$minimum),
'message' => Str\format('must be greater than or equal to %s', (string)$minimum),
);
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);

Expand Down
3 changes: 2 additions & 1 deletion src/Constraints/StringMaxLengthConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Slack\Hack\JsonSchema\Constraints;

use namespace HH\Lib\Str;
use namespace Slack\Hack\JsonSchema;

class StringMaxLengthConstraint {
Expand All @@ -14,7 +15,7 @@ public static function check(int $length, int $maximum, string $pointer): void {
'expected' => $maximum,
'got' => $length,
),
'message' => 'must be less than '.($maximum + 1).' characters',
'message' => Str\format('must be less than or equal to %s characters', (string)$maximum),
);
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Constraints/StringMinLengthConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Slack\Hack\JsonSchema\Constraints;

use namespace HH\Lib\Str;
use namespace Slack\Hack\JsonSchema;

class StringMinLengthConstraint {
Expand All @@ -14,7 +15,7 @@ public static function check(int $length, int $minimum, string $pointer): void {
'expected' => $minimum,
'got' => $length,
),
'message' => 'must be more than '.($minimum - 1).' characters',
'message' => Str\format('must be greater than or equal to %s characters', (string)$minimum),
);
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);
}
Expand Down
55 changes: 44 additions & 11 deletions tests/NumericalSchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ public function testInteger(): void {
$cases = vec[
shape(
'input' => darray['integer' => 1000],
'output' => darray['integer' => 1000],
'output' => darray[
'integer_limits' => 5,
'integer' => 1000
],
'valid' => true,
),
shape(
'input' => darray['integer' => 0],
'output' => darray['integer' => 0],
'output' => darray[
'integer_limits' => 5,
'integer' => 0
],
'valid' => true,
),
shape('input' => darray['integer' => '1000'], 'valid' => false),
Expand All @@ -42,12 +48,18 @@ public function testNumber(): void {
$cases = vec[
shape(
'input' => darray['number' => 1000],
'output' => darray['number' => 1000],
'output' => darray[
'integer_limits' => 5,
'number' => 1000
],
'valid' => true,
),
shape(
'input' => darray['number' => 1000.00],
'output' => darray['number' => 1000.00],
'output' => darray[
'integer_limits' => 5,
'number' => 1000.00
],
'valid' => true,
),
shape('input' => darray['number' => '1000'], 'valid' => false),
Expand All @@ -61,12 +73,18 @@ public function testIntegerCoerce(): void {
$cases = vec[
shape(
'input' => darray['integer_coerce' => 1],
'output' => darray['integer_coerce' => 1],
'output' => darray[
'integer_limits' => 5,
'integer_coerce' => 1
],
'valid' => true,
),
shape(
'input' => darray['integer_coerce' => '100'],
'output' => darray['integer_coerce' => 100],
'output' => darray[
'integer_limits' => 5,
'integer_coerce' => 100
],
'valid' => true,
),
shape(
Expand All @@ -86,17 +104,26 @@ public function testNumberCoerce(): void {
$cases = vec[
shape(
'input' => darray['number_coerce' => 1.0],
'output' => darray['number_coerce' => 1.0],
'output' => darray[
'integer_limits' => 5,
'number_coerce' => 1.0
],
'valid' => true,
),
shape(
'input' => darray['number_coerce' => '100'],
'output' => darray['number_coerce' => 100],
'output' => darray[
'integer_limits' => 5,
'number_coerce' => 100
],
'valid' => true,
),
shape(
'input' => darray['number_coerce' => '100.0'],
'output' => darray['number_coerce' => 100.0],
'output' => darray[
'integer_limits' => 5,
'number_coerce' => 100.0
],
'valid' => true,
),
shape(
Expand All @@ -112,12 +139,18 @@ public function testHackEnum(): void {
$cases = vec[
shape(
'input' => darray['hack_enum' => 1],
'output' => darray['hack_enum' => TestIntEnum::ABC],
'output' => darray[
'integer_limits' => 5,
'hack_enum' => TestIntEnum::ABC
],
'valid' => true,
),
shape(
'input' => darray['hack_enum' => 2],
'output' => darray['hack_enum' => TestIntEnum::DEF],
'output' => darray[
'integer_limits' => 5,
'hack_enum' => TestIntEnum::DEF
],
'valid' => true,
),
shape('input' => darray['hack_enum' => 0], 'valid' => false),
Expand Down
10 changes: 8 additions & 2 deletions tests/TopLevelRefValidatorTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ final class TopLevelRefValidatorTest extends BaseCodegenTestCase {
$cases = vec[
shape(
'input' => darray['integer' => 1000],
'output' => darray['integer' => 1000],
'output' => darray[
'integer_limits' => 5,
'integer' => 1000
],
'valid' => true,
),
shape(
'input' => darray['integer' => 0],
'output' => darray['integer' => 0],
'output' => darray[
'integer_limits' => 5,
'integer' => 0
],
'valid' => true,
),
shape('input' => darray['integer' => '1000'], 'valid' => false),
Expand Down
44 changes: 43 additions & 1 deletion tests/examples/codegen/ExamplesNumericalSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* To re-generate this file run `make test`
*
*
* @generated SignedSource<<69e5c34e78272d88aec25b725df66a41>>
* @generated SignedSource<<4e71e0ec7d07d93792cc2bd02d3a3c8d>>
*/
namespace Slack\Hack\JsonSchema\Tests\Generated;
use namespace Slack\Hack\JsonSchema;
Expand All @@ -17,6 +17,7 @@
?'integer_coerce' => int,
?'number_coerce' => num,
?'hack_enum' => \Slack\Hack\JsonSchema\Tests\TestIntEnum,
'integer_limits' => int,
...
);

Expand Down Expand Up @@ -86,6 +87,30 @@ public static function check(
}
}

final class ExamplesNumericalSchemaPropertiesIntegerLimits {

private static int $maximum = 10;
private static int $minimum = 1;
private static bool $coerce = false;

public static function check(mixed $input, string $pointer): int {
$typed =
Constraints\IntegerConstraint::check($input, $pointer, self::$coerce);

Constraints\NumberMaximumConstraint::check(
$typed,
self::$maximum,
$pointer,
);
Constraints\NumberMinimumConstraint::check(
$typed,
self::$minimum,
$pointer,
);
return $typed;
}
}

final class ExamplesNumericalSchema
extends JsonSchema\BaseValidator<TExamplesNumericalSchema> {

Expand All @@ -97,6 +122,12 @@ public static function check(
): TExamplesNumericalSchema {
$typed = Constraints\ObjectConstraint::check($input, $pointer, self::$coerce);

$defaults = dict[
'integer_limits' => 5,
];
$typed = \HH\Lib\Dict\merge($defaults, $typed);


$errors = vec[];
$output = shape();

Expand Down Expand Up @@ -161,6 +192,17 @@ public static function check(
}
}

if (\HH\Lib\C\contains_key($typed, 'integer_limits')) {
try {
$output['integer_limits'] = ExamplesNumericalSchemaPropertiesIntegerLimits::check(
$typed['integer_limits'],
JsonSchema\get_pointer($pointer, 'integer_limits'),
);
} catch (JsonSchema\InvalidFieldException $e) {
$errors = \HH\Lib\Vec\concat($errors, $e->errors);
}
}

if (\HH\Lib\C\count($errors)) {
throw new JsonSchema\InvalidFieldException($pointer, $errors);
}
Expand Down
44 changes: 43 additions & 1 deletion tests/examples/codegen/NumericalSchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* To re-generate this file run `make test`
*
*
* @generated SignedSource<<5d96bf78c70de4ebc3454eec9a957550>>
* @generated SignedSource<<922cf6e7d1d33858846294df801f20f3>>
*/
namespace Slack\Hack\JsonSchema\Tests\Generated;
use namespace Slack\Hack\JsonSchema;
Expand All @@ -17,6 +17,7 @@
?'integer_coerce' => int,
?'number_coerce' => num,
?'hack_enum' => \Slack\Hack\JsonSchema\Tests\TestIntEnum,
'integer_limits' => int,
...
);

Expand Down Expand Up @@ -86,6 +87,30 @@ public static function check(
}
}

final class NumericalSchemaValidatorPropertiesIntegerLimits {

private static int $maximum = 10;
private static int $minimum = 1;
private static bool $coerce = false;

public static function check(mixed $input, string $pointer): int {
$typed =
Constraints\IntegerConstraint::check($input, $pointer, self::$coerce);

Constraints\NumberMaximumConstraint::check(
$typed,
self::$maximum,
$pointer,
);
Constraints\NumberMinimumConstraint::check(
$typed,
self::$minimum,
$pointer,
);
return $typed;
}
}

final class NumericalSchemaValidator
extends JsonSchema\BaseValidator<TNumericalSchemaValidator> {

Expand All @@ -97,6 +122,12 @@ public static function check(
): TNumericalSchemaValidator {
$typed = Constraints\ObjectConstraint::check($input, $pointer, self::$coerce);

$defaults = dict[
'integer_limits' => 5,
];
$typed = \HH\Lib\Dict\merge($defaults, $typed);


$errors = vec[];
$output = shape();

Expand Down Expand Up @@ -161,6 +192,17 @@ public static function check(
}
}

if (\HH\Lib\C\contains_key($typed, 'integer_limits')) {
try {
$output['integer_limits'] = NumericalSchemaValidatorPropertiesIntegerLimits::check(
$typed['integer_limits'],
JsonSchema\get_pointer($pointer, 'integer_limits'),
);
} catch (JsonSchema\InvalidFieldException $e) {
$errors = \HH\Lib\Vec\concat($errors, $e->errors);
}
}

if (\HH\Lib\C\count($errors)) {
throw new JsonSchema\InvalidFieldException($pointer, $errors);
}
Expand Down
8 changes: 7 additions & 1 deletion tests/examples/numerical-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
"hack_enum": {
"type": "integer",
"hackEnum": "Slack\\Hack\\JsonSchema\\Tests\\TestIntEnum"
},
"integer_limits": {
"type": "integer",
"minimum": 1,
"maximum": 10,
"default": 5
}
}
}
}
6 changes: 5 additions & 1 deletion tests/examples/string-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@
"type": "string",
"hackEnum": "Slack\\Hack\\JsonSchema\\Tests\\TestStringEnum"
},
"min_length": {
"type": "string",
"minLength": 1
},
"max_length": {
"type": "string",
"maxLength": 10
}
}
}
}
Loading