-
Notifications
You must be signed in to change notification settings - Fork 2
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
validator: added support for autocasting from a string #8
Conversation
src/Validator.php
Outdated
} | ||
} elseif ($type === 'int') { | ||
if (is_int($node)) { | ||
$isValid = $this->validateNumber($node, $schema, $path, $errors); | ||
break; | ||
} elseif ($autoCast && is_string($node) && preg_match('#^-?[1-9][0-9]*+\z#', $node) === 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wouldn't accept 0
, right? There is also filter_var
... maybe?
src/Validator.php
Outdated
} | ||
} elseif ($type === 'float') { | ||
if (is_float($node)) { | ||
$isValid = $this->validateNumber($node, $schema, $path, $errors); | ||
break; | ||
} elseif ($autoCast && is_string($node) && preg_match('#^-?[1-9][0-9]*+(\.[0-9]+)?\z#', $node) === 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also wouldn't accept 0
and there are also other float notations (1e12, etc.), but I'm not sure if we want to support those. Here is_numeric()
would probably do the job.
$errors = []; | ||
$stack = [[$this->schema, $data, '/']]; | ||
while (list ($schema, $node, $path) = array_pop($stack)) { | ||
$stack = [[$this->schema, & $outData, '/']]; |
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.
Why so many &
? It's kind of hard to read.
src/Validator.php
Outdated
@@ -29,11 +29,16 @@ public function __construct(array $schema, bool $failFast = false, callable $ref | |||
} | |||
|
|||
|
|||
public function validate($data): Result | |||
public function validate($data, bool $autoCast = false): Result |
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.
btw: this is often called coercion, e.g. https://github.com/justinrainbow/json-schema#type-coercion
Guys, thanks for the review. |
No description provided.