Skip to content

Commit

Permalink
Validate blank dictId in fragment definition (#5815)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrzontek committed Apr 4, 2024
1 parent 38eb082 commit 45c8ded
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
Expand Up @@ -351,6 +351,10 @@ object ProcessCompilationError {
extends DuplicateFragmentOutputNames
with ScenarioGraphLevelError

final case class EmptyMandatoryField(nodeId: String, qualifiedFieldName: ParameterName)
extends PartSubGraphCompilationError
with InASingleNode

final case class DictNotDeclared(dictId: String, nodeId: String, paramName: ParameterName)
extends PartSubGraphCompilationError
with InASingleNode
Expand Down
Expand Up @@ -211,6 +211,12 @@ object PrettyValidationErrors {
paramName =
Some(qualifiedParamFieldName(paramName = paramName, subFieldName = Some(ValidationExpressionFieldName)))
)
case EmptyMandatoryField(_, qualifiedFieldName) =>
node(
message = s"This field is mandatory and cannot be empty",
description = s"This field is mandatory and cannot be empty",
paramName = Some(qualifiedFieldName)
)
case DictNotDeclared(dictId, _, paramName) =>
node(
message = s"Dictionary not declared: $dictId",
Expand Down
Expand Up @@ -612,7 +612,20 @@ class UIProcessValidatorSpec extends AnyFunSuite with Matchers with TableDrivenP
valueCompileTimeValidation = None
),
FragmentParameter(
ParameterName("subParam3_valid"),
ParameterName("subParam3"),
FragmentClazzRef[java.lang.String],
initialValue = None,
hintText = None,
valueEditor = Some(
ValueInputWithDictEditor(
dictId = "",
allowOtherValue = false
)
),
valueCompileTimeValidation = None
),
FragmentParameter(
ParameterName("subParam4_valid"),
FragmentClazzRef[java.lang.String],
initialValue =
Some(FixedExpressionValue("""{"key":"some string key","label":"some label"}""", "some label")),
Expand Down Expand Up @@ -660,6 +673,14 @@ class UIProcessValidatorSpec extends AnyFunSuite with Matchers with TableDrivenP
Some("$param.subParam2.$dictId"),
NodeValidationErrorType.SaveAllowed,
None
),
NodeValidationError(
"EmptyMandatoryField",
"This field is mandatory and cannot be empty",
_,
Some("$param.subParam3.$dictId"),
NodeValidationErrorType.SaveAllowed,
None
)
) =>
}
Expand Down
Expand Up @@ -138,37 +138,54 @@ object FragmentParameterValidator {
)(implicit nodeId: NodeId): Validated[NonEmptyList[PartSubGraphCompilationError], Unit] =
fragmentParameter.valueEditor match {
case Some(ValueInputWithDictEditor(dictId, _)) =>
dictionaries.get(dictId) match {
case Some(dictDefinition) =>
val fragmentParameterTypingResult = fragmentParameter.typ
.toRuntimeClass(classLoader)
.map(Typed(_))
.getOrElse(Unknown)
validateNonEmptyDictId(dictId, fragmentParameter.name).andThen(_ =>
dictionaries.get(dictId) match {
case Some(dictDefinition) =>
val fragmentParameterTypingResult = fragmentParameter.typ
.toRuntimeClass(classLoader)
.map(Typed(_))
.getOrElse(Unknown)

val dictValueType = dictDefinition.valueType(dictId)
val dictValueType = dictDefinition.valueType(dictId)

if (dictValueType.canBeSubclassOf(fragmentParameterTypingResult)) {
Valid(())
} else {
if (dictValueType.canBeSubclassOf(fragmentParameterTypingResult)) {
Valid(())
} else {
invalidNel(
DictIsOfInvalidType(
dictId,
dictValueType,
fragmentParameterTypingResult,
nodeId.id,
qualifiedParamFieldName(fragmentParameter.name, Some(DictIdFieldName))
)
)
}
case None =>
invalidNel(
DictIsOfInvalidType(
DictNotDeclared(
dictId,
dictValueType,
fragmentParameterTypingResult,
nodeId.id,
qualifiedParamFieldName(fragmentParameter.name, Some(DictIdFieldName))
)
)
}
case None =>
invalidNel(
DictNotDeclared(dictId, nodeId.id, qualifiedParamFieldName(fragmentParameter.name, Some(DictIdFieldName)))
)
}
}
)

case _ => Valid(())
}

private def validateNonEmptyDictId(dictId: String, parameterName: ParameterName)(implicit nodeId: NodeId) =
if (dictId.isBlank)
invalidNel(
EmptyMandatoryField(
nodeId.id,
qualifiedParamFieldName(parameterName, Some(DictIdFieldName))
)
)
else
valid(())

def validateParameterNames(
parameters: List[Parameter]
)(implicit nodeId: NodeId): ValidatedNel[ProcessCompilationError, Unit] = {
Expand Down

0 comments on commit 45c8ded

Please sign in to comment.