Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add media attribute validator #14670

Merged
merged 5 commits into from Feb 27, 2023
Merged

add media attribute validator #14670

merged 5 commits into from Feb 27, 2023

Conversation

Marc-Roig
Copy link
Contributor

@Marc-Roig Marc-Roig commented Oct 19, 2022

👋🏻 What does it do?

You could create an entity via the REST API with an empty media attribute that was required.
Now it will be required and you will receive a 400 Validation error.

🧪 How to test it?

Very well explained in #14648.

Example Content Type Schema
{
  "kind": "collectionType",
  "info": {
    "displayName": "Test",
    "singularName": "test",
    "pluralName": "tests",
    "description": ""
  },
  "options": {
    "draftAndPublish": false
  },
  "attributes": {
    "name": {
      "type": "string",
    },
    "media": {
      "type": "media",
      "multiple": false,
      "required": true,
      "allowedTypes": ["images", "files", "videos", "audios"]
    },
    "medias": {
      "allowedTypes": ["images", "files", "videos", "audios"],
      "type": "media",
      "multiple": true
    }
  }
}
Example Request POST http://localhost:1337/api/tests
	{
	    "media": 10,
	    "medias": [{"id": 8}],
	    "name": "test"
	}

🔍 Expected behavior

Scenario 1: Single media attribute

  • Given I have a content type with a required media attribute
  • When I use the REST API to create an entity
    And I create it without the media attribute
  • I receive a 400 Validation Error

Scenario 2: Multiple media attribute

  • Given I have a content type with a required multiple media attribute
  • When I use the REST API to create an entity
    And I create it without the multiple media attribute
  • I receive a 400 Validation Error

🔗Related issue(s)/PR(s)

#14648

@Marc-Roig Marc-Roig linked an issue Oct 19, 2022 that may be closed by this pull request
@Marc-Roig Marc-Roig added source: core:strapi Source is core/strapi package pr: fix This PR is fixing a bug labels Oct 19, 2022
@Marc-Roig Marc-Roig added this to the 4.5.0 milestone Oct 19, 2022
@codecov
Copy link

codecov bot commented Oct 19, 2022

Codecov Report

Base: 58.41% // Head: 58.42% // Increases project coverage by +0.01% 🎉

Coverage data is based on head (9f78939) compared to base (85ff7d1).
Patch coverage: 77.77% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14670      +/-   ##
==========================================
+ Coverage   58.41%   58.42%   +0.01%     
==========================================
  Files        1545     1546       +1     
  Lines       38203    38229      +26     
  Branches     7514     7517       +3     
==========================================
+ Hits        22315    22337      +22     
- Misses      13595    13599       +4     
  Partials     2293     2293              
Flag Coverage Δ
back 47.40% <77.77%> (+0.01%) ⬆️
front 66.06% <ø> (+0.01%) ⬆️
unit_back 47.40% <77.77%> (+0.01%) ⬆️
unit_front 66.06% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...core/strapi/lib/services/entity-validator/index.js 90.81% <77.77%> (-0.72%) ⬇️
...es/core/admin/admin/src/pages/HomePage/CloudBox.js 88.23% <0.00%> (ø)
...re/admin/admin/src/pages/HomePage/ContentBlocks.js 72.22% <0.00%> (+1.63%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

jhoward1994
jhoward1994 previously approved these changes Oct 24, 2022
Copy link
Contributor

@jhoward1994 jhoward1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 🙂

@petersg83
Copy link
Contributor

Interesting, I need to take the time to look at it as media are relations and we don't enforce "required" on relations. So I wonder what is currently done with media and what needs to be done.
Example: what happens if I delete the media afterwards?

@Marc-Roig
Copy link
Contributor Author

Interesting, I need to take the time to look at it as media are relations and we don't enforce "required" on relations. So I wonder what is currently done with media and what needs to be done. Example: what happens if I delete the media afterwards?

That is a good point, and I think this is more of a product decision that was taken when we let users select media as required.
RN you can have required media field and delete the media afterwards. That leaves you with an invalid entity I guess. I can test this a little bit more to see what implications it has.

@petersg83 petersg83 added the flag: don't merge This PR should not be merged at the moment label Nov 3, 2022
@Convly Convly modified the milestones: 4.5.0, 4.5.1 Nov 9, 2022
@Marc-Roig Marc-Roig removed this from the 4.5.1 milestone Nov 14, 2022
@Marc-Roig Marc-Roig removed the flag: don't merge This PR should not be merged at the moment label Feb 23, 2023
@Marc-Roig
Copy link
Contributor Author

After further discussions, we have agreed to push this forward in order to be consistent with how the CM works.

@jhoward1994
Copy link
Contributor

I think we should rename the test file now to a test.api.js file

@Marc-Roig
Copy link
Contributor Author

@jhoward1994 good catch

jhoward1994
jhoward1994 previously approved these changes Feb 23, 2023
Copy link
Contributor

@jhoward1994 jhoward1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍🏻

@Marc-Roig Marc-Roig added this to the 4.7.0 milestone Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: fix This PR is fixing a bug source: core:strapi Source is core/strapi package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Required media field is not required in the API.
4 participants