Skip to content

Commit 956c5f4

Browse files
committed
align function naming with json schema
1 parent 6517944 commit 956c5f4

30 files changed

Lines changed: 2256 additions & 533 deletions

README.md

Lines changed: 3 additions & 3 deletions
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 "^2.4"
38+
composer require chubbyphp/chubbyphp-parsing "^2.5"
3939
```
4040

4141
## Quick Start
@@ -49,7 +49,7 @@ $p = new Parser();
4949
$userSchema = $p->object([
5050
'name' => $p->string()->minLength(1)->maxLength(100),
5151
'email' => $p->string()->email(),
52-
'age' => $p->int()->gte(0)->lte(150),
52+
'age' => $p->int()->minimum(0)->maximum(150),
5353
]);
5454

5555
// Parse and validate data
@@ -93,7 +93,7 @@ $user = $userSchema->parse([
9393

9494
| Schema | Description | Documentation |
9595
|--------|-------------|---------------|
96-
| `literal()` | Exact value matching | [LiteralSchema](doc/Schema/LiteralSchema.md) |
96+
| `const()` | Exact value matching | [ConstSchema](doc/Schema/ConstSchema.md) |
9797
| `backedEnum()` | PHP BackedEnum validation | [BackedEnumSchema](doc/Schema/BackedEnumSchema.md) |
9898
| `lazy()` | Recursive/self-referencing schemas | [LazySchema](doc/Schema/LazySchema.md) |
9999
| `respectValidation()` | Integration with Respect/Validation | [RespectValidationSchema](doc/Schema/RespectValidationSchema.md) |

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": "2.4-dev"
53+
"dev-master": "2.5-dev"
5454
}
5555
},
5656
"scripts": {

doc/ErrorHandling.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ $p = new Parser();
226226
$requestSchema = $p->object([
227227
'name' => $p->string()->trim()->minLength(1)->maxLength(100),
228228
'email' => $p->string()->trim()->toLowerCase()->email(),
229-
'age' => $p->int()->gte(0)->lte(150),
229+
'age' => $p->int()->minimum(0)->maximum(150),
230230
])->strict();
231231

232232
function handleRequest(array $input): array
@@ -267,8 +267,8 @@ Each schema type uses a consistent error code prefix:
267267
| Schema | Prefix | Example Codes |
268268
|--------|--------|---------------|
269269
| string | `string.` | `string.type`, `string.minLength`, `string.email` |
270-
| int | `int.` | `int.type`, `int.gt`, `int.positive` |
271-
| float | `float.` | `float.type`, `float.gte`, `float.negative` |
270+
| int | `int.` | `int.type`, `int.exclusiveMinimum`, `int.positive` |
271+
| float | `float.` | `float.type`, `float.minimum`, `float.negative` |
272272
| bool | `bool.` | `bool.type` |
273273
| array | `array.` | `array.type`, `array.minLength` |
274274
| assoc | `assoc.` | `assoc.type`, `assoc.unknownField` |

doc/Schema/ArraySchema.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ $data = $schema->parse([1, 2, 3, 4, 5]); // Returns: [1, 2, 3, 4, 5]
1919
### Length Constraints
2020

2121
```php
22-
$schema->length(5); // Exact length of 5 items
23-
$schema->minLength(1); // At least 1 item
24-
$schema->maxLength(10); // At most 10 items
22+
$schema->exactItems(5); // Exact count of 5 items
23+
$schema->minItems(1); // At least 1 item
24+
$schema->maxItems(10); // At most 10 items
2525
```
2626

2727
### Content Check
2828

2929
```php
30-
$schema->includes(5); // Array must contain value 5
30+
$schema->contains(5); // Array must contain value 5
3131
```
3232

3333
## Transformations
@@ -89,14 +89,14 @@ $sumSchema->parse([1, 2, 3, 4, 5]); // Returns: 15
8989
### Non-Empty Array
9090

9191
```php
92-
$nonEmptySchema = $p->array($p->string())->minLength(1);
92+
$nonEmptySchema = $p->array($p->string())->minItems(1);
9393
```
9494

9595
### Unique Tags with Limit
9696

9797
```php
9898
$tagsSchema = $p->array($p->string()->trim()->minLength(1))
99-
->maxLength(10)
99+
->maxItems(10)
100100
->map(static fn (string $tag) => strtolower($tag));
101101
```
102102

@@ -147,9 +147,9 @@ $matrixSchema->parse([
147147
| Code | Description |
148148
|------|-------------|
149149
| `array.type` | Value is not an array |
150-
| `array.length` | Array length doesn't match exact length |
151-
| `array.minLength` | Array has fewer items than minimum |
152-
| `array.maxLength` | Array has more items than maximum |
153-
| `array.includes` | Array doesn't contain required value |
150+
| `array.exactItems` | Array items count doesn't match exact count |
151+
| `array.minItems` | Array has fewer items than minimum |
152+
| `array.maxItems` | Array has more items than maximum |
153+
| `array.contains` | Array doesn't contain required value |
154154

155155
Item-level errors will include the array index in the error path (e.g., `items.0`, `items.1`).

doc/Schema/AssocSchema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ echo $config['database']['host']; // 'localhost'
158158
$addressSchema = $p->assoc([
159159
'street' => $p->string(),
160160
'city' => $p->string(),
161-
'zipCode' => $p->string()->regexp('/^\d{5}$/'),
161+
'zipCode' => $p->string()->pattern('/^\d{5}$/'),
162162
]);
163163

164164
$personSchema = $p->assoc([
Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# LiteralSchema
1+
# ConstSchema
22

3-
The `LiteralSchema` validates that a value matches an exact literal value. It supports string, integer, float, and boolean literals.
3+
> **Deprecated**: Use [ConstSchema](ConstSchema.md) instead. The `ConstSchema` and `const()` method are deprecated and will be removed in a future version.
4+
5+
The `ConstSchema` validates that a value matches an exact const value. It supports string, integer, float, and boolean consts.
46

57
## Basic Usage
68

@@ -9,27 +11,27 @@ use Chubbyphp\Parsing\Parser;
911

1012
$p = new Parser();
1113

12-
// String literal
13-
$schema = $p->literal('email');
14+
// String const
15+
$schema = $p->const('email');
1416
$data = $schema->parse('email'); // Returns: 'email'
1517

16-
// Integer literal
17-
$schema = $p->literal(42);
18+
// Integer const
19+
$schema = $p->const(42);
1820
$data = $schema->parse(42); // Returns: 42
1921

20-
// Boolean literal
21-
$schema = $p->literal(true);
22+
// Boolean const
23+
$schema = $p->const(true);
2224
$data = $schema->parse(true); // Returns: true
2325
```
2426

