-
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
Support minProperties and maxProperties on objects #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
?'additionalProperties' => mixed, | ||
?'patternProperties' => dict<string, TSchema>, | ||
?'coerce' => bool, | ||
?'minProperties' => int, | ||
?'maxProperties' => int, | ||
... | ||
); | ||
|
||
|
@@ -74,6 +76,34 @@ public function build(): this { | |
->setValue($hb->getCode(), HackBuilderValues::literal()); | ||
} | ||
|
||
$max_properties = $this->typed_schema['maxProperties'] ?? null; | ||
if ($max_properties is nonnull) { | ||
if ($max_properties < 0) { | ||
throw new \Exception('maxProperties must be a non-negative integer'); | ||
} | ||
|
||
$class_properties[] = $this->codegenProperty('maxProperties') | ||
->setType('int') | ||
->setValue($max_properties, HackBuilderValues::export()); | ||
} | ||
|
||
$min_properties = $this->typed_schema['minProperties'] ?? null; | ||
if ($min_properties is nonnull) { | ||
if ($min_properties < 0) { | ||
throw new \Exception('minProperties must be a non-negative integer'); | ||
} | ||
|
||
$class_properties[] = $this->codegenProperty('minProperties') | ||
->setType('int') | ||
->setValue($min_properties, HackBuilderValues::export()); | ||
} | ||
|
||
if ($min_properties is nonnull && $max_properties is nonnull) { | ||
if ($min_properties > $max_properties) { | ||
throw new \Exception('maxProperties must be greater than minProperties'); | ||
} | ||
} | ||
|
||
$class->addProperties($class_properties); | ||
$this->addBuilderClass($class); | ||
|
||
|
@@ -132,6 +162,29 @@ protected function getCheckMethodCode( | |
$include_error_handling = true; | ||
} | ||
|
||
$max_properties = $this->typed_schema['maxProperties'] ?? null; | ||
$min_properties = $this->typed_schema['minProperties'] ?? null; | ||
Comment on lines
+165
to
+166
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. someone asked about whether we should validate if these integers are non-negative. i noticed we don't validate that for if we were to add that check for objects, i'm trying to figure out where the best place for it would be so that it'd error out for the developer trying to generate a schema with negative min/max numbers. doesn't seem like the 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. It wasn't intentional that we left those off, just didn't get around to it. I'd add it anywhere in this codegen path, maybe above where you set the class properties? 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. okay, sounds good - i added checks for those above in the |
||
|
||
if ($max_properties is nonnull || $min_properties is nonnull) { | ||
$hb->addAssignment('$length', '\HH\Lib\C\count($typed)', HackBuilderValues::literal())->ensureEmptyLine(); | ||
} | ||
|
||
if ($max_properties is nonnull) { | ||
$hb->addMultilineCall( | ||
'Constraints\ObjectMaxPropertiesConstraint::check', | ||
vec['$length', 'self::$maxProperties', '$pointer'], | ||
) | ||
->ensureEmptyLine(); | ||
} | ||
|
||
if ($min_properties is nonnull) { | ||
$hb->addMultilineCall( | ||
'Constraints\ObjectMinPropertiesConstraint::check', | ||
vec['$length', 'self::$minProperties', '$pointer'], | ||
) | ||
->ensureEmptyLine(); | ||
} | ||
|
||
$defaults = $this->getDefaults(); | ||
if (!C\is_empty($defaults)) { | ||
$hb->ensureEmptyLine(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?hh // strict | ||
|
||
namespace Slack\Hack\JsonSchema\Constraints; | ||
|
||
use namespace Slack\Hack\JsonSchema; | ||
|
||
class ObjectMaxPropertiesConstraint { | ||
public static function check(int $num_properties, int $max_properties, string $pointer): void { | ||
if ($num_properties > $max_properties) { | ||
$error = shape( | ||
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT, | ||
'constraint' => shape( | ||
'type' => JsonSchema\FieldErrorConstraint::MAX_PROPERTIES, | ||
'expected' => $max_properties, | ||
'got' => $num_properties, | ||
), | ||
'message' => "no more than {$max_properties} properties allowed", | ||
); | ||
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?hh // strict | ||
|
||
namespace Slack\Hack\JsonSchema\Constraints; | ||
|
||
use namespace Slack\Hack\JsonSchema; | ||
|
||
class ObjectMinPropertiesConstraint { | ||
public static function check(int $num_properties, int $min_properties, string $pointer): void { | ||
if ($num_properties < $min_properties) { | ||
$error = shape( | ||
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT, | ||
'constraint' => shape( | ||
'type' => JsonSchema\FieldErrorConstraint::MIN_PROPERTIES, | ||
'expected' => $min_properties, | ||
'got' => $num_properties, | ||
), | ||
'message' => "must have minimum {$min_properties} properties", | ||
); | ||
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]); | ||
} | ||
} | ||
} |
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.
i noticed @sarahhenkens also updated
HHVM_VERSION
in her PR #52, should we also updateHHVM_NEXT_VERSION
? i wasn't 100% sure if they should always matchThere 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.
hmm I think we should probably update this to
4.121
?