Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String type to slug type for sanity io

I am importing data from an external API into my sanity schema and the API contains the slug in the form of a string. I have tried removing the double quotes of the string and making it single quoted but that is still not accepted by sanity as slug type.

So how do I convert it to the slug type?

code I'm currently using:

const slicedSlug = externalReport.slug.replace(/["]+/g, '');

like image 619
Marium Ali Avatar asked Jan 25 '26 10:01

Marium Ali


1 Answers

The slug is an "object" with _type: 'slug'. In case you were trying to add it as a string, try this:

const slug = {
    _type: 'slug',
    current: 'your-slug-without-quotes',
};

Personally I would also replace spaces and make all lower caps but it might not be relevant to your situation.

yourSlug.replace(/["]+/g, '').replace(/\s+/g, '-').toLowerCase();

like image 175
cfm Avatar answered Jan 26 '26 22:01

cfm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!