2527
## Supported Types
2628

2729
```php
28-
$p->literal('string'); // String literal
29-
$p->literal(42); // Integer literal
30-
$p->literal(3.14); // Float literal
31-
$p->literal(true); // Boolean literal
32-
$p->literal(false); // Boolean literal
30+
$p->const('string'); // String const
31+
$p->const(42); // Integer const
32+
$p->const(3.14); // Float const
33+
$p->const(true); // Boolean const
34+
$p->const(false); // Boolean const
3335
```
3436

3537
## Common Patterns
@@ -41,11 +43,11 @@ Used with `DiscriminatedUnionSchema` to identify object types:
4143
```php
4244
$contactSchema = $p->discriminatedUnion([
4345
$p->object([
44-
'type' => $p->literal('email'),
46+
'type' => $p->const('email'),
4547
'address' => $p->string()->email(),
4648
]),
4749
$p->object([
48-
'type' => $p->literal('phone'),
50+
'type' => $p->const('phone'),
4951
'number' => $p->string(),
5052
]),
5153
], 'type');
@@ -55,9 +57,9 @@ $contactSchema = $p->discriminatedUnion([
5557

5658
```php
5759
$statusSchema = $p->union([
58-
$p->literal('pending'),
59-
$p->literal('approved'),
60-
$p->literal('rejected'),
60+
$p->const('pending'),
61+
$p->const('approved'),
62+
$p->const('rejected'),
6163
]);
6264

