prisma-php 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,293 @@
1
+ ---
2
+ title: Validator
3
+ description: Learn how Prisma PHP validation works so AI agents can sanitize, cast, and validate request data with PP\Validator and the documented Rule workflow instead of inventing custom validation patterns.
4
+ related:
5
+ title: Related docs
6
+ description: Read the official Prisma PHP Validator docs before generating or editing validation code.
7
+ links:
8
+ - /docs/php-validator
9
+ - /docs/fetch-function
10
+ - /docs/route-php
11
+ - /docs/error-handler
12
+ ---
13
+
14
+ Prisma PHP validation should follow the documented `PP\Validator` model, not assumptions copied from Laravel FormRequest, Symfony Validator, Joi, Yup, Zod, or ad hoc request-wrapper patterns.
15
+
16
+ If a task involves sanitization, casting, validation rules, `Rule`, `Validator::withRules(...)`, request normalization, or expected validation failures, AI agents should read the relevant Prisma PHP Validator docs first and keep the implementation aligned with the installed Prisma PHP version.
17
+
18
+ ## AI rule: read the Validator docs first
19
+
20
+ Before generating, editing, or reviewing validation code, use this order:
21
+
22
+ 1. Read `./prisma-php.json`.
23
+ 2. Read the installed local docs in `node_modules/prisma-php/dist/docs`.
24
+ 3. Read this `validator.md` file.
25
+ 4. Inspect the current `index.php`, `layout.php`, exposed function, or `route.php` that receives the data.
26
+ 5. Inspect the payload shape and expected response contract.
27
+ 6. Inspect Prisma PHP core internals only when the docs do not answer the task.
28
+
29
+ Do not assume another framework's validation lifecycle applies directly.
30
+
31
+ ## Read this doc when you need
32
+
33
+ - **sanitization, casting, normalization, `PP\Validator`, `Rule`, or `Validator::withRules(...)`** → `validator.md`
34
+ - **interactive validation with `pp.fetchFunction(...)` inside page routes** → `fetching-data.md` plus `validator.md`
35
+ - **request validation in `route.php`** → `route-handlers.md` plus `validator.md`
36
+ - **expected validation failures and response shaping** → `error-handling.md` plus `validator.md`
37
+ - **credentials, registration, login, or auth form validation** → `authentication.md` plus `validator.md`
38
+ - **rename fields, labels, or other non-file request values in upload flows** → `file-manager.md` plus `validator.md`
39
+
40
+ ## Core validation model AI should follow
41
+
42
+ The official Prisma PHP Validator docs describe `PP\Validator` as the central server-side validation layer.
43
+
44
+ Use this split:
45
+
46
+ - use **PulsePoint** or browser-side logic for local UX only
47
+ - use **`PP\Validator`** on the PHP side for authoritative sanitization, casting, and validation
48
+ - use **`PP\Rule`** when business constraints must be checked declaratively
49
+ - return structured failures for expected validation problems instead of treating them like crashes
50
+
51
+ `Validator` is not the ORM result layer, not a frontend schema runtime, and not a replacement for route or function structure.
52
+
53
+ ## Exact core file location
54
+
55
+ The Prisma PHP core `Validator` class lives here:
56
+
57
+ ```txt
58
+ vendor/tsnc/prisma-php/src/Validator.php
59
+ ```
60
+
61
+ When AI needs to inspect framework internals because the docs do not fully answer a validation task, this is the exact core file to review.
62
+
63
+ ## Identity and string helpers
64
+
65
+ The official docs describe these common helpers:
66
+
67
+ - `string($value, bool $escapeHtml = true): string`
68
+ - `email($value): ?string`
69
+ - `url($value): ?string`
70
+ - `ip($value): ?string`
71
+ - `uuid($value): ?string`
72
+ - `ulid($value): ?string`
73
+ - `cuid($value): ?string`
74
+ - `cuid2($value): ?string`
75
+ - `emojis(string $content): string`
76
+
77
+ Use these helpers when the goal is to normalize or validate a single incoming value before applying additional rules.
78
+
79
+ ## Number and utility helpers
80
+
81
+ The official docs also describe:
82
+
83
+ - `int($value): ?int`
84
+ - `float($value): ?float`
85
+ - `bigInt($value): ?BigInteger`
86
+ - `decimal($value, int $scale = 30): ?BigDecimal`
87
+ - `bytes($value): ?string`
88
+ - `boolean($value): ?bool`
89
+ - `json($value): string`
90
+ - `enum($value, array $allowed): bool`
91
+ - `enumClass($value, string $class)`
92
+ - `date($value, string $format = "Y-m-d"): ?string`
93
+ - `dateTime($value, string $format = "Y-m-d H:i:s"): ?string`
94
+
95
+ Important behavior to remember:
96
+
97
+ - many helper methods return `null` when the value is invalid
98
+ - `enum(...)` checks membership against a simple allowed array
99
+ - `json(...)` accepts JSON strings or safely encodes arrays
100
+ - `boolean(...)` performs smart casting for values such as `"true"`, `"1"`, `"on"`, and `"yes"`
101
+
102
+ ## Rule-based validation
103
+
104
+ The main rule-based entry point is:
105
+
106
+ ```php
107
+ Validator::withRules($value, string|Rule|array $rules, $confirm = null)
108
+ ```
109
+
110
+ Return contract:
111
+
112
+ - `true` when all rules pass
113
+ - `string` when the first rule fails
114
+
115
+ Always use a strict success check:
116
+
117
+ ```php
118
+ if ($result === true) {
119
+ // valid
120
+ }
121
+ ```
122
+
123
+ ## Recommended rule syntax
124
+
125
+ The official docs support three rule styles.
126
+
127
+ ### 1. Rule builder, recommended
128
+
129
+ This is the preferred style for most application code because it is easier to discover, safer to refactor, and clearer in reviews.
130
+
131
+ ```php
132
+ use PP\Rule;
133
+
134
+ $rules = Rule::required()
135
+ ->min(3)
136
+ ->max(50)
137
+ ->startsWith('J');
138
+ ```
139
+
140
+ ### 2. Pipe string syntax
141
+
142
+ Useful for small inline rules or dynamic rules loaded from config.
143
+
144
+ ```php
145
+ $rules = 'required|min:3|max:50';
146
+ ```
147
+
148
+ ### 3. Array syntax
149
+
150
+ Useful when rules need to be appended conditionally.
151
+
152
+ ```php
153
+ $rules = ['required', 'min:8'];
154
+ $rules[] = 'confirmed';
155
+ ```
156
+
157
+ ## Common documented rules
158
+
159
+ The official docs list rules such as:
160
+
161
+ - `required`
162
+ - `min:n`
163
+ - `max:n`
164
+ - `size:n`
165
+ - `between:min,max`
166
+ - `email`
167
+ - `url`
168
+ - `ip`
169
+ - `uuid`
170
+ - `ulid`
171
+ - `cuid`
172
+ - `int`
173
+ - `float`
174
+ - `boolean`
175
+ - `startsWith:value`
176
+ - `endsWith:value`
177
+ - `confirmed`
178
+ - `in:a,b,c`
179
+ - `notIn:a,b,c`
180
+ - `date:format`
181
+ - `dateFormat:format`
182
+ - `before:date`
183
+ - `after:date`
184
+ - `json`
185
+ - `timezone`
186
+ - `regex:pattern`
187
+ - `digits:n`
188
+ - `digitsBetween:min,max`
189
+ - `extensions:jpg,png`
190
+ - `mimes:image/jpeg,image/png`
191
+ - `file`
192
+
193
+ AI should not invent undocumented rule names when these already cover the common Prisma PHP validation workflow.
194
+
195
+ ## Recommended example patterns
196
+
197
+ ### Basic casting and validation
198
+
199
+ ```php
200
+ <?php
201
+
202
+ use PP\Validator;
203
+
204
+ $name = Validator::string($_POST['name'] ?? '');
205
+ $age = Validator::int($_POST['age'] ?? null);
206
+ $email = Validator::email($_POST['email'] ?? '');
207
+
208
+ if ($email === null) {
209
+ return [
210
+ 'success' => false,
211
+ 'errors' => [
212
+ 'email' => 'A valid email address is required.',
213
+ ],
214
+ ];
215
+ }
216
+ ```
217
+
218
+ ### Rule builder, recommended
219
+
220
+ ```php
221
+ <?php
222
+
223
+ use PP\Rule;
224
+ use PP\Validator;
225
+
226
+ $username = Validator::string($_POST['username'] ?? '');
227
+
228
+ $result = Validator::withRules(
229
+ $username,
230
+ Rule::required()->min(3)->max(50)
231
+ );
232
+
233
+ if ($result !== true) {
234
+ return [
235
+ 'success' => false,
236
+ 'errors' => [
237
+ 'username' => $result,
238
+ ],
239
+ ];
240
+ }
241
+ ```
242
+
243
+ ### Confirmation rule
244
+
245
+ ```php
246
+ <?php
247
+
248
+ use PP\Validator;
249
+
250
+ $password = Validator::string($_POST['password'] ?? '', false);
251
+ $confirm = Validator::string($_POST['password_confirmation'] ?? '', false);
252
+
253
+ $result = Validator::withRules($password, ['required', 'min:8', 'confirmed'], $confirm);
254
+ ```
255
+
256
+ ## Operational notes
257
+
258
+ - use `=== true` when checking `Validator::withRules(...)`
259
+ - use the third parameter for `confirmed` comparisons
260
+ - pass full regex patterns with delimiters when using `regex`
261
+ - use array syntax when rules depend on runtime conditions
262
+ - apply validation before database writes, auth state changes, uploads, or other side effects
263
+
264
+ ## Validation boundary
265
+
266
+ Keep these concerns separate:
267
+
268
+ - use **browser-side checks** for immediate feedback only
269
+ - use **`PP\Validator`** for authoritative backend validation
270
+ - use **`error-handling.md`** patterns for expected validation failures
271
+ - use **`fetching-data.md`** or **`route-handlers.md`** based on where the request enters PHP
272
+
273
+ Do not treat browser validation, HTML attributes, or frontend checks as the final source of truth.
274
+
275
+ ## AI rules for validation work
276
+
277
+ - read the official Validator docs before generating validation code
278
+ - do not guess validation APIs from Laravel, Symfony, Joi, Yup, Zod, or other ecosystems
279
+ - prefer `PP\Validator` for sanitization, casting, and helper-based validation
280
+ - prefer the `Rule` builder for most rule-based validation
281
+ - do not assume `Validator::withRules(...)` throws exceptions for normal failures
282
+ - return structured validation results for expected user-input errors
283
+ - inspect `vendor/tsnc/prisma-php/src/Validator.php` only when the docs and current code are not enough
284
+
285
+ ## Suggested file name
286
+
287
+ Use this file name:
288
+
289
+ ```txt
290
+ validator.md
291
+ ```
292
+
293
+ It is clear, consistent with the existing local docs set, and easy for AI agents to discover alongside files such as `authentication.md`, `file-manager.md`, and `route-handlers.md`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-php",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Prisma PHP tooling",
5
5
  "main": "index.js",
6
6
  "scripts": {