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
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.
283
+
### Validating types
285
284
286
-
### String validation
285
+
You can add validation constraints to your types (both aliases and references) to ensure data integrity and provide better error messages in generated SDKs:
287
286
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
287
+
```yaml {4-6}
309
288
types:
310
-
Email:
289
+
Sentence:
311
290
type: string
312
291
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}$"
292
+
minLength: 4
293
+
maxLength: 256
327
294
```
328
295
329
-
#### Example: String validation on object properties
296
+
<AccordionGroup>
297
+
<Accordion title="String validation">
330
298
331
-
```yaml
299
+
String types support several validation constraints.
300
+
301
+
```yaml {4-6, 11-13, 16-19}
332
302
types:
303
+
Word:
304
+
type: string
305
+
validation:
306
+
minLength: 2
307
+
maxLength: 26
333
308
User:
334
309
properties:
335
310
email:
@@ -343,64 +318,37 @@ types:
343
318
minLength: 3
344
319
maxLength: 20
345
320
pattern: "^[a-zA-Z0-9_]+$"
346
-
bio:
347
-
type: optional<string>
348
-
validation:
349
-
maxLength: 500
350
321
```
351
322
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)
323
+
<ParamField path="minLength" type="integer">
324
+
Minimum number of characters required
358
325
</ParamField>
359
326
360
-
<ParamField path="max" type="number">
361
-
Maximum value (inclusive by default)
327
+
<ParamField path="maxLength" type="integer">
328
+
Maximum number of characters allowed
362
329
</ParamField>
363
330
364
-
<ParamField path="exclusiveMin" type="boolean">
365
-
When true, the minimum value is exclusive (value must be greater than min)
331
+
<ParamField path="pattern" type="string">
332
+
Regular expression pattern that the string must match
366
333
</ParamField>
367
334
368
-
<ParamField path="exclusiveMax" type="boolean">
369
-
When true, the maximum value is exclusive (value must be less than max)
335
+
<ParamField path="format" type="string">
336
+
String format specification (e.g., "email", "uri", "date-time")
370
337
</ParamField>
371
338
372
-
<ParamField path="multipleOf" type="number">
373
-
Value must be a multiple of this number
374
-
</ParamField>
339
+
</Accordion>
375
340
376
-
#### Example: Number validation on type alias
341
+
<Accordion title="Number validation">
377
342
378
-
```yaml
343
+
Number types (including `integer`, `long`, and `double`) support several validation constraints.
344
+
345
+
```yaml {4-6, 12-15, 18-20}
379
346
types:
380
347
Age:
381
348
type: integer
382
349
validation:
383
350
min: 0
384
351
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
352
Product:
405
353
properties:
406
354
name: string
@@ -415,157 +363,76 @@ types:
415
363
validation:
416
364
min: 1
417
365
max: 1000
418
-
discount:
419
-
type: optional<double>
420
-
validation:
421
-
min: 0
422
-
max: 100
423
366
```
424
367
425
-
### Validation in generated SDKs
368
+
<ParamField path="min" type="number">
369
+
Minimum value (inclusive by default)
370
+
</ParamField>
426
371
427
-
When you add validation constraints to your Fern Definition, they are automatically enforced in generated SDKs:
372
+
<ParamField path="max" type="number">
373
+
Maximum value (inclusive by default)
374
+
</ParamField>
428
375
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
-
```
376
+
<ParamField path="exclusiveMin" type="boolean">
377
+
When true, the minimum value is exclusive (value must be greater than min)
378
+
</ParamField>
379
+
380
+
<ParamField path="exclusiveMax" type="boolean">
381
+
When true, the maximum value is exclusive (value must be less than max)
382
+
</ParamField>
383
+
384
+
<ParamField path="multipleOf" type="number">
385
+
Value must be a multiple of this number
386
+
</ParamField>
446
387
447
-
```typescript title="TypeScript SDK"
448
-
import { YourApiClient, ValidationError } from "your-api-sdk";
388
+
</Accordion>
389
+
</AccordionGroup>
449
390
450
-
const client = new YourApiClient({
451
-
apiKey: "your-api-key"
452
-
});
391
+
#### Validation in generated SDKs
453
392
393
+
When you add validation constraints to your Fern Definition, they are automatically enforced in generated SDKs:
394
+
395
+
<CodeBlock title="Fern Definition">
396
+
```yaml {8-11, 15-17}
397
+
types:
398
+
Person:
399
+
docs: A person represents a human being
400
+
properties:
401
+
name:
402
+
docs: The person's full name
403
+
type: string
404
+
validation:
405
+
minLength: 2
406
+
maxLength: 100
407
+
pattern: "^[A-Za-z ]+$"
408
+
age:
409
+
docs: Age in years
410
+
type: integer
411
+
validation:
412
+
min: 0
413
+
max: 150
414
+
```
415
+
</CodeBlock>
416
+
<CodeBlock title="Generated TypeScript SDK from Fern Definition">
417
+
```typescript
418
+
interface Person {
419
+
/** The person's full name */
420
+
name: string;
421
+
/** Age in years */
422
+
age: number;
423
+
}
424
+
425
+
// Validation is automatically enforced when creating Person objects
426
+
const client = new YourApiClient();
454
427
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