Contenful Preview API returns draft entries even though they are not valid?

Is there a way to know if a Preview API returned entry is valid or not, if it is not a valid entry, what is the reason? For example, if the entry has all required fields filled or not.

Thank you for your help.

Hi Bwei,

You could associate validations to the fields of the content type that the entry is based on.

Other than that, you could also use the UI extension SDK to try to implement further validation that are not covered by our UI.

Also, you could use the existence parameter to only retrieve fields that have certain fields present in their response.

Let me know if that makes sense or if you have any other questions.

Hi Gabriel,

Thanks for your response. Sorry my question was supposed to be if we are able to know entries accessed through Preview API are valid or not (I updated my question title. Sorry for mistakenly typed Delivery API instead of Preview).

A published entry is guaranteed to be valid, for example, required fields are filled. Therefore, through Delivery API, all returned entries are valid.
However, an entry in draft status is not necessarily valid. Through Preview API, we can get draft entries, no matter if they are valid or not (i.e. some required fields might not have been filled). I am wondering if we are able to know if an entry returned by Preview API is valid?

Thank you.

In this case, the validations are only checked once you actually try to publish the content. You could however try do so through the UI extension, but it’s not something that can be directly achieved in our Web Interface.

Thanks for your reply.

The concern came up when I was trying to set up content preview for one content model with some required fields. When the content entries are returned from Delivery API, the production website would work properly. However, the preview website will crash when some Preview API returned entries miss some required fields.

Since, like you said, the Preview API doesn’t provide validation, we can’t use the same code base for production website and preview site. We need to add some extra validation processes for the preview site. Is there any recommended approach to deal with this?

One of the options here would be to instead use environments for your Preview website, since they’d also rely on publishing for validation, but would effectively a step behind your production website:

Do you think that’d be a feasible implementation for your use case?

I will look into multiple environments.

For now I will add some input validation for Preview API returned entry data.
Do you think it would a good feature to add to Preview API as to indicate whether an entry is potentially valid or not?

Thanks for your help.

Hi @bwei,

Whereas I don’t think that a validation should be enforced at the level of auto-saving, I will indeed send your feedback to our product team to understand whether we could establish another feature to check that a step before publishing (e.g. check validations).

Still, I think this would be a minor use case that could easily be avoided by making good use of our multiple environments capabilities.

Thanks for your help, Gabriel.

No problem at all :slight_smile: - let me know if you have any other questions.

I have this same problem. Is the suggestion above, i.e. to use a separate environment instead of the Contentful preview API, still the recommended solution to solve the issue of the preview API returning invalid draft entries?

If I’m not misunderstanding something, that would mean that editors would need to enter and keep content, content models etc updated in both of the environments to avoid issues.