public function testMapStrict() { # Validation functions $vfs = ['a' => function ($in) { return $in . 'a'; }, 'b' => function ($in) { return $in . 'b'; }]; $_ = map($vfs); $this->assertEquals(['a' => 'aa', 'b' => 'bb', 'c' => 'c'], $_(['a' => 'a', 'b' => 'b', 'c' => 'c'])); $_ = map_($vfs); $this->assertEquals(['a' => 'aa', 'b' => 'bb', 'c' => 'c'], $_(['a' => 'a', 'b' => 'b', 'c' => 'c'])); try { $_ = mapS($vfs); $this->assertEquals(['a' => 'aa', 'b' => 'bb', 'c' => 'c'], $_(['a' => 'a', 'b' => 'b', 'c' => 'c'])); } catch (ValidationException $e) { $this->assertEquals('the following fields lack validation rules: c', $e->getMessage()); } try { $_ = map_S($vfs); $this->assertEquals(['a' => 'aa', 'b' => 'bb', 'c' => 'c'], $_(['a' => 'a', 'b' => 'b', 'c' => 'c'])); } catch (ValidationException $e) { $this->assertEquals('the following fields lack validation rules: c', $e->getMessage()); } }
/** * Combine a map of validation functions into a map validator * * Fields with no rules count as validation errors. * Throw exception immediately at the first problem. */ function map_S($callables) { return seq_([function ($data) use($callables) { $diff = array_diff(array_keys($data), array_keys($callables)); if (count($diff) > 0) { throw new IncompleteValidationException("the following fields lack validation rules: " . implode(',', $diff)); } return $data; }, map_($callables)]); }