diff --git a/package.json b/package.json index 8476263..7df53fb 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "axios": "^0.27.2", "js-yaml": "^4.1.0", "json-schema-to-typescript": "^10.1.5", - "openapi-types": "^11.0.1", + "openapi-types": "^12.1.3", "yargs": "^17.5.1", "zod-to-json-schema": "^3.20.2", "zod-validation-error": "^0.3.0" @@ -47,13 +47,13 @@ "jest": "^28.1.0", "koa": "^2.13.4", "koa-bodyparser": "^4.3.0", - "openapi-schema-validator": "^11.0.1", + "openapi-schema-validator": "^12.1.3", "prettier": "^2.6.2", "semantic-release": "^19.0.3", "tmp": "^0.2.1", "ts-jest": "^28.0.3", "ts-node": "^10.8.0", - "typescript": "^4.7.2", + "typescript": "^5.1.6", "uuid": "^9.0.0", "zod": "^3.20.2" } diff --git a/src/koa.ts b/src/koa.ts index 54ef06e..857c447 100644 --- a/src/koa.ts +++ b/src/koa.ts @@ -70,27 +70,6 @@ export type ImplementationConfig< const ajv = new Ajv(); -const defaultParse: ImplementationConfig['parse'] = ( - ctx, - { endpoint, data, schema }, -) => { - if (!ajv.validate(schema, data)) { - const method = (endpoint as string).split(' ')[0]; - const dataVar = ['GET', 'DELETE'].includes(method) - ? 'query parameters' - : 'payload'; - - return ctx.throw( - 400, - `The request did not conform to the required schema: ${ajv.errorsText( - undefined, - { dataVar }, - )}`, - ); - } - return data as any; -}; - /** * Implements the specified `schema` on the provided router object. * @@ -113,12 +92,33 @@ export const implementSchema = < addIntrospection(introspection, () => schema, router); } + const defaultParse: ImplementationConfig['parse'] = ( + ctx, + { endpoint, data, schema }, + ) => { + if (!ajv.validate(schema, data)) { + const method = (endpoint as string).split(' ')[0]; + const dataVar = ['GET', 'DELETE'].includes(method) + ? 'query parameters' + : 'payload'; + + return ctx.throw( + 400, + `The request did not conform to the required schema: ${ajv.errorsText( + undefined, + { dataVar }, + )}`, + ); + } + return data; + }; + // Iterate through every handler, and add a route for it based on // the key/route description. for (const endpoint in implementation) { const routeHandler = implementation[endpoint]; - const parser: typeof parse = parse ?? defaultParse; + const parser = parse ?? defaultParse; implementRoute( endpoint, diff --git a/yarn.lock b/yarn.lock index 33783aa..b5bc74a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5894,20 +5894,20 @@ only@~0.0.2: resolved "https://registry.yarnpkg.com/only/-/only-0.0.2.tgz#2afde84d03e50b9a8edc444e30610a70295edfb4" integrity sha1-Kv3oTQPlC5qO3EROMGEKcCle37Q= -openapi-schema-validator@^11.0.1: - version "11.0.1" - resolved "https://registry.yarnpkg.com/openapi-schema-validator/-/openapi-schema-validator-11.0.1.tgz#430ca95e02b79f9750b57f51051a1f97af30248c" - integrity sha512-c2+6qqsIZ2rX5CrxrW7QjsDYNw/fA1XPMBq/dMeU+k/I1y6GSbl64jutbLaLhEORd2coavga16eeYx3GtmkWKQ== +openapi-schema-validator@^12.1.3: + version "12.1.3" + resolved "https://registry.yarnpkg.com/openapi-schema-validator/-/openapi-schema-validator-12.1.3.tgz#c9234af67b00cdbbecfdd4eb546d7006bacfe518" + integrity sha512-xTHOmxU/VQGUgo7Cm0jhwbklOKobXby+/237EG967+3TQEYJztMgX9Q5UE2taZKwyKPUq0j11dngpGjUuxz1hQ== dependencies: ajv "^8.1.0" ajv-formats "^2.0.2" lodash.merge "^4.6.1" - openapi-types "^11.0.1" + openapi-types "^12.1.3" -openapi-types@^11.0.1: - version "11.0.1" - resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-11.0.1.tgz#7e1cee2c9b8cb16787d14f2b0f433c5ba0e32e96" - integrity sha512-P2pGRlHFXgP8z6vrp5P/MtftOXYtlIY1A+V0VmioOoo85NN6RSPgGbEprRAUNMIsbfRjnCPdx/r8mi8QRR7grQ== +openapi-types@^12.1.3: + version "12.1.3" + resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-12.1.3.tgz#471995eb26c4b97b7bd356aacf7b91b73e777dd3" + integrity sha512-N4YtSYJqghVu4iek2ZUvcN/0aqH1kRDuNqzcycDxhOUpg7GdvLa2F3DgS6yBNhInhv2r/6I0Flkn7CqL8+nIcw== opener@^1.5.2: version "1.5.2" @@ -7143,10 +7143,10 @@ type@^2.5.0: resolved "https://registry.yarnpkg.com/type/-/type-2.6.0.tgz#3ca6099af5981d36ca86b78442973694278a219f" integrity sha512-eiDBDOmkih5pMbo9OqsqPRGMljLodLcwd5XD5JbtNB0o89xZAwynY9EdCDsJU7LtcVCClu9DvM7/0Ep1hYX3EQ== -typescript@^4.7.2: - version "4.7.2" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.2.tgz#1f9aa2ceb9af87cca227813b4310fff0b51593c4" - integrity sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A== +typescript@^5.1.6: + version "5.1.6" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.6.tgz#02f8ac202b6dad2c0dd5e0913745b47a37998274" + integrity sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA== uglify-js@^3.1.4: version "3.15.5"