Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add extra validation, update translations

Piotr Grundas5e9bb6f96ead
diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.py
index 123de77cfd..6002d9ff4e 100644
--- a/saleor/graphql/attribute/utils.py
+++ b/saleor/graphql/attribute/utils.py
@@ -494,6 +494,8 @@ def validate_attributes_input(
validate_file_attributes_input(*attrs)
elif attribute.input_type == AttributeInputType.REFERENCE:
validate_reference_attributes_input(*attrs)
+ elif attribute.input_type == AttributeInputType.RICH_TEXT:
+ validate_rich_text_attributes_input(*attrs)
# validation for other input types
else:
validate_standard_attributes_input(*attrs)
@@ -545,6 +547,19 @@ def validate_reference_attributes_input(
)
+def validate_rich_text_attributes_input(
+ attribute: "Attribute",
+ attr_values: "AttrValuesInput",
+ attribute_errors: T_ERROR_DICT,
+ variant_validation: bool,
+):
+ attribute_id = attr_values.global_id
+ text = clean_editor_js(attr_values.rich_text or {}, to_string=True)
+
+ if not text.strip() and attribute.value_required:
+ attribute_errors[AttributeInputErrors.ERROR_NO_VALUE_GIVEN].append(attribute_id)
+
+
def validate_standard_attributes_input(
attribute: "Attribute",
attr_values: "AttrValuesInput",
diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.py
index 0017458d15..57e1601ad3 100644
--- a/saleor/graphql/product/tests/test_variant.py
+++ b/saleor/graphql/product/tests/test_variant.py
@@ -984,6 +984,7 @@ def test_create_variant_invalid_variant_attributes(
warehouse,
color_attribute,
weight_attribute,
+ rich_text_attribute,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -1006,6 +1007,12 @@ def test_create_variant_invalid_variant_attributes(
product_type.variant_attributes.add(weight_attribute)
weight_attr_id = graphene.Node.to_global_id("Attribute", weight_attribute.id)
+ # Add fourth attribute
+ rich_text_attribute.value_required = True
+ rich_text_attribute.save()
+ product_type.variant_attributes.add(rich_text_attribute)
+ rich_text_attr_id = graphene.Node.to_global_id("Attribute", rich_text_attribute.id)
+
stocks = [
{
"warehouse": graphene.Node.to_global_id("Warehouse", warehouse.pk),
@@ -1024,6 +1031,7 @@ def test_create_variant_invalid_variant_attributes(
{"id": color_attr_id, "values": [" "]},
{"id": weight_attr_id, "values": [None]},
{"id": size_attr_id, "values": [non_existent_attr_value, size_value_slug]},
+ {"id": rich_text_attr_id, "richText": json.dumps(dummy_editorjs(" "))},
],
"trackInventory": True,
}
@@ -1036,7 +1044,7 @@ def test_create_variant_invalid_variant_attributes(
errors = data["productErrors"]
assert not data["productVariant"]
- assert len(errors) == 2
+ assert len(errors) == 3
expected_errors = [
{
@@ -1051,6 +1059,12 @@ def test_create_variant_invalid_variant_attributes(
"field": "attributes",
"message": ANY,
},
+ {
+ "attributes": [rich_text_attr_id],
+ "code": ProductErrorCode.REQUIRED.name,
+ "field": "attributes",
+ "message": ANY,
+ },
]
for error in expected_errors:
assert error in errors
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index ccbe80b12d..e716113920 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -636,6 +636,11 @@ type AttributeValueTranslation implements Node {
language: LanguageDisplay!
}
+input AttributeValueTranslationInput {
+ name: String
+ richText: JSONString
+}
+
type AttributeValueUpdate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes. This field will be removed after 2020-07-31.")
attribute: Attribute
@@ -2814,7 +2819,7 @@ type Mutation {
attributeValueCreate(attribute: ID!, input: AttributeValueCreateInput!): AttributeValueCreate
attributeValueDelete(id: ID!): AttributeValueDelete
attributeValueUpdate(id: ID!, input: AttributeValueCreateInput!): AttributeValueUpdate
- attributeValueTranslate(id: ID!, input: NameTranslationInput!, languageCode: LanguageCodeEnum!): AttributeValueTranslate
+ attributeValueTranslate(id: ID!, input: AttributeValueTranslationInput!, languageCode: LanguageCodeEnum!): AttributeValueTranslate
attributeReorderValues(attributeId: ID!, moves: [ReorderInput]!): AttributeReorderValues
appCreate(input: AppInput!): AppCreate
appUpdate(id: ID!, input: AppInput!): AppUpdate
diff --git a/saleor/graphql/translations/mutations.py b/saleor/graphql/translations/mutations.py
index b88fad4673..0dac66ca78 100644
--- a/saleor/graphql/translations/mutations.py
+++ b/saleor/graphql/translations/mutations.py
@@ -44,6 +44,10 @@ class NameTranslationInput(graphene.InputObjectType):
name = graphene.String()
+class AttributeValueTranslationInput(NameTranslationInput):
+ rich_text = graphene.JSONString()
+
+
class SeoTranslationInput(graphene.InputObjectType):
seo_title = graphene.String()
seo_description = graphene.String()
@@ -181,7 +185,7 @@ class AttributeValueTranslate(BaseTranslateMutation):
language_code = graphene.Argument(
LanguageCodeEnum, required=True, description="Translation language code."
)
- input = NameTranslationInput(required=True)
+ input = AttributeValueTranslationInput(required=True)
class Meta:
description = "Creates/Updates translations for attribute value."