@@ -3,17 +3,17 @@ import { ContextVariablesSchemas } from "./functions/context";
33import { FunctionDescriptor } from "./functions/types" ;
44
55export class ParseContext {
6- paths ?: Record < string , TypeSchema > = { } ;
7- additionalContext ?: Record < string , TypeSchema > ;
8- knownVariables : Set < string > ;
6+ private readonly paths ?: Record < string , TypeSchema > ;
7+ private readonly additionalContext ?: Record < string , TypeSchema > ;
8+ private knownVariables : Set < string > ;
99
1010 constructor (
1111 paths ?: Record < string , TypeSchema > ,
1212 additionalContext ?: Record < string , TypeSchema > ,
1313 previousPaths ?: string [ ] ,
1414 ) {
1515 this . paths = paths ;
16- this . additionalContext = additionalContext ?? ContextVariablesSchemas ;
16+ this . additionalContext = additionalContext ;
1717 this . knownVariables = new Set ( ) ;
1818 if ( paths ) {
1919 for ( const path in paths ) {
@@ -35,14 +35,46 @@ export class ParseContext {
3535 }
3636 }
3737
38+ hasPaths ( ) {
39+ return Boolean ( this . paths ) ;
40+ }
41+
42+ hasPath ( path : string ) {
43+ return typeof this . paths ?. [ path ] !== "undefined" ;
44+ }
45+
46+ /**
47+ * If you are about to change the result, use this and not 'resolve()'
48+ * @param path
49+ */
50+ getPath ( path : string ) {
51+ return this . paths ?. [ path ] ;
52+ }
53+
54+ setPath ( path : string , type : TypeSchema ) {
55+ if ( ! this . paths ) return ;
56+ this . paths [ path ] = type ;
57+ const v = path . split ( / [ . [ ] / , 1 ) [ 0 ] ;
58+ this . knownVariables . add ( v ) ;
59+ }
60+
61+ removePaths ( paths : string [ ] , variableToRemove ?: string ) {
62+ for ( const path of paths ) {
63+ delete this . paths ?. [ path ] ;
64+ }
65+ if ( variableToRemove ) {
66+ this . knownVariables . delete ( variableToRemove ) ;
67+ }
68+ }
69+
3870 resolve ( key : string ) {
39- return this . additionalContext ?. [ key ] ?? this . paths ?. [ key ] ;
71+ return ContextVariablesSchemas [ key ] ?? this . additionalContext ?. [ key ] ?? this . paths ?. [ key ] ;
4072 }
4173
42- isJsonPathReference ( path : any ) : boolean {
43- if ( typeof path !== "string" ) return false ;
74+ isReferencingKnownVariable ( path : any ) : boolean {
75+ if ( typeof path !== "string" || path . startsWith ( "$$" ) ) return false ;
4476 const v = path . split ( / [ . [ ] / , 1 ) [ 0 ] ;
45- return path === v || path . startsWith ( v + "." ) || path . startsWith ( v + "[" ) ;
77+ return this . knownVariables . has ( v ) && ( path === v || path . startsWith ( v + "." ) || path . startsWith ( v + "[" ) ) ;
4678 }
4779}
4880
0 commit comments