6365
$statusSchema->parse('approved'); // Returns: 'approved'
@@ -67,16 +69,16 @@ $statusSchema->parse('invalid'); // Throws error
6769
### Magic Numbers
6870

6971
```php
70-
$httpOkSchema = $p->literal(200);
71-
$httpNotFoundSchema = $p->literal(404);
72+
$httpOkSchema = $p->const(200);
73+
$httpNotFoundSchema = $p->const(404);
7274

7375
$responseSchema = $p->object([
7476
'status' => $p->union([
75-
$p->literal(200),
76-
$p->literal(201),
77-
$p->literal(400),
78-
$p->literal(404),
79-
$p->literal(500),
77+
$p->const(200),
78+
$p->const(201),
79+
$p->const(400),
80+
$p->const(404),
81+
$p->const(500),
8082
]),
8183
'body' => $p->string(),
8284
]);
@@ -85,12 +87,12 @@ $responseSchema = $p->object([
8587
### Boolean Flags
8688

8789
```php
88-
$enabledSchema = $p->literal(true);
89-
$disabledSchema = $p->literal(false);
90+
$enabledSchema = $p->const(true);
91+
$disabledSchema = $p->const(false);
9092

9193
// Only accept explicitly true
9294
$mustBeEnabled = $p->object([
93-
'feature' => $p->literal(true),
95+
'feature' => $p->const(true),
9496
]);
9597
```
9698

@@ -99,8 +101,8 @@ $mustBeEnabled = $p->object([
99101
```php
100102
$sortSchema = $p->record(
101103
$p->union([
102-
$p->literal('asc'),
103-
$p->literal('desc'),
104+
$p->const('asc'),
105+
$p->const('desc'),
104106
])
105107
);
106108

