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
155157enum 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