@@ -15,13 +15,14 @@ import {
1515 WithChildrenNode ,
1616 InlineNode ,
1717 NodeType ,
18- Record ,
18+ Record as DatoCmsRecord ,
1919 StructuredText ,
2020 ThematicBreak ,
2121 Document ,
2222} from './types' ;
2323
2424import {
25+ allowedNodeTypes ,
2526 headingNodeType ,
2627 spanNodeType ,
2728 rootNodeType ,
@@ -98,33 +99,50 @@ export function isThematicBreak(node: Node): node is ThematicBreak {
9899 return node . type === thematicBreakNodeType ;
99100}
100101
101- export function isStructuredText < R1 extends Record , R2 extends Record = R1 > (
102- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
103- obj : any ,
104- ) : obj is StructuredText < R1 , R2 > {
105- return obj && 'value' in obj && isDocument ( obj . value ) ;
102+ function isObject ( obj : unknown ) : obj is Record < string , unknown > {
103+ return Boolean ( typeof obj === 'object' && obj ) ;
106104}
107105
108- export function isDocument (
109- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
110- obj : any ,
111- ) : obj is Document {
112- return obj && 'schema' in obj && 'document' in obj ;
106+ export function isNodeType ( value : string ) : value is NodeType {
107+ return allowedNodeTypes . includes ( value as NodeType ) ;
113108}
114109
115- export function isEmptyDocument (
116- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
117- obj : any ,
118- ) : boolean {
110+ export function isNode ( obj : unknown ) : obj is Node {
111+ return Boolean (
112+ isObject ( obj ) &&
113+ 'type' in obj &&
114+ typeof obj . type === 'string' &&
115+ isNodeType ( obj . type ) ,
116+ ) ;
117+ }
118+
119+ export function isStructuredText <
120+ R1 extends DatoCmsRecord ,
121+ R2 extends DatoCmsRecord = R1
122+ > ( obj : unknown ) : obj is StructuredText < R1 , R2 > {
123+ return Boolean ( isObject ( obj ) && 'value' in obj && isDocument ( obj . value ) ) ;
124+ }
125+
126+ export function isDocument ( obj : unknown ) : obj is Document {
127+ return Boolean (
128+ isObject ( obj ) &&
129+ 'schema' in obj &&
130+ 'document' in obj &&
131+ obj . schema === 'dast' ,
132+ ) ;
133+ }
134+
135+ export function isEmptyDocument ( obj : unknown ) : boolean {
119136 if ( ! obj ) {
120137 return true ;
121138 }
122139
123- const document = isStructuredText ( obj )
124- ? obj . value
125- : isDocument ( obj )
126- ? obj
127- : null ;
140+ const document =
141+ isStructuredText ( obj ) && isDocument ( obj . value )
142+ ? obj . value
143+ : isDocument ( obj )
144+ ? obj
145+ : null ;
128146
129147 if ( ! document ) {
130148 throw new Error (
@@ -133,7 +151,6 @@ export function isEmptyDocument(
133151 }
134152
135153 return (
136- document . schema === 'dast' &&
137154 document . document . children . length === 1 &&
138155 document . document . children [ 0 ] . type === 'paragraph' &&
139156 document . document . children [ 0 ] . children . length === 1 &&
0 commit comments