API / Swagger Interview questions
Explain how to enforce validation constraints in an OpenAPI schema?
OpenAPI schemas support a range of validation keywords, inherited from JSON Schema, that go beyond simply declaring a field's type — letting a spec express constraints like string length, numeric ranges, and allowed value sets that a validating tool can check requests and responses against.
Product: type: object required: [name, price] properties: name: type: string minLength: 1 maxLength: 100 price: type: number minimum: 0 exclusiveMinimum: true category: type: string enum: [electronics, clothing, food] sku: type: string pattern: '^[A-Z]{3}-\\d{4}$'
Common constraint keywords include minLength/maxLength for strings, minimum/maximum (with optional exclusiveMinimum/exclusiveMaximum) for numbers, enum for a fixed set of allowed values, and pattern for regex-validated string formats like the SKU example above; array types additionally support minItems, maxItems, and uniqueItems.
These constraints matter beyond documentation value: request validation middleware (in an API gateway, or in framework-integrated validation like Spring's Bean Validation working alongside springdoc-openapi) can enforce them automatically against real incoming requests, rejecting invalid data before it ever reaches business logic, and mock servers can use the same constraints to generate more realistic sample data than an unconstrained schema would allow.
More Related questions...