Skip to content

Commit 026f556

Browse files
Documentation updates from Promptless
1 parent 296a938 commit 026f556

File tree

1 file changed

+291
-1
lines changed

1 file changed

+291
-1
lines changed

fern/products/fern-def/pages/types.mdx

Lines changed: 291 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,294 @@ interface Person {
278278
age: number;
279279
}
280280
```
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
458+
username: "ab", // Fails minLength validation
459+
age: -5 // Fails min value validation
460+
});
461+
} catch (error) {
462+
if (error instanceof ValidationError) {
463+
console.log(`Validation failed: ${error.message}`);
464+
}
465+
}
466+
```
467+
468+
```java title="Java SDK"
469+
import com.yourapi.YourApiClient;
470+
import com.yourapi.core.ValidationException;
471+
472+
YourApiClient client = YourApiClient.builder()
473+
.apiKey("your-api-key")
474+
.build();
475+
476+
try {
477+
// This will throw a ValidationException
478+
User user = client.users().create(CreateUserRequest.builder()
479+
.email("invalid-email") // Fails email format validation
480+
.username("ab") // Fails minLength validation
481+
.age(-5) // Fails min value validation
482+
.build());
483+
} catch (ValidationException e) {
484+
System.out.println("Validation failed: " + e.getMessage());
485+
}
486+
```
487+
</CodeBlocks>
488+
489+
### Best practices
490+
491+
<Tip>
492+
**Use validation to improve API reliability**: Validation constraints help catch errors early and provide clear feedback to API consumers.
493+
</Tip>
494+
495+
<Tip>
496+
**Be consistent with formats**: Use standard format values like "email", "uri", "date-time" for better interoperability.
497+
</Tip>
498+
499+
<Tip>
500+
**Document your patterns**: When using regex patterns, consider adding documentation to explain the expected format.
501+
</Tip>
502+
503+
```yaml
504+
types:
505+
ProductCode:
506+
docs: Product code must be 3 uppercase letters followed by 4 digits (e.g., ABC1234)
507+
type: string
508+
validation:
509+
pattern: "^[A-Z]{3}\\d{4}$"
510+
```
511+
512+
<Warning>
513+
**Regex escaping**: Remember to properly escape backslashes in YAML strings. Use `\\d` instead of `\d` for digit patterns.
514+
</Warning>
515+
516+
### Common validation patterns
517+
518+
Here are some commonly used validation patterns:
519+
520+
```yaml
521+
types:
522+
# Email validation
523+
Email:
524+
type: string
525+
validation:
526+
format: email
527+
maxLength: 254
528+
529+
# URL validation
530+
WebsiteUrl:
531+
type: string
532+
validation:
533+
format: uri
534+
pattern: "^https?://"
535+
536+
# Phone number (international format)
537+
PhoneNumber:
538+
type: string
539+
validation:
540+
pattern: "^\\+?[1-9]\\d{1,14}$"
541+
542+
# Credit card number (basic format)
543+
CreditCardNumber:
544+
type: string
545+
validation:
546+
pattern: "^\\d{13,19}$"
547+
minLength: 13
548+
maxLength: 19
549+
550+
# Positive currency amount
551+
Amount:
552+
type: double
553+
validation:
554+
min: 0
555+
exclusiveMin: true
556+
multipleOf: 0.01
557+
558+
# Percentage (0-100)
559+
Percentage:
560+
type: double
561+
validation:
562+
min: 0
563+
max: 100
564+
565+
# Port number
566+
Port:
567+
type: integer
568+
validation:
569+
min: 1
570+
max: 65535
571+
```

0 commit comments

Comments
 (0)