Skip to content

Commit bcc9a6d

Browse files
docs: Document validation in Fern Definition (#384)
Co-authored-by: promptless[bot] <179508745+promptless[bot]@users.noreply.github.com> Co-authored-by: Devin Logan <[email protected]>
1 parent 296a938 commit bcc9a6d

File tree

1 file changed

+148
-1
lines changed

1 file changed

+148
-1
lines changed

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

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,151 @@ interface Person {
278278
age: number;
279279
}
280280
```
281-
</CodeBlock>
281+
</CodeBlock>
282+
283+
### Validating types
284+
285+
You can add validation constraints to your types (both aliases and references) to ensure data integrity. Validation constracts are automatically enforced in generated SDKs.
286+
287+
<CodeBlock title="Fern Definition">
288+
```yaml {8-11, 15-17}
289+
types:
290+
Person:
291+
docs: A person represents a human being
292+
properties:
293+
name:
294+
docs: The person's full name
295+
type: string
296+
validation:
297+
minLength: 2
298+
maxLength: 100
299+
pattern: "^[A-Za-z ]+$"
300+
age:
301+
docs: Age in years
302+
type: integer
303+
validation:
304+
min: 0
305+
max: 150
306+
```
307+
</CodeBlock>
308+
<CodeBlock title="Generated TypeScript SDK from Fern Definition">
309+
310+
```typescript
311+
interface Person {
312+
/** The person's full name */
313+
name: string;
314+
/** Age in years */
315+
age: number;
316+
}
317+
318+
// Validation is automatically enforced when creating Person objects
319+
const client = new YourApiClient();
320+
try {
321+
const person = await client.createPerson({
322+
name: "A", // Fails minLength validation
323+
age: -5, // Fails min validation
324+
});
325+
} catch (error) {
326+
if (error instanceof ValidationError) {
327+
console.log(`Validation failed: ${error.message}`);
328+
}
329+
}
330+
```
331+
</CodeBlock>
332+
333+
<AccordionGroup>
334+
<Accordion title="String validation reference">
335+
336+
String types support several validation constraints.
337+
338+
```yaml {4-6, 11-13, 16-19}
339+
types:
340+
Word:
341+
type: string
342+
validation:
343+
minLength: 2
344+
maxLength: 26
345+
User:
346+
properties:
347+
email:
348+
type: string
349+
validation:
350+
format: email
351+
maxLength: 254
352+
username:
353+
type: string
354+
validation:
355+
minLength: 3
356+
maxLength: 20
357+
pattern: "^[a-zA-Z0-9_]+$"
358+
```
359+
360+
<ParamField path="minLength" type="integer">
361+
Minimum number of characters required
362+
</ParamField>
363+
364+
<ParamField path="maxLength" type="integer">
365+
Maximum number of characters allowed
366+
</ParamField>
367+
368+
<ParamField path="pattern" type="string">
369+
Regular expression pattern that the string must match
370+
</ParamField>
371+
372+
<ParamField path="format" type="string">
373+
String format specification (e.g., "email", "uri", "date-time")
374+
</ParamField>
375+
376+
</Accordion>
377+
378+
<Accordion title="Number validation reference">
379+
380+
Number types (including `integer`, `long`, and `double`) support several validation constraints.
381+
382+
```yaml {4-6, 12-15, 18-20}
383+
types:
384+
Age:
385+
type: integer
386+
validation:
387+
min: 0
388+
max: 150
389+
Product:
390+
properties:
391+
name: string
392+
price:
393+
type: double
394+
validation:
395+
min: 0
396+
exclusiveMin: true
397+
multipleOf: 0.01
398+
quantity:
399+
type: integer
400+
validation:
401+
min: 1
402+
max: 1000
403+
```
404+
405+
<ParamField path="min" type="number">
406+
Minimum value (inclusive by default)
407+
</ParamField>
408+
409+
<ParamField path="max" type="number">
410+
Maximum value (inclusive by default)
411+
</ParamField>
412+
413+
<ParamField path="exclusiveMin" type="boolean">
414+
When true, the minimum value is exclusive (value must be greater than min)
415+
</ParamField>
416+
417+
<ParamField path="exclusiveMax" type="boolean">
418+
When true, the maximum value is exclusive (value must be less than max)
419+
</ParamField>
420+
421+
<ParamField path="multipleOf" type="number">
422+
Value must be a multiple of this number
423+
</ParamField>
424+
425+
</Accordion>
426+
</AccordionGroup>
427+
428+

0 commit comments

Comments
 (0)