express-openapi-validator
Advanced tools
Comparing version 5.3.1 to 5.3.2
@@ -1,2 +0,2 @@ | ||
import { OpenAPIV3, SecurityHandlers, OpenApiRequestHandler } from '../framework/types'; | ||
import { OpenApiRequestHandler, OpenAPIV3, SecurityHandlers } from '../framework/types'; | ||
export declare function security(apiDoc: OpenAPIV3.Document, securityHandlers: SecurityHandlers): OpenApiRequestHandler; |
@@ -6,2 +6,16 @@ "use strict"; | ||
const defaultSecurityHandler = (req, scopes, schema) => true; | ||
function extractErrorsFromResults(results) { | ||
return results.map(result => { | ||
if (Array.isArray(result)) { | ||
return result.map(it => it).filter(it => !it.success); | ||
} | ||
return [result].filter(it => !it.success); | ||
}).flatMap(it => [...it]); | ||
} | ||
function didAllSecurityRequirementsPass(results) { | ||
return results.every(it => it.success); | ||
} | ||
function didOneSchemaPassValidation(results) { | ||
return results.some(result => Array.isArray(result) ? didAllSecurityRequirementsPass(result) : result.success); | ||
} | ||
function security(apiDoc, securityHandlers) { | ||
@@ -33,39 +47,3 @@ return async (req, res, next) => { | ||
// This assumes OR only! i.e. at least one security passed authentication | ||
let firstError = null; | ||
let success = false; | ||
function checkErrorWithOr(res) { | ||
return res.forEach((r) => { | ||
if (r.success) { | ||
success = true; | ||
} | ||
else if (!firstError) { | ||
firstError = r; | ||
} | ||
}); | ||
} | ||
function checkErrorsWithAnd(res) { | ||
let allSuccess = false; | ||
res.forEach((r) => { | ||
if (!r.success) { | ||
allSuccess = false; | ||
if (!firstError) { | ||
firstError = r; | ||
} | ||
} | ||
else if (!firstError) { | ||
allSuccess = true; | ||
} | ||
}); | ||
if (allSuccess) { | ||
success = true; | ||
} | ||
} | ||
results.forEach((result) => { | ||
if (Array.isArray(result) && result.length > 1) { | ||
checkErrorsWithAnd(result); | ||
} | ||
else { | ||
checkErrorWithOr(result); | ||
} | ||
}); | ||
const success = didOneSchemaPassValidation(results); | ||
if (success) { | ||
@@ -75,3 +53,4 @@ next(); | ||
else { | ||
throw firstError; | ||
const errors = extractErrorsFromResults(results); | ||
throw errors[0]; | ||
} | ||
@@ -78,0 +57,0 @@ } |
{ | ||
"name": "express-openapi-validator", | ||
"version": "5.3.1", | ||
"version": "5.3.2", | ||
"description": "Automatically validate API requests and responses with OpenAPI 3 and Express.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
367855
6244