Skip to content

Commit

Permalink
Add option to raise error when a unknown parameter is received (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Jun 2, 2023
2 parents 31f5997 + dd24866 commit 50b9ec8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
6 changes: 5 additions & 1 deletion src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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
Expand Down
40 changes: 38 additions & 2 deletions src/route.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -94,6 +107,29 @@ export class OpenAPIRoute implements OpenAPIRouteSchema {
const validatedObj: Record<string, any> = {}
const validationErrors: Record<string, any> = {}

// 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
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 50b9ec8

Please sign in to comment.