@@ -16,7 +16,7 @@ export function isDirective(value: FieldValue): value is Directive {
16
16
value !== null &&
17
17
typeof value === 'object' &&
18
18
Object . keys ( value ) . some ( ( key ) =>
19
- [ '@if' , '@path' , '@template' , '@literal' , '@arrayPath' , '@case' , '@replace' ] . includes ( key )
19
+ [ '@if' , '@path' , '@template' , '@literal' , '@arrayPath' , '@case' , '@replace' , '@json' ] . includes ( key )
20
20
)
21
21
)
22
22
}
@@ -36,7 +36,7 @@ export function isTemplateDirective(value: FieldValue): value is TemplateDirecti
36
36
return isDirective ( value ) && '@template' in value
37
37
}
38
38
39
- export function getFieldValue ( value : FieldValue ) : any {
39
+ export function getFieldValue ( value : FieldValue ) : unknown {
40
40
if ( isTemplateDirective ( value ) ) {
41
41
return value [ '@template' ]
42
42
}
@@ -123,6 +123,24 @@ export function isReplaceDirective(value: FieldValue): value is ReplaceDirective
123
123
'pattern' in value [ '@replace' ]
124
124
)
125
125
}
126
+
127
+ export interface JSONDirective extends DirectiveMetadata {
128
+ '@json' : {
129
+ value : FieldValue
130
+ mode : PrimitiveValue
131
+ }
132
+ }
133
+
134
+ export function isJSONDirective ( value : FieldValue ) : value is JSONDirective {
135
+ return (
136
+ isDirective ( value ) &&
137
+ '@json' in value &&
138
+ value [ '@json' ] !== null &&
139
+ typeof value [ '@json' ] === 'object' &&
140
+ 'value' in value [ '@json' ]
141
+ )
142
+ }
143
+
126
144
type DirectiveKeysToType < T > = {
127
145
[ '@arrayPath' ] : ( input : ArrayPathDirective ) => T
128
146
[ '@case' ] : ( input : CaseDirective ) => T
@@ -131,6 +149,7 @@ type DirectiveKeysToType<T> = {
131
149
[ '@path' ] : ( input : PathDirective ) => T
132
150
[ '@replace' ] : ( input : ReplaceDirective ) => T
133
151
[ '@template' ] : ( input : TemplateDirective ) => T
152
+ [ '@json' ] : ( input : JSONDirective ) => T
134
153
}
135
154
136
155
function directiveType < T > ( directive : Directive , checker : DirectiveKeysToType < T > ) : T | null {
@@ -155,6 +174,9 @@ function directiveType<T>(directive: Directive, checker: DirectiveKeysToType<T>)
155
174
if ( isTemplateDirective ( directive ) ) {
156
175
return checker [ '@template' ] ( directive )
157
176
}
177
+ if ( isJSONDirective ( directive ) ) {
178
+ return checker [ '@json' ] ( directive )
179
+ }
158
180
return null
159
181
}
160
182
@@ -166,6 +188,8 @@ export type Directive =
166
188
| PathDirective
167
189
| ReplaceDirective
168
190
| TemplateDirective
191
+ | JSONDirective
192
+
169
193
export type PrimitiveValue = boolean | number | string | null
170
194
export type FieldValue = Directive | PrimitiveValue | { [ key : string ] : FieldValue } | FieldValue [ ] | undefined
171
195
@@ -188,7 +212,8 @@ export function getFieldValueKeys(value: FieldValue): string[] {
188
212
'@literal' : ( _ : LiteralDirective ) => [ '' ] ,
189
213
'@path' : ( input : PathDirective ) => [ input [ '@path' ] ] ,
190
214
'@replace' : ( input : ReplaceDirective ) => getRawKeys ( input [ '@replace' ] . value ) ,
191
- '@template' : ( input : TemplateDirective ) => getTemplateKeys ( input [ '@template' ] )
215
+ '@template' : ( input : TemplateDirective ) => getTemplateKeys ( input [ '@template' ] ) ,
216
+ '@json' : ( input : JSONDirective ) => getRawKeys ( input [ '@json' ] . value )
192
217
} ) ?. filter ( ( k ) => k ) ?? [ ]
193
218
)
194
219
} else if ( isObject ( value ) ) {
0 commit comments