-
Notifications
You must be signed in to change notification settings - Fork 14
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
Implement multipleOf #29
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
82a4c56
Implement multipleOf
lexidor 70287d3
HHVM 4.0 BC
lexidor 45498df
Update last forgotten toEqual()s
lexidor fc844a3
Fix TNumberSchema
lexidor 9745f47
Merge branch 'master' into multipleOf
2d3f4f5
Add some comments and remove a var_dump()
lexidor 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 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 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,49 @@ | ||
<?hh // strict | ||
|
||
namespace Slack\Hack\JsonSchema\Constraints; | ||
|
||
use namespace HH\Lib\Math; | ||
use namespace Slack\Hack\JsonSchema; | ||
|
||
// Because comparing floating point numbers using `===` will have undesierable results, | ||
// we will compare with a little bit of leeway. | ||
// We are looking for a remainer of 0 after the modulo. | ||
// `30 % 3` is 0, so 30 is a multiple of 3. | ||
// `fmod(6., .6)` is not 0, because of floating point rounding errors. | ||
// It is 2.220...E-16, but this is almost zero, so we pass the test. | ||
const float COMPARISON_LEEWAY = 10. ** -6; | ||
|
||
class NumberMultipleOfConstraint { | ||
public static function check(num $dividend, num $devisor, string $pointer): void { | ||
invariant($devisor > 0, 'multipleOf 0 or a negative number does not make sense. Use a positive non-zero number.'); | ||
|
||
$remainer = Math\abs( | ||
$dividend is int && $devisor is int ? $dividend % $devisor : \fmod((float)$dividend, (float)$devisor), | ||
); | ||
|
||
$error = shape( | ||
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT, | ||
'constraint' => shape( | ||
'type' => JsonSchema\FieldErrorConstraint::MULTIPLE_OF, | ||
'expected' => $devisor, | ||
'got' => $dividend, | ||
), | ||
'message' => "must be a multiple of {$devisor}", | ||
); | ||
|
||
if ($remainer is int && $remainer !== 0) { | ||
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]); | ||
} | ||
if ($remainer is float) { | ||
// If we are closer to 0 than to the devisor, we check assert that our remainer | ||
// is less than our COMPARISON_LEEWAY. | ||
if ($remainer < $devisor / 2 && $remainer > COMPARISON_LEEWAY) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to have a comment about this logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that is a good plan. |
||
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]); | ||
// However, sometimes the remainer is very close to the devisor. | ||
// That could also indicate a multiple of the devisor. | ||
} else if ($remainer > $devisor / 2 && ($devisor - $remainer) > COMPARISON_LEEWAY) { | ||
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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,63 @@ | ||
<?hh // partial | ||
|
||
namespace Slack\Hack\JsonSchema\Tests; | ||
|
||
use function Facebook\FBExpect\expect; | ||
use type Facebook\HackTest\DataProvider; | ||
use namespace Slack\Hack\JsonSchema; | ||
|
||
final class MultipleOfConstraintTest extends BaseCodegenTestCase { | ||
|
||
<<__Override>> | ||
public static async function beforeFirstTestAsync(): Awaitable<void> { | ||
$ret = self::getBuilder('multiple-of.json', 'MultipleOfValidator'); | ||
$ret['codegen']->build(); | ||
require_once($ret['path']); | ||
} | ||
|
||
public function provideCasesForIsValid( | ||
): dict< | ||
string, | ||
(shape(?'a_multiple_of_five_int' => int, ?'a_multiple_of_1_point_one_repeating_number' => num), bool), | ||
> { | ||
return dict[ | ||
'zero is a multiple of 5' => tuple(shape('a_multiple_of_five_int' => 0), true), | ||
'one is not a multiple of 5' => tuple(shape('a_multiple_of_five_int' => 1), false), | ||
'five is a multiple of 5' => tuple(shape('a_multiple_of_five_int' => 5), true), | ||
'fifty is a multiple of 5' => tuple(shape('a_multiple_of_five_int' => 50), true), | ||
'zero is a mutliple of 1.1...' => tuple(shape('a_multiple_of_1_point_one_repeating_number' => 0), true), | ||
'one is not a mutliple of 1.1...' => tuple(shape('a_multiple_of_1_point_one_repeating_number' => 1), false), | ||
'5.5... is a multiple of 1.1...' => | ||
// This will have an modulus of 1.1111111111056, testing if I can deal with it being slightly below the devidor. | ||
tuple(shape('a_multiple_of_1_point_one_repeating_number' => 5.55555555555), true), | ||
'5.5...6 is a multiple of 1.1... if you place the 6 far enough back' => | ||
// This will have an modulus of 4.4444449986969E-7, testing if I can deal with it being slightly above zero. | ||
tuple(shape('a_multiple_of_1_point_one_repeating_number' => 5.555556), true), | ||
'5555555.5... is a multiple of 1.1...' => | ||
tuple(shape('a_multiple_of_1_point_one_repeating_number' => 55555555.55555555555), true), | ||
// I arbitrarily choose to check for 6 digits. These tests need updating if we choose a different number of digits. | ||
'1.11111 <- 5 times a one behind the period is a multiple of 1.1...' => | ||
tuple(shape('a_multiple_of_1_point_one_repeating_number' => 1.11111), false), | ||
'1.111111 <- 6 times a one behind the period is a multiple of 1.1...' => | ||
tuple(shape('a_multiple_of_1_point_one_repeating_number' => 1.111111), true), | ||
]; | ||
} | ||
|
||
<<DataProvider('provideCasesForIsValid')>> | ||
public function testIsValid(shape(...) $value, bool $is_valid): void { | ||
$schema = new Generated\MultipleOfValidator($value); | ||
$schema->validate(); | ||
expect($schema->isValid())->toBeSame($is_valid); | ||
} | ||
|
||
public function testForError(): void { | ||
$schema = new Generated\MultipleOfValidator(shape('a_multiple_of_five_int' => 1)); | ||
$schema->validate(); | ||
$err = $schema->getErrors()[0]; | ||
expect(Shapes::idx($err, 'constraint')as nonnull['type'])->toBeSame(JsonSchema\FieldErrorConstraint::MULTIPLE_OF); | ||
expect(Shapes::idx($err, 'constraint') |> Shapes::idx($$ as nonnull, 'got'))->toBeSame(1); | ||
expect(Shapes::idx($err, 'constraint') |> Shapes::idx($$ as nonnull, 'expected'))->toBeSame(5); | ||
expect($err['message'])->toBeSame('must be a multiple of 5'); | ||
} | ||
|
||
} |
This file contains 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,109 @@ | ||
<?hh // strict | ||
/** | ||
* This file is generated. Do not modify it manually! | ||
* | ||
* To re-generate this file run `make test` | ||
* | ||
* | ||
* @generated SignedSource<<2a4a4e3e9c399de1de58724be9cf4435>> | ||
*/ | ||
namespace Slack\Hack\JsonSchema\Tests\Generated; | ||
use namespace Slack\Hack\JsonSchema; | ||
use namespace Slack\Hack\JsonSchema\Constraints; | ||
|
||
type TMultipleOfValidator = shape( | ||
?'a_multiple_of_five_int' => int, | ||
?'a_multiple_of_1_point_one_repeating_number' => num, | ||
... | ||
); | ||
|
||
final class MultipleOfValidatorPropertiesAMultipleOfFiveInt { | ||
|
||
private static num $devisorForMultipleOf = 5; | ||
private static bool $coerce = false; | ||
|
||
public static function check(mixed $input, string $pointer): int { | ||
$typed = | ||
Constraints\IntegerConstraint::check($input, $pointer, self::$coerce); | ||
|
||
Constraints\NumberMultipleOfConstraint::check( | ||
$typed, | ||
self::$devisorForMultipleOf, | ||
$pointer, | ||
); | ||
return $typed; | ||
} | ||
} | ||
|
||
final class MultipleOfValidatorPropertiesAMultipleOf1PointOneRepeatingNumber { | ||
|
||
private static num $devisorForMultipleOf = 1.1111111111111; | ||
private static bool $coerce = false; | ||
|
||
public static function check(mixed $input, string $pointer): num { | ||
$typed = Constraints\NumberConstraint::check($input, $pointer, self::$coerce); | ||
|
||
Constraints\NumberMultipleOfConstraint::check( | ||
$typed, | ||
self::$devisorForMultipleOf, | ||
$pointer, | ||
); | ||
return $typed; | ||
} | ||
} | ||
|
||
final class MultipleOfValidator | ||
extends JsonSchema\BaseValidator<TMultipleOfValidator> { | ||
|
||
private static bool $coerce = false; | ||
|
||
public static function check( | ||
mixed $input, | ||
string $pointer, | ||
): TMultipleOfValidator { | ||
$typed = Constraints\ObjectConstraint::check($input, $pointer, self::$coerce); | ||
|
||
$errors = vec[]; | ||
$output = shape(); | ||
|
||
/*HHAST_IGNORE_ERROR[UnusedVariable] Some loops generated with this statement do not use their $value*/ | ||
foreach ($typed as $key => $value) { | ||
/* HH_IGNORE_ERROR[4051] allow dynamic access to preserve input. See comment in the codegen lib for reasoning and alternatives if needed. */ | ||
$output[$key] = $value; | ||
} | ||
|
||
if (\HH\Lib\C\contains_key($typed, 'a_multiple_of_five_int')) { | ||
try { | ||
$output['a_multiple_of_five_int'] = MultipleOfValidatorPropertiesAMultipleOfFiveInt::check( | ||
$typed['a_multiple_of_five_int'], | ||
JsonSchema\get_pointer($pointer, 'a_multiple_of_five_int'), | ||
); | ||
} catch (JsonSchema\InvalidFieldException $e) { | ||
$errors = \HH\Lib\Vec\concat($errors, $e->errors); | ||
} | ||
} | ||
|
||
if (\HH\Lib\C\contains_key($typed, 'a_multiple_of_1_point_one_repeating_number')) { | ||
try { | ||
$output['a_multiple_of_1_point_one_repeating_number'] = MultipleOfValidatorPropertiesAMultipleOf1PointOneRepeatingNumber::check( | ||
$typed['a_multiple_of_1_point_one_repeating_number'], | ||
JsonSchema\get_pointer($pointer, 'a_multiple_of_1_point_one_repeating_number'), | ||
); | ||
} catch (JsonSchema\InvalidFieldException $e) { | ||
$errors = \HH\Lib\Vec\concat($errors, $e->errors); | ||
} | ||
} | ||
|
||
if (\HH\Lib\C\count($errors)) { | ||
throw new JsonSchema\InvalidFieldException($pointer, $errors); | ||
} | ||
|
||
/* HH_IGNORE_ERROR[4163] */ | ||
return $output; | ||
} | ||
|
||
<<__Override>> | ||
protected function process(): TMultipleOfValidator { | ||
return self::check($this->input, $this->pointer); | ||
} | ||
} |
This file contains 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,13 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"a_multiple_of_five_int": { | ||
"type": "integer", | ||
"multipleOf": 5 | ||
}, | ||
"a_multiple_of_1_point_one_repeating_number": { | ||
"type": "number", | ||
"multipleOf": 1.1111111111111111 | ||
} | ||
} | ||
} |
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.
Can we add a comment about this?
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.
Sure, will do.