From 3cde3288a627e6f7a0f97f9b38e5c487da4ff2a1 Mon Sep 17 00:00:00 2001 From: Jeremy LaCivita Date: Thu, 23 May 2024 13:42:57 -0400 Subject: [PATCH] fix: Use x-response-name to disambiguate pass-through schemas --- src/validate/validator/index.mjs | 35 ++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/validate/validator/index.mjs b/src/validate/validator/index.mjs index 2bf7e3b0..687aaada 100644 --- a/src/validate/validator/index.mjs +++ b/src/validate/validator/index.mjs @@ -202,6 +202,7 @@ export const validatePasshtroughs = (json) => { const provider = json.methods.find(m => m.name === providerName) let destination, examples1 let source, examples2 + let sourceName if (!provider) { result.errors.push({ @@ -214,12 +215,14 @@ export const validatePasshtroughs = (json) => { destination = getPayloadFromEvent(method) examples1 = method.examples.map(e => e.result.value) source = provider.params[provider.params.length-1].schema + sourceName = provider.params[provider.params.length-1].name examples2 = provider.examples.map(e => e.params[e.params.length-1].value) } else { destination = method.result.schema examples1 = method.examples.map(e => e.result.value) source = JSON.parse(JSON.stringify(provider.tags.find(t => t['x-response'])['x-response'])) + sourceName = provider.tags.find(t => t['x-response'])['x-response-name'] examples2 = provider.tags.find(t => t['x-response'])['x-response'].examples delete source.examples } @@ -231,24 +234,26 @@ export const validatePasshtroughs = (json) => { source = getPropertySchema(source, '.', json) destination = getPropertySchema(destination, '.', json) - if (properties && properties.length) { - const destinationProperty = properties.find(property => { - let candidate = getPropertySchema(destination, `properties.${property}`, json) - - candidate && (candidate = getPropertySchema(candidate, '.', json)) // follow $refs + if (properties && properties.length && sourceName) { + let candidate = getPropertySchema(getPropertySchema(destination, `properties.${sourceName}`, json), '.', json) - if (schemasMatch(candidate, source)) { - return true - } - }) - - if (!destinationProperty) { + if (!candidate) { + result.errors.push({ + message: `The x-provided-by method '${providerName}' does not have a matching result schema or ${sourceName} property`, + instancePath: `/methods/${json.methods.indexOf(method)}` + }) + } else if (!schemasMatch(candidate, source)) { result.errors.push({ - message: `The x-provided-by method '${providerName}' does not have a matching schema or sub-schema`, + message: `The x-provided-by method '${providerName}' does not have a matching result schema or ${sourceName} schema`, instancePath: `/methods/${json.methods.indexOf(method)}` - }) - result.title = `Mismatched x-provided-by schemas in ${result.errors.length} methods.` - } + }) + } + } + else if (!sourceName) { + result.errors.push({ + message: `The x-provided-by method '${providerName}' does not have a matching result schema and has no x-response-name property to inject into`, + instancePath: `/methods/${json.methods.indexOf(method)}` + }) } else { result.errors.push({