Skip to content

Commit

Permalink
Fix boolean validation and request body parse (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Nov 29, 2022
2 parents 897349e + 65f7b02 commit f6fad81
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cloudflare/itty-router-openapi",
"version": "0.0.7",
"version": "0.0.8",
"description": "OpenAPI schema generator and validator for Cloudflare Workers",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export class DateOnly extends Str {
}
}

export class Bool extends BaseParameter {
export class Bool extends Str {
type = 'boolean'
private validValues = ['true', 'false']

Expand All @@ -339,7 +339,7 @@ export class Bool extends BaseParameter {
value = value.toLowerCase()

if (!this.validValues.includes(value)) {
throw new ValidationError('is not a valid boolean, allowed values true or false')
throw new ValidationError('is not a valid boolean, allowed values are true or false')
}

value = value === 'true'
Expand Down
15 changes: 12 additions & 3 deletions src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,22 @@ export class OpenAPIRoute implements OpenAPIRouteSchema {
}

if (request.method.toLowerCase() !== 'get' && requestBody) {
const json = await request.json()
let json
let loaded = false

try {
validatedObj['body'] = new Body(requestBody).validate(json)
json = await request.json()
loaded = true
} catch (e) {
validationErrors['body' + (e as ApiException).key] = (e as ApiException).message
validationErrors['body'] = (e as ApiException).message
}

if (loaded)
try {
validatedObj['body'] = new Body(requestBody).validate(json)
} catch (e) {
validationErrors['body' + (e as ApiException).key] = (e as ApiException).message
}
}

return {
Expand Down

0 comments on commit f6fad81

Please sign in to comment.