How can I check if a Structured Text field is blank

I can see the query filter to remove items if the structured text field is blank. But is there a way to know in code if it’s empty or not.

My usecase is this: I have a MDX field and a structured text field, and not all content fields has been migrated to the structured text, so I need to display the content if the structured text field is not available.

Is there a way to achieve this?

Hello @marvin.kome

As you said, the query parameter can eliminate blank Structured Text from being fetched on the graphQL end.

There is this PR here that offers a helper function to verify if a structured text is or isn’t empty on code, and i think it is exactly what you are looking for: Create isEmptyOrInvalid helper function by thomasverleye · Pull Request #22 · datocms/structured-text · GitHub

What is the current best way to do this? it seems isEmptyDocument from structured-text/packages/utils/src/guards.ts at main · datocms/structured-text · GitHub

is not able to be imported…

Hey @brian1,

Could you please import from datocms-structured-text-utils instead? It’s in a different package: structured-text/packages/utils at main · datocms/structured-text · GitHub

Full example:

import {isEmptyDocument} from 'datocms-structured-text-utils'

const graphQLResponse = {
    "data": {
        "post": {
            "structuredText": {
                "value": {
                    "schema": "dast",
                    "document": {
                        "type": "root",
                        "children": [
                            {
                                "type": "paragraph",
                                "children": [
                                    {
                                        "type": "span",
                                        "value": ""
                                    }
                                ]
                            }
                        ]
                    }
                }
            }
        }
    }
}

const structuredTextField = graphQLResponse.data.post.structuredText;

console.log(isEmptyDocument(structuredTextField)); // returns true
1 Like

Fantactic. Thank you @roger !

1 Like