Skip to content

Commit 3546263

Browse files
committed
feat: exposed coerced query parameters to other hooks
1 parent 14405ae commit 3546263

File tree

11 files changed

+272
-390
lines changed

11 files changed

+272
-390
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
This mono repository contains libraries for implementing APIs. It contains the following sub-packages:
44

55
* [ajv-openapi-request-response-validator](./packages/ajv-openapi-request-response-validator/README.md): Validation of http request and responses against an OpenAPI schema.
6-
* [azure-functions-openapi-validator](./packages/azure-functions-openapi-validator/): Azure functions v4 middleware that uses the openapi validator
6+
* [azure-functions-openapi-validator](./packages/azure-functions-openapi-validator/README.md): Azure functions v4 middleware that uses the openapi validator
77
* [azure-functions-openapi-validator-example](./packages/azure-functions-openapi-validator-example/README.md): Example Azure functions v4 app that uses the middleware

packages/ajv-openapi-request-response-validator/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This library contains an Open API v3.0 validator that validates http requests an
1414
* Does not support `readOnly` or `writeOnly`.
1515
* The default config sets AJV's `removeAdditional` to `false`, otherwise `allOf` validation may cause unexpected results
1616
* The default config sets AJV's `coerceTypes` to `false`, otherwise `anyof` validation may cause unexpected results
17-
* Query params are usually string values on the other hand, to make them work this library coerces those by default prior to validation
17+
* Query params are usually string values on the other hand, so this library coerces those by default prior to validation
1818
* This library does not validate the Open API specification itself. I suggest you use another tool for this for now.
1919

2020
To check out what is supported, take a look at the [test fixtures](/test/fixtures/)

packages/ajv-openapi-request-response-validator/src/ajv-openapi-request-response-validator.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
Primitive,
1818
isURLSearchParams,
1919
unserializeParameters,
20+
STRICT_COERCION_STRATEGY,
2021
} from './openapi-validator'
2122
import { merge, openApiMergeRules } from 'allof-merge'
2223

@@ -111,7 +112,7 @@ export class AjvOpenApiValidator {
111112
origParams: Record<string, Primitive> | URLSearchParams,
112113
strict = true,
113114
logger?: Logger
114-
): ErrorObj[] | undefined {
115+
): { normalizedParams: Record<string, Primitive>; errors: ErrorObj[] | undefined } {
115116
const parameterDefinitions = this.paramsValidators.filter((p) => p.path === path?.toLowerCase() && p.method === method?.toLowerCase())
116117

117118
const log = logger ? logger : this.validatorOpts.logger
@@ -196,7 +197,7 @@ export class AjvOpenApiValidator {
196197
})
197198
}
198199

199-
return errResponse.length ? errResponse : undefined
200+
return { normalizedParams: params, errors: errResponse.length ? errResponse : undefined }
200201
}
201202

202203
validateRequestBody(path: string, method: string, data: unknown, strict = true, logger?: Logger): ErrorObj[] | undefined {
@@ -274,7 +275,7 @@ export class AjvOpenApiValidator {
274275
return undefined
275276
}
276277

277-
private initialize(origSpec: OpenAPIV3.Document, coerceTypes: boolean): void {
278+
private initialize(origSpec: OpenAPIV3.Document, coerceTypes: boolean | 'strict'): void {
278279
const schemaCompileFailures: string[] = []
279280
const spec: OpenAPIV3.Document = merge(origSpec, { rules: openApiMergeRules() })
280281

@@ -478,14 +479,15 @@ export class AjvOpenApiValidator {
478479
}
479480
})
480481

481-
if (this.validatorOpts.coerceTypes == true && resolvedParams.length > 0) {
482+
if (this.validatorOpts.coerceTypes && resolvedParams.length > 0) {
482483
this.requestCoercers.push({
483484
path: path.toLowerCase(),
484485
method: method.toLowerCase() as string,
485486
coercer: new OpenapiRequestCoercer({
486487
logger: this.validatorOpts.logger,
487488
enableObjectCoercion: true,
488489
parameters: resolvedParams,
490+
coercionStrategy: this.validatorOpts.coerceTypes === 'strict' ? STRICT_COERCION_STRATEGY : undefined,
489491
}),
490492
})
491493
}

packages/ajv-openapi-request-response-validator/src/coercer.ts

Lines changed: 0 additions & 299 deletions
This file was deleted.

0 commit comments

Comments
 (0)