Skip to content

Commit

Permalink
schema only passes non-null values to prop validation. fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
shrink0r committed Oct 22, 2016
1 parent 26bb1b7 commit 06b032a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
19 changes: 10 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
language: php

php:
- 5.6
- 7.0
- nightly

matrix:
allow_failures:
- php: nightly

before_script:
- composer self-update
- composer install

script:
- composer test

- composer code-sniffer
after_script:
- php vendor/bin/coveralls -v
notifications:
irc:
channels:
- "chat.freenode.net#honeybee"
on_success: always
on_failure: always
use_notice: false
skip_join: false

6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"name": "shrink0r/php-schema",
"description": "Lib for building and validating array structures in php.",
"keywords": [ "schema", "validation" ],
"support": {
"issues": "https://github.com/shrink0r/php-schema/issues",
"wiki": "https://github.com/shrink0r/php-schema/wiki",
"irc": "irc://irc.freenode.org/honeybee",
"source": "https://github.com/shrink0r/php-schema"
},
"license": "MIT",
"require": {
"php": ">=5.6.1"
Expand Down
2 changes: 1 addition & 1 deletion src/Ok.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function unit($data = null)
{
$class = static::class;

return new $class($data ?: []);
return new $class($data);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ protected function validateMappedValues(array $data)
foreach (array_diff_key($this->properties, [ ':any_name:' => 1 ]) as $key => $property) {
$result = $this->selectValue($property, $data);
if ($result instanceof Ok) {
$result = $property->validate($result->unwrap());
$value = $result->unwrap();
if ($value === null) {
continue;
}
$result = $property->validate($value);
}
if ($result instanceof Error) {
$errors[$key] = $result->unwrap();
Expand All @@ -175,7 +179,7 @@ protected function selectValue(PropertyInterface $property, array $data)
{
$errors = [];
$key = $property->getName();
$value = isset($data[$key]) ? $data[$key] : null;
$value = array_key_exists($key, $data) ? $data[$key] : null;

if ($value === null && $property->isRequired()) {
if (!array_key_exists($key, $data)) {
Expand All @@ -201,6 +205,12 @@ protected function validateAnyValues(array $data)

foreach (array_diff_key($data, $this->properties) as $key => $value) {
if (isset($this->properties[':any_name:'])) {
if ($value === null) {
if ($this->properties[':any_name:']->isRequired()) {
$errors[$key] = [ Error::MISSING_VALUE ];
}
continue;
}
$result = $this->properties[':any_name:']->validate($value);
if ($result instanceof Error) {
$errors[$key] = $result->unwrap();
Expand Down

0 comments on commit 06b032a

Please sign in to comment.