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

validator: added support for autocasting from a string #8

Merged
merged 1 commit into from
Apr 18, 2017

Conversation

hrach
Copy link
Contributor

@hrach hrach commented Apr 18, 2017

No description provided.

@hrach hrach requested a review from JanJakes April 18, 2017 15:52
}
} 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) {

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?

}
} 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) {

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, '/']];

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.

@@ -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
Copy link

@JanTvrdik JanTvrdik Apr 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hrach
Copy link
Contributor Author

hrach commented Apr 18, 2017

Guys, thanks for the review.

@hrach hrach merged commit 8179c35 into master Apr 18, 2017
@hrach hrach deleted the auto_casting branch April 18, 2017 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants