Skip to content

Commit 09256a3

Browse files
committed
field-to-schema
1 parent 2c66740 commit 09256a3

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Heavily inspired by the well-known TypeScript library [zod](https://github.com/c
3535
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-parsing][1].
3636

3737
```sh
38-
composer require chubbyphp/chubbyphp-parsing "^1.3"
38+
composer require chubbyphp/chubbyphp-parsing "^1.4"
3939
```
4040

4141
## Usage
@@ -267,6 +267,9 @@ $p = new Parser();
267267

268268
$schema = $p->object(['name' => $p->string()]);
269269

270+
// create a new schema based on a existing once
271+
$schema2 = $p->object([...$schema->getFieldToSchema(), 'value' => $p->string()]);
272+
270273
// stdClass object
271274
$data = $schema->parse(['name' => 'example']);
272275

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"extra": {
5252
"branch-alias": {
53-
"dev-master": "1.3-dev"
53+
"dev-master": "1.4-dev"
5454
}
5555
},
5656
"scripts": {

src/Schema/ObjectSchema.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class ObjectSchema extends AbstractSchema implements ObjectSchemaInterface
1818
/**
1919
* @var array<string, SchemaInterface>
2020
*/
21-
private array $fieldNameToSchema;
21+
private array $fieldToSchema;
2222

2323
/**
2424
* @var null|array<string>
@@ -31,16 +31,16 @@ final class ObjectSchema extends AbstractSchema implements ObjectSchemaInterface
3131
private ?array $optional = null;
3232

3333
/**
34-
* @param array<string, SchemaInterface> $fieldNameToSchema
34+
* @param array<string, SchemaInterface> $fieldToSchema
3535
* @param class-string $classname
3636
*/
37-
public function __construct(array $fieldNameToSchema, private string $classname = \stdClass::class)
37+
public function __construct(array $fieldToSchema, private string $classname = \stdClass::class)
3838
{
39-
foreach ($fieldNameToSchema as $fieldName => $fieldSchema) {
39+
foreach ($fieldToSchema as $fieldName => $fieldSchema) {
4040
if (!\is_string($fieldName)) {
4141
throw new \InvalidArgumentException(
4242
\sprintf(
43-
'Argument #1 name #%s ($fieldNameToSchema) must be of type string, %s given',
43+
'Argument #1 name #%s ($fieldToSchema) must be of type string, %s given',
4444
$fieldName,
4545
$this->getDataType($fieldName)
4646
)
@@ -50,7 +50,7 @@ public function __construct(array $fieldNameToSchema, private string $classname
5050
if (!$fieldSchema instanceof SchemaInterface) {
5151
throw new \InvalidArgumentException(
5252
\sprintf(
53-
'Argument #1 value of #%s ($fieldNameToSchema) must be of type %s, %s given',
53+
'Argument #1 value of #%s ($fieldToSchema) must be of type %s, %s given',
5454
$fieldName,
5555
SchemaInterface::class,
5656
$this->getDataType($fieldSchema)
@@ -59,7 +59,7 @@ public function __construct(array $fieldNameToSchema, private string $classname
5959
}
6060
}
6161

62-
$this->fieldNameToSchema = $fieldNameToSchema;
62+
$this->fieldToSchema = $fieldToSchema;
6363
}
6464

6565
public function parse(mixed $input): mixed
@@ -111,9 +111,17 @@ public function parse(mixed $input): mixed
111111
}
112112
}
113113

114-
public function getFieldSchema(string $fieldName): ?SchemaInterface
114+
/**
115+
* @return array<string, SchemaInterface>
116+
*/
117+
public function getFieldToSchema(): array
118+
{
119+
return $this->fieldToSchema;
120+
}
121+
122+
public function getFieldSchema(string $field): ?SchemaInterface
115123
{
116-
return $this->fieldNameToSchema[$fieldName] ?? null;
124+
return $this->fieldToSchema[$field] ?? null;
117125
}
118126

119127
/**
@@ -150,7 +158,7 @@ private function unknownFields(array $input, ParserErrorException $childrenParse
150158
}
151159

152160
foreach (array_keys($input) as $fieldName) {
153-
if (!\in_array($fieldName, $this->strict, true) && !isset($this->fieldNameToSchema[$fieldName])) {
161+
if (!\in_array($fieldName, $this->strict, true) && !isset($this->fieldToSchema[$fieldName])) {
154162
$childrenParserErrorException->addError(new Error(
155163
self::ERROR_UNKNOWN_FIELD_CODE,
156164
self::ERROR_UNKNOWN_FIELD_TEMPLATE,
@@ -165,7 +173,7 @@ private function unknownFields(array $input, ParserErrorException $childrenParse
165173
*/
166174
private function parseFields(array $input, object $object, ParserErrorException $childrenParserErrorException): void
167175
{
168-
foreach ($this->fieldNameToSchema as $fieldName => $fieldSchema) {
176+
foreach ($this->fieldToSchema as $fieldName => $fieldSchema) {
169177
try {
170178
if (
171179
!\array_key_exists($fieldName, $input)

tests/Unit/Schema/ObjectSchemaTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Chubbyphp\Tests\Parsing\Unit\Schema;
66

77
use Chubbyphp\Parsing\ParserErrorException;
8+
use Chubbyphp\Parsing\Schema\BoolSchema;
89
use Chubbyphp\Parsing\Schema\FloatSchema;
910
use Chubbyphp\Parsing\Schema\IntSchema;
1011
use Chubbyphp\Parsing\Schema\ObjectSchema;
@@ -56,7 +57,7 @@ public function testConstructWithoutFieldName(): void
5657
throw new \Exception('code should not be reached');
5758
} catch (\InvalidArgumentException $invalidArgumentException) {
5859
self::assertSame(
59-
'Argument #1 name #0 ($fieldNameToSchema) must be of type string, integer given',
60+
'Argument #1 name #0 ($fieldToSchema) must be of type string, integer given',
6061
$invalidArgumentException->getMessage()
6162
);
6263
}
@@ -70,7 +71,7 @@ public function testConstructWithoutFieldSchema(): void
7071
throw new \Exception('code should not be reached');
7172
} catch (\InvalidArgumentException $invalidArgumentException) {
7273
self::assertSame(
73-
'Argument #1 value of #field2 ($fieldNameToSchema) must be of type Chubbyphp\Parsing\Schema\SchemaInterface, string given',
74+
'Argument #1 value of #field2 ($fieldToSchema) must be of type Chubbyphp\Parsing\Schema\SchemaInterface, string given',
7475
$invalidArgumentException->getMessage()
7576
);
7677
}
@@ -370,6 +371,21 @@ public function testSafeParseFailed(): void
370371
], $this->errorsToSimpleArray($schema->safeParse(null)->exception->getErrors()));
371372
}
372373

374+
public function testGetFieldToSchema(): void
375+
{
376+
$fieldToSchema = ['field1' => new StringSchema(), 'field2' => new IntSchema()];
377+
378+
$schema = new ObjectSchema($fieldToSchema);
379+
380+
self::assertSame($fieldToSchema, $schema->getFieldToSchema());
381+
382+
$fieldToSchema2 = [...$schema->getFieldToSchema(), 'field3' => new BoolSchema()];
383+
384+
$schema2 = new ObjectSchema($fieldToSchema2);
385+
386+
self::assertSame($fieldToSchema2, $schema2->getFieldToSchema());
387+
}
388+
373389
public function testGetFieldSchemaSuccess(): void
374390
{
375391
$field2Schema = new IntSchema();

0 commit comments

Comments
 (0)