You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: fern/products/fern-def/pages/types.mdx
+291-1Lines changed: 291 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -278,4 +278,294 @@ interface Person {
278
278
age: number;
279
279
}
280
280
```
281
-
</CodeBlock>
281
+
</CodeBlock>
282
+
### Validation
283
+
284
+
You can add validation constraints to your types to ensure data integrity and provide better error messages in generated SDKs. You can add validation rules to both type aliases and type references.
285
+
286
+
### String validation
287
+
288
+
String types support several validation constraints:
289
+
290
+
<ParamField path="minLength" type="integer">
291
+
Minimum number of characters required
292
+
</ParamField>
293
+
294
+
<ParamField path="maxLength" type="integer">
295
+
Maximum number of characters allowed
296
+
</ParamField>
297
+
298
+
<ParamField path="pattern" type="string">
299
+
Regular expression pattern that the string must match
300
+
</ParamField>
301
+
302
+
<ParamField path="format" type="string">
303
+
String format specification (e.g., "email", "uri", "date-time")
304
+
</ParamField>
305
+
306
+
#### Example: String validation on type alias
307
+
308
+
```yaml
309
+
types:
310
+
Email:
311
+
type: string
312
+
validation:
313
+
format: email
314
+
maxLength: 254
315
+
316
+
Username:
317
+
type: string
318
+
validation:
319
+
minLength: 3
320
+
maxLength: 20
321
+
pattern: "^[a-zA-Z0-9_]+$"
322
+
323
+
PhoneNumber:
324
+
type: string
325
+
validation:
326
+
pattern: "^\\+?[1-9]\\d{1,14}$"
327
+
```
328
+
329
+
#### Example: String validation on object properties
330
+
331
+
```yaml
332
+
types:
333
+
User:
334
+
properties:
335
+
email:
336
+
type: string
337
+
validation:
338
+
format: email
339
+
maxLength: 254
340
+
username:
341
+
type: string
342
+
validation:
343
+
minLength: 3
344
+
maxLength: 20
345
+
pattern: "^[a-zA-Z0-9_]+$"
346
+
bio:
347
+
type: optional<string>
348
+
validation:
349
+
maxLength: 500
350
+
```
351
+
352
+
### Number validation
353
+
354
+
Number types (including `integer`, `long`, and `double`) support validation constraints:
355
+
356
+
<ParamField path="min" type="number">
357
+
Minimum value (inclusive by default)
358
+
</ParamField>
359
+
360
+
<ParamField path="max" type="number">
361
+
Maximum value (inclusive by default)
362
+
</ParamField>
363
+
364
+
<ParamField path="exclusiveMin" type="boolean">
365
+
When true, the minimum value is exclusive (value must be greater than min)
366
+
</ParamField>
367
+
368
+
<ParamField path="exclusiveMax" type="boolean">
369
+
When true, the maximum value is exclusive (value must be less than max)
370
+
</ParamField>
371
+
372
+
<ParamField path="multipleOf" type="number">
373
+
Value must be a multiple of this number
374
+
</ParamField>
375
+
376
+
#### Example: Number validation on type alias
377
+
378
+
```yaml
379
+
types:
380
+
Age:
381
+
type: integer
382
+
validation:
383
+
min: 0
384
+
max: 150
385
+
```
386
+
Price:
387
+
type: double
388
+
validation:
389
+
min: 0
390
+
exclusiveMin: true # Price must be greater than 0
391
+
multipleOf: 0.01# Price must be in cents
392
+
393
+
Percentage:
394
+
type: double
395
+
validation:
396
+
min: 0
397
+
max: 100
398
+
```
399
+
400
+
#### Example: Number validation on object properties
401
+
402
+
```yaml
403
+
types:
404
+
Product:
405
+
properties:
406
+
name: string
407
+
price:
408
+
type: double
409
+
validation:
410
+
min: 0
411
+
exclusiveMin: true
412
+
multipleOf: 0.01
413
+
quantity:
414
+
type: integer
415
+
validation:
416
+
min: 1
417
+
max: 1000
418
+
discount:
419
+
type: optional<double>
420
+
validation:
421
+
min: 0
422
+
max: 100
423
+
```
424
+
425
+
### Validation in generated SDKs
426
+
427
+
When you add validation constraints to your Fern Definition, they are automatically enforced in generated SDKs:
428
+
429
+
<CodeBlocks>
430
+
```python title="Python SDK"
431
+
from your_api import YourApiClient
432
+
from your_api.errors import ValidationError
433
+
434
+
client = YourApiClient(api_key="your-api-key")
435
+
436
+
try:
437
+
# This will raise a ValidationError
438
+
user = client.users.create(
439
+
email="invalid-email", # Fails email format validation
440
+
username="ab", # Fails minLength validation
441
+
age=-5 # Fails min value validation
442
+
)
443
+
except ValidationError as e:
444
+
print(f"Validation failed: {e}")
445
+
```
446
+
447
+
```typescript title="TypeScript SDK"
448
+
import { YourApiClient, ValidationError } from "your-api-sdk";
449
+
450
+
const client = new YourApiClient({
451
+
apiKey: "your-api-key"
452
+
});
453
+
454
+
try {
455
+
// This will throw a ValidationError
456
+
const user = await client.users.create({
457
+
email: "invalid-email", // Fails email format validation
0 commit comments