Skip to content

Commit

Permalink
fix joi validator for referenced alternative schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Jan 10, 2025
1 parent ca979ff commit 584312e
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ class JoiValidatorModuleRenderer constructor(override val vrapTypeProvider: Vrap

}.joinToString(separator = ", ")

val id = if(sharedSchema.isNotEmpty()) """.id('$parentType').${sharedSchema.joinToString(separator = ".") { "shared($it())" }}""" else ""
val id = if(sharedSchema.isNotEmpty()) """.${sharedSchema.joinToString(separator = ".") { "shared($it())" }}""" else ""
joiAlternativesTypes.add(this.toVrapType().simpleJoiName())

schemaDeclaration = """Joi.alternatives().try($allSubsCases)$id"""
schemaDeclaration = """Joi.alternatives().try($allSubsCases).id('$parentType')$id"""

} else {
schemaDeclaration = """<${renderPropertySchemas()}>"""
Expand All @@ -121,21 +121,27 @@ class JoiValidatorModuleRenderer constructor(override val vrapTypeProvider: Vrap
val patternProperties = this.allProperties.renderPatternProperties()
val additionalProperties = this.additionalProperties?:true
val unknown = if (!additionalProperties) ".unknown(false)" else ""

val sharedTypes = this.allProperties
.filter { !it.isPatternProperty() }
.filter { joiAlternativesTypes.contains(it.type.toVrapType().simpleJoiName()) && discriminatorProperty }
.sortedWith(PropertiesComparator)
.joinToString(separator = ".") { "shared(${it.type.toVrapType().simpleJoiName()}())" }
return if (patternProperties.isNullOrEmpty())
""" |Joi.object()${unknown}.keys({
| <$nonPatternProperties>
|})
|})${if (sharedTypes.isNotEmpty()) ".$sharedTypes" else ""}
""".trimMargin()
else {
if (nonPatternProperties.isNotBlank())
"""
|Joi.object().keys({
| <$nonPatternProperties>
|})
|$patternProperties
|$patternProperties${if (sharedTypes.isNotEmpty()) ".$sharedTypes" else ""}
""".trimMargin()
else
"Joi.object()<$patternProperties>"
"Joi.object()<$patternProperties>${if (sharedTypes.isNotEmpty()) ".$sharedTypes" else ""}"
}
}

Expand Down

0 comments on commit 584312e

Please sign in to comment.