From 584312e480e2bd618c260769bc84e2ff008d39cf Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Fri, 10 Jan 2025 10:51:20 +0100 Subject: [PATCH] fix joi validator for referenced alternative schemas --- .../typescript/joi/JoiValidatorModuleRenderer.kt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/languages/typescript/src/main/kotlin/io/vrap/codegen/languages/typescript/joi/JoiValidatorModuleRenderer.kt b/languages/typescript/src/main/kotlin/io/vrap/codegen/languages/typescript/joi/JoiValidatorModuleRenderer.kt index 09f66e4c6..140c5f812 100644 --- a/languages/typescript/src/main/kotlin/io/vrap/codegen/languages/typescript/joi/JoiValidatorModuleRenderer.kt +++ b/languages/typescript/src/main/kotlin/io/vrap/codegen/languages/typescript/joi/JoiValidatorModuleRenderer.kt @@ -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()}>""" @@ -121,10 +121,16 @@ 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()) @@ -132,10 +138,10 @@ class JoiValidatorModuleRenderer constructor(override val vrapTypeProvider: Vrap |Joi.object().keys({ | <$nonPatternProperties> |}) - |$patternProperties + |$patternProperties${if (sharedTypes.isNotEmpty()) ".$sharedTypes" else ""} """.trimMargin() else - "Joi.object()<$patternProperties>" + "Joi.object()<$patternProperties>${if (sharedTypes.isNotEmpty()) ".$sharedTypes" else ""}" } }