Skip to content

Commit

Permalink
fix: Use x-response-name to disambiguate pass-through schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
jlacivita committed May 23, 2024
1 parent 7dfdf7d commit 3cde328
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/validate/validator/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
}
Expand All @@ -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({
Expand Down

0 comments on commit 3cde328

Please sign in to comment.