Skip to content

Commit c199343

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

36 files changed

Lines changed: 2311 additions & 588 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: 4 additions & 4 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,9 +267,10 @@ 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` |
273+
| const | `const.` | `const.type` |
273274
| array | `array.` | `array.type`, `array.minLength` |
274275
| assoc | `assoc.` | `assoc.type`, `assoc.unknownField` |
275276
| object | `object.` | `object.type`, `object.unknownField` |
@@ -278,7 +279,6 @@ Each schema type uses a consistent error code prefix:
278279
| record | `record.` | `record.type` |
279280
| union | `union.` | `union.type` |
280281
| discriminatedUnion | `discriminatedUnion.` | `discriminatedUnion.type`, `discriminatedUnion.discriminator` |
281-
| literal | `literal.` | `literal.type` |
282282
| backedEnum | `backedEnum.` | `backedEnum.type` |
283283
| lazy | (delegates to inner schema) | |
284284
| respectValidation | `respectValidation.` | `respectValidation.assert` |

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([

doc/Schema/BackedEnumSchema.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ enum Rank: int
170170
{
171171
case Two = 2;
172172
case Three = 3;
173-
// ...
173+
// ...
174174
case Jack = 11;
175175
case Queen = 12;
176176
case King = 13;
@@ -182,7 +182,7 @@ $cardSchema = $p->object([
182182
'rank' => $p->backedEnum(Rank::class),
183183
]);
184184

185-
$cardSchema->parse(['suit' => 'H', 'rank' => 14]);
185+
$cardSchema->parse(['suit' => 'H', 'rank' => 14]);
186186
// Returns object with Suit::Hearts and Rank::Ace
187187
```
188188

@@ -204,14 +204,14 @@ $tagsSchema->parse(['featured', 'new', 'sale']);
204204
// Returns: [Tag::Featured, Tag::New, Tag::Sale]
205205
```
206206

207-
## BackedEnum vs Literal Union
207+
## BackedEnum vs Const Union
208208

209209
Use **BackedEnumSchema** when:
210210
- You already have a PHP enum defined
211211
- You want type safety with enum instances
212212
- You need enum methods/functionality
213213

214-
Use **Literal union** when:
214+
Use **Const union** when:
215215
- Values are ad-hoc or temporary
216216
- You don't need enum type safety
217217
- You're matching simple string/int values
@@ -222,8 +222,8 @@ enum Status: string { case Active = 'active'; case Inactive = 'inactive'; }
222222
$schema = $p->backedEnum(Status::class);
223223
// Returns Status enum instances
224224

225-
// Literal union: simple string values
226-
$schema = $p->union([$p->literal('active'), $p->literal('inactive')]);
225+
// Const union: simple string values
226+
$schema = $p->union([$p->const('active'), $p->const('inactive')]);
227227
// Returns strings
228228
```
229229

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.

0 commit comments

Comments
 (0)