Netlify-cms: Add custom validation logic to existing or default widgets.

Created on 9 Oct 2018  路  4Comments  路  Source: netlify/netlify-cms

Is your feature request related to a problem? Please describe.

This is the current problem we keep running into.

Someone uploads a product to the CMS. On the actual site it will match up the ID of the product to an ID in an api that contains all the product data. We would like to validate that all the data we want is there and if it is not show an error.

What happens is a person loads a product into the CMS, in the api it is missing some content (lets say images). Now on the site this product does not show any images, a complaint happens and then we are stuck emailing other people to get the image into the api. It becomes a three day process.

Describe the solution you'd like

It would be nice if when the user adds a product in the CMS we could do some custom validation like:

onValidate = async (productId) => { 
 const res = await productAPI(productId)
 if(!res.images) return {error: "No Images will be shown"}
}

This is just one example of being able to have custom validation. Again this is for the default widgets that we want this for. So this may trigger as soon as a user were to put in the ID field of a product.

Describe alternatives you've considered

We have considered just essentially copying the default widgets and adding the isValid method to do this, but that seems a bit excessive.

stale

Most helpful comment

Me and another dev are going to do a PR for this and try to get it working :)

All 4 comments

Me and another dev are going to do a PR for this and try to get it working :)

That'd be awesome 馃憤

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This is possible by using a custom widget with an isValid method: https://www.netlifycms.org/docs/custom-widgets/#advanced-field-validation

Was this page helpful?
0 / 5 - 0 ratings