@@ -114,29 +116,29 @@ $sortSchema->parse([
114116

115117
```php
116118
$requestSchema = $p->object([
117-
'version' => $p->literal('2.0'),
119+
'version' => $p->const('2.0'),
118120
'method' => $p->string(),
119121
'params' => $p->record($p->string()),
120122
]);
121123
```
122124

123-
### Null Literal
125+
### Null Const
124126

125-
While `nullable()` is preferred for optional null values, you can use literal for explicit null:
127+
While `nullable()` is preferred for optional null values, you can use const for explicit null:
126128

127129
```php
128-
$nullSchema = $p->literal(null);
130+
$nullSchema = $p->const(null);
129131

130132
// Useful in unions where null has specific meaning
131133
$valueOrNotSet = $p->union([
132134
$p->string(),
133-
$p->literal(null),
135+
$p->const(null),
134136
]);
135137
```
136138

137-
## Literal vs Enum
139+
## Const vs Enum
138140

139-
Use **LiteralSchema** when:
141+
Use **ConstSchema** when:
140142
- You have a single specific value
141143
- You're building discriminators for unions
142144
- You need to match magic numbers or specific strings
@@ -147,9 +149,9 @@ Use **BackedEnumSchema** when:
147149
- The values represent a closed set of options
148150

149151
```php
150-
// Literal: single value or ad-hoc unions
151-
$type = $p->literal('user');
152-
$direction = $p->union([$p->literal('asc'), $p->literal('desc')]);
152+
// Const: single value or ad-hoc unions
153+
$type = $p->const('user');
154+
$direction = $p->union([$p->const('asc'), $p->const('desc')]);
153155

154156
// BackedEnum: related values with type safety
155157
enum Direction: string {
@@ -163,6 +165,6 @@ $direction = $p->backedEnum(Direction::class);
163165

164166
| Code | Description |
165167
|------|-------------|
166-
| `literal.type` | Value doesn't match the expected literal |
168+
| `const.type` | Value doesn't match the expected const |
167169

168-
The error will include the expected literal value and the actual value received.
170+
The error will include the expected const value and the actual value received.

doc/Schema/DiscriminatedUnionSchema.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ $contactSchema = $p->discriminatedUnion([
6363
]),
6464
$p->object([
6565
'_type' => $p->literal('phone'),
66-
'number' => $p->string()->regexp('/^\+\d{10,15}$/'),
66+
'number' => $p->string()->pattern('/^\+\d{10,15}$/'),
6767
'country' => $p->string()->length(2),
6868
]),
6969
$p->object([
@@ -124,9 +124,9 @@ $eventSchema = $p->discriminatedUnion([
124124
$paymentSchema = $p->discriminatedUnion([
125125
$p->object([
126126
'method' => $p->literal('credit_card'),
127-
'cardNumber' => $p->string()->regexp('/^\d{16}$/'),
128-
'expiry' => $p->string()->regexp('/^\d{2}\/\d{2}$/'),
129-
'cvv' => $p->string()->regexp('/^\d{3,4}$/'),
127+
'cardNumber' => $p->string()->pattern('/^\d{16}$/'),
128+
'expiry' => $p->string()->pattern('/^\d{2}\/\d{2}$/'),
129+
'cvv' => $p->string()->pattern('/^\d{3,4}$/'),
130130
]),
131131
$p->object([
132132
'method' => $p->literal('bank_transfer'),

doc/Schema/FloatSchema.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ $data = $schema->parse(4.2); // Returns: 4.2
1919
### Comparison Constraints
2020

2121
```php
22-
$schema->gt(5.0); // Greater than 5.0
23-
$schema->gte(5.0); // Greater than or equal to 5.0
24-
$schema->lt(10.0); // Less than 10.0
25-
$schema->lte(10.0); // Less than or equal to 10.0
22+
$schema->minimum(5.0); // Greater than or equal to 5.0
23+
$schema->exclusiveMinimum(5.0); // Greater than 5.0
24+
$schema->exclusiveMaximum(10.0); // Less than 10.0
25+
$schema->maximum(10.0); // Less than or equal to 10.0
2626
```
2727

2828
### Sign Constraints
@@ -56,17 +56,18 @@ $priceSchema->parse(-5.0); // Throws: nonNegative validation error
5656
### Percentage
5757

5858
```php
59-
$percentageSchema = $p->float()->gte(0.0)->lte(100.0);
59+
$percentageSchema = $p->float()->minimum(0.0)->maximum(100.0);
6060

6161
$percentageSchema->parse(75.5); // Returns: 75.5
62-
$percentageSchema->parse(150.0); // Throws: lte validation error
62+
$percentageSchema->parse(-1.0); // Throws: minimum validation error
63+
$percentageSchema->parse(150.0); // Throws: maximum validation error
6364
```
6465

6566
### Coordinates
6667

6768
```php
68-
$latitudeSchema = $p->float()->gte(-90.0)->lte(90.0);
69-
$longitudeSchema = $p->float()->gte(-180.0)->lte(180.0);
69+
$latitudeSchema = $p->float()->minimum(-90.0)->maximum(90.0);
70+
$longitudeSchema = $p->float()->minimum(-180.0)->maximum(180.0);
7071

7172
$coordinatesSchema = $p->object([
7273
'lat' => $latitudeSchema,
@@ -81,8 +82,8 @@ $coordinatesSchema->parse(['lat' => 47.1, 'lng' => 8.2]);
8182
| Code | Description |
8283
|------|-------------|
8384
| `float.type` | Value is not a float |
84-
| `float.gt` | Value is not greater than threshold (used by `gt()` and `positive()`) |
85-
| `float.gte` | Value is not greater than or equal to threshold (used by `gte()` and `nonNegative()`) |
86-
| `float.lt` | Value is not less than threshold (used by `lt()` and `negative()`) |
87-
| `float.lte` | Value is not less than or equal to threshold (used by `lte()` and `nonPositive()`) |
85+
| `float.minimum` | Value is not greater than or equal to threshold (used by `minimum` and `nonNegative()`) |
86+
| `float.exclusiveMinimum` | Value is not greater than threshold (used by `exclusiveMinimum()` and `positive()`) |
87+
| `float.exclusiveMaximum` | Value is not less than threshold (used by `exclusiveMaximum()` and `negative()`) |
88+
| `float.maximum` | Value is not less than or equal to threshold (used by `maximum()` and `nonPositive()`) |
8889
| `float.int` | Cannot convert float to int without precision loss (for `toInt()`) |

0 commit comments

Comments
 (0)