Skip to content
Snippets Groups Projects
Commit 9c971b14 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Support multiline strings in user configurations

parent 9eba7b3a
No related branches found
No related tags found
1 merge request!2496Support multiline strings in user configurations
......@@ -169,11 +169,12 @@ class UserConfigurationFieldSerializer(serializers.Serializer):
subtype = EnumField(UserConfigurationFieldType, required=False)
required = serializers.BooleanField(default=False)
choices = serializers.ListField(required=False, allow_empty=False, allow_null=True)
multiline = serializers.BooleanField(required=False)
def to_internal_value(self, data):
errors = defaultdict(list)
allowed_fields = ["title", "type", "required", "default", "choices", "subtype"]
allowed_fields = ("title", "type", "required", "default", "choices", "subtype", "multiline")
if not isinstance(data, dict):
errors["__all__"] = [f"User configuration field definitions should be of type dict, not {type(data).__name__}."]
......@@ -182,7 +183,7 @@ class UserConfigurationFieldSerializer(serializers.Serializer):
for field in data:
if field not in allowed_fields:
errors[field].append(
"Configurable properties can only be defined using the following keys: title, type, required, default, subtype, choices."
f"Configurable properties can only be defined using the following keys: {', '.join(allowed_fields)}."
)
default_value = data.get("default")
......@@ -194,12 +195,14 @@ class UserConfigurationFieldSerializer(serializers.Serializer):
if field_type == UserConfigurationFieldType.List and not subtype:
errors["subtype"].append('The "subtype" field must be set for "list" type properties.')
# Handle subtypes
if subtype is not None:
if field_type != UserConfigurationFieldType.List:
errors["subtype"].append('The "subtype" field can only be set for a "list" type property.')
if subtype not in [UserConfigurationFieldType.Int, UserConfigurationFieldType.Float, UserConfigurationFieldType.String, UserConfigurationFieldType.Boolean]:
errors["subtype"].append("Subtype can only be int, float, bool or string.")
# Handle enums
if choices is not None:
if field_type != UserConfigurationFieldType.Enum:
......@@ -207,6 +210,11 @@ class UserConfigurationFieldSerializer(serializers.Serializer):
# If the configuration parameter is of enum type, an eventual default value won't match the field type
if default_value and default_value not in choices:
errors["default"].append(f"{default_value} is not an available choice.")
# Only allow the multiline property on string fields
if "multiline" in data and field_type != UserConfigurationFieldType.String:
errors["multiline"].append('The "multiline" field can only be set for a "string" type property.')
# Handle everything else
if default_value is not None and field_type != UserConfigurationFieldType.Enum:
try:
......@@ -278,17 +286,16 @@ class WorkerVersionSerializer(serializers.ModelSerializer):
return tag
def validate_configuration(self, configuration):
errors = defaultdict(list)
user_configuration = configuration.get("user_configuration")
if not user_configuration:
return configuration
field = serializers.DictField(child=UserConfigurationFieldSerializer())
try:
field.to_internal_value(user_configuration)
except ValidationError as e:
errors["user_configuration"].append(e.detail)
if errors:
raise ValidationError(errors)
raise ValidationError({"user_configuration": e.detail})
return configuration
def validate(self, data):
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment