diff --git a/README.md b/README.md index c610dd4..95a8265 100644 --- a/README.md +++ b/README.md @@ -135,15 +135,16 @@ and ready to be invoked. #### `OpenAPIRouter(options = {})` -| Name | Type(s) | Description | Examples | -| ------------- | --------------------------------- | ----------------------------------------------------------------------- | -------------------------------------------------------------------------- | -| `base` | `string` | prefixes all routes with this string | `Router({ base: '/api' })` | -| `routes` | `array of routes` | array of manual routes for preloading | [see documentation](https://github.com/kwhitley/itty-router#manual-routes) | -| `schema` | `object` | Object of the common OpenAPI customizations | [see documentation](#4-core-openapi-schema-customizations) | -| `docs_url` | `string` or `null` or `undefined` | Path for swagger docs, `null`: disabled, `undefined`: `/docs` | `/docs` | -| `redoc_url` | `string` or `null` or `undefined` | Path for redoc docs, `null`: disabled, `undefined`: `/redocs` | `/redocs` | -| `openapi_url` | `string` or `null` or `undefined` | Path for openapi schema, `null`: disabled, `undefined`: `/openapi.json` | `/openapi.json` | -| `aiPlugin` | `object` or `undefined` | Object that will be used to generate the `ai-plugin.json` schema | [see schema bellow](#aiplugin) | +| Name | Type(s) | Description | Examples | +| ------------------------ | --------------------------------- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | +| `base` | `string` | prefixes all routes with this string | `Router({ base: '/api' })` | +| `routes` | `array of routes` | array of manual routes for preloading | [see documentation](https://github.com/kwhitley/itty-router#manual-routes) | +| `schema` | `object` | Object of the common OpenAPI customizations | [see documentation](#4-core-openapi-schema-customizations) | +| `docs_url` | `string` or `null` or `undefined` | Path for swagger docs, `null`: disabled, `undefined`: `/docs` | `/docs` | +| `redoc_url` | `string` or `null` or `undefined` | Path for redoc docs, `null`: disabled, `undefined`: `/redocs` | `/redocs` | +| `openapi_url` | `string` or `null` or `undefined` | Path for openapi schema, `null`: disabled, `undefined`: `/openapi.json` | `/openapi.json` | +| `raiseUnknownParameters` | `boolean` | This will raise validation errors when an endpoint received an unknown query parameter | true | +| `aiPlugin` | `object` or `undefined` | Object that will be used to generate the `ai-plugin.json` schema | [see schema bellow](#aiplugin) | #### `aiPlugin` diff --git a/src/openapi.ts b/src/openapi.ts index 64ea6ba..64af871 100644 --- a/src/openapi.ts +++ b/src/openapi.ts @@ -14,6 +14,7 @@ export function OpenAPIRouter(options?: RouterOptions): OpenAPIRouterSchema { title: options?.schema?.info?.title || 'OpenAPI', version: options?.schema?.info?.version || '1.0', }, + raiseUnknownParameters: options?.raiseUnknownParameters, // TODO: turn this true by default in the future ...options?.schema, } @@ -104,7 +105,10 @@ export function OpenAPIRouter(options?: RouterOptions): OpenAPIRouterSchema { } if (handler.isRoute) { - return (...params: any[]) => new handler().execute(...params) + return (...params: any[]) => + new handler({ + raiseUnknownParameters: openapiConfig.raiseUnknownParameters, + }).execute(...params) } return handler diff --git a/src/route.ts b/src/route.ts index 62dc930..d2d8996 100644 --- a/src/route.ts +++ b/src/route.ts @@ -1,11 +1,24 @@ -import { OpenAPIRouteSchema, OpenAPISchema, RouteValidated } from './types' +import { OpenAPIRouteSchema, OpenAPISchema, RouteOptions, RouteValidated } from './types' import { ApiException } from './exceptions' -import { Body, extractParameter, extractQueryParameters, getFormatedParameters, Parameter, Resp } from './parameters' +import { + Arr, + Body, + extractParameter, + extractQueryParameters, + getFormatedParameters, + Parameter, + Resp, +} from './parameters' export class OpenAPIRoute implements OpenAPIRouteSchema { static isRoute = true static schema: OpenAPISchema + params: RouteOptions + + constructor(params: RouteOptions) { + this.params = params + } static getSchema(): OpenAPISchema { return this.schema @@ -94,6 +107,29 @@ export class OpenAPIRoute implements OpenAPIRouteSchema { const validatedObj: Record = {} const validationErrors: Record = {} + // check for query unknown parameters + if (this.params?.raiseUnknownParameters) { + for (const key of Object.keys(queryParams)) { + if (Array.isArray(params)) { + let exists = false + for (const param of params) { + if (param.params.name === key) { + exists = true + break + } + } + + if (!exists) { + validationErrors[key] = `is an unknown parameter` + } + } else { + if (params[key] === undefined) { + validationErrors[key] = `is an unknown parameter` + } + } + } + } + for (const [key, value] of Object.entries(params)) { // @ts-ignore const param: Parameter = value diff --git a/src/types.ts b/src/types.ts index 72d5345..5b48eea 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,6 +30,11 @@ export interface RouterOptions { redoc_url?: string openapi_url?: string aiPlugin?: AIPlugin + raiseUnknownParameters?: boolean +} + +export interface RouteOptions { + raiseUnknownParameters: boolean } export interface OpenAPISchema {