diff --git a/fern/products/fern-def/pages/types.mdx b/fern/products/fern-def/pages/types.mdx index 3769526f6..f623832d7 100644 --- a/fern/products/fern-def/pages/types.mdx +++ b/fern/products/fern-def/pages/types.mdx @@ -278,4 +278,151 @@ interface Person { age: number; } ``` - \ No newline at end of file + + +### Validating types + +You can add validation constraints to your types (both aliases and references) to ensure data integrity. Validation constracts are automatically enforced in generated SDKs. + + +```yaml {8-11, 15-17} +types: + Person: + docs: A person represents a human being + properties: + name: + docs: The person's full name + type: string + validation: + minLength: 2 + maxLength: 100 + pattern: "^[A-Za-z ]+$" + age: + docs: Age in years + type: integer + validation: + min: 0 + max: 150 +``` + + + +```typescript +interface Person { + /** The person's full name */ + name: string; + /** Age in years */ + age: number; +} + +// Validation is automatically enforced when creating Person objects +const client = new YourApiClient(); +try { + const person = await client.createPerson({ + name: "A", // Fails minLength validation + age: -5, // Fails min validation + }); +} catch (error) { + if (error instanceof ValidationError) { + console.log(`Validation failed: ${error.message}`); + } +} +``` + + + + + +String types support several validation constraints. + +```yaml {4-6, 11-13, 16-19} +types: + Word: + type: string + validation: + minLength: 2 + maxLength: 26 + User: + properties: + email: + type: string + validation: + format: email + maxLength: 254 + username: + type: string + validation: + minLength: 3 + maxLength: 20 + pattern: "^[a-zA-Z0-9_]+$" +``` + + + Minimum number of characters required + + + + Maximum number of characters allowed + + + + Regular expression pattern that the string must match + + + + String format specification (e.g., "email", "uri", "date-time") + + + + + + +Number types (including `integer`, `long`, and `double`) support several validation constraints. + +```yaml {4-6, 12-15, 18-20} +types: + Age: + type: integer + validation: + min: 0 + max: 150 + Product: + properties: + name: string + price: + type: double + validation: + min: 0 + exclusiveMin: true + multipleOf: 0.01 + quantity: + type: integer + validation: + min: 1 + max: 1000 +``` + + + Minimum value (inclusive by default) + + + + Maximum value (inclusive by default) + + + + When true, the minimum value is exclusive (value must be greater than min) + + + + When true, the maximum value is exclusive (value must be less than max) + + + + Value must be a multiple of this number + + + + + +