Merge "Split parameter validation off as separate methods"

This commit is contained in:
jenkins-bot 2022-12-13 21:09:37 +00:00 committed by Gerrit Code Review
commit e1b5970d3f

View file

@ -118,12 +118,60 @@ class TemplateDataValidator {
// to decide whether 'inherits' will add it or not. // to decide whether 'inherits' will add it or not.
$unnormalizedParams = unserialize( serialize( $data->params ) ); $unnormalizedParams = unserialize( serialize( $data->params ) );
$status = $this->validateParameters( $data->params );
if ( $status ) {
return $status;
}
foreach ( $data->params as $paramName => $param ) { foreach ( $data->params as $paramName => $param ) {
if ( isset( $param->inherits ) ) {
$parentParam = $data->params->{ $param->inherits };
foreach ( $parentParam as $key => $value ) {
if ( !isset( $unnormalizedParams->$paramName->$key ) ) {
$param->$key = is_object( $value ) ? clone $value : $value;
}
}
unset( $param->inherits );
}
}
return $this->validateParameterOrder( $data->paramOrder ?? null, $data->params ) ??
$this->validateSets( $data->sets, $data->params ) ??
$this->validateMaps( $data->maps, $data->params ) ??
Status::newGood();
}
/**
* @param stdClass $params
* @return Status|null Null on success, otherwise a Status object with the error message
*/
private function validateParameters( stdClass $params ): ?Status {
foreach ( $params as $paramName => $param ) {
if ( !( $param instanceof stdClass ) ) { if ( !( $param instanceof stdClass ) ) {
return Status::newFatal( 'templatedata-invalid-type', "params.{$paramName}", return Status::newFatal( 'templatedata-invalid-type', "params.{$paramName}",
'object' ); 'object' );
} }
$status = $this->validateParameter( $paramName, $param );
if ( $status ) {
return $status;
}
if ( isset( $param->inherits ) && !isset( $params->{ $param->inherits } ) ) {
return Status::newFatal( 'templatedata-invalid-missing',
"params.{$param->inherits}" );
}
}
return null;
}
/**
* @param string $paramName
* @param stdClass $param
* @return Status|null Null on success, otherwise a Status object with the error message
*/
private function validateParameter( string $paramName, stdClass $param ): ?Status {
foreach ( $param as $key => $value ) { foreach ( $param as $key => $value ) {
if ( !in_array( $key, self::VALID_PARAM_KEYS ) ) { if ( !in_array( $key, self::VALID_PARAM_KEYS ) ) {
return Status::newFatal( 'templatedata-invalid-unknown', return Status::newFatal( 'templatedata-invalid-unknown',
@ -269,30 +317,8 @@ class TemplateDataValidator {
} else { } else {
$param->suggestedvalues = []; $param->suggestedvalues = [];
} }
}
// Param.inherits return null;
// Done afterwards to avoid code duplication
foreach ( $data->params as $paramName => $param ) {
if ( isset( $param->inherits ) ) {
if ( !isset( $data->params->{ $param->inherits } ) ) {
return Status::newFatal( 'templatedata-invalid-missing',
"params.{$param->inherits}" );
}
$parentParam = $data->params->{ $param->inherits };
foreach ( $parentParam as $key => $value ) {
if ( !isset( $unnormalizedParams->$paramName->$key ) ) {
$param->$key = is_object( $value ) ? clone $value : $value;
}
}
unset( $param->inherits );
}
}
return $this->validateParameterOrder( $data->paramOrder ?? null, $data->params ) ??
$this->validateSets( $data->sets, $data->params ) ??
$this->validateMaps( $data->maps, $data->params ) ??
Status::newGood();
} }
/** /**