@@ -39,39 +39,47 @@ function getDraft(drafts: Draft[], $schema: string) {
39
39
}
40
40
41
41
export type Context = {
42
- /** root node of this json-schema */
42
+ /** root node of this JSON Schema */
43
43
rootNode : SchemaNode ;
44
44
/** available draft configurations */
45
45
drafts : Draft [ ] ;
46
- /** [SHARED ACROSS REMOTES] root nodes of registered remote json-schema , stored by id/url */
46
+ /** [SHARED ACROSS REMOTES] root nodes of registered remote JSON Schema , stored by id/url */
47
47
remotes : Record < string , SchemaNode > ;
48
48
/** references stored by fully resolved schema-$id + local-pointer */
49
49
refs : Record < string , SchemaNode > ;
50
50
/** anchors stored by fully resolved schema-$id + $anchor */
51
51
anchors : Record < string , SchemaNode > ;
52
52
/** [SHARED ACROSS REMOTES] dynamicAnchors stored by fully resolved schema-$id + $anchor */
53
53
dynamicAnchors : Record < string , SchemaNode > ;
54
- /** json-schema parser, validator, reducer and resolver for this json-schema (root- schema and its child nodes) */
54
+ /** JSON Schema parser, validator, reducer and resolver for this JSON Schema (root schema and its child nodes) */
55
55
keywords : Draft [ "keywords" ] ;
56
- /** json-schema draft- dependend methods */
56
+ /** JSON Schema draft dependend methods */
57
57
methods : Draft [ "methods" ] ;
58
- /** draft- version */
58
+ /** draft version */
59
59
version : Draft [ "version" ] ;
60
+ /** draft errors & template-strings */
60
61
errors : Draft [ "errors" ] ;
62
+ /** draft formats & validators */
61
63
formats : Draft [ "formats" ] ;
62
64
/** [SHARED USING ADD REMOTE] getData default options */
63
65
templateDefaultOptions ?: TemplateOptions ;
64
66
} ;
65
67
66
68
export interface SchemaNode extends SchemaNodeMethodsType {
67
69
context : Context ;
70
+ /** JSON Schema of node */
68
71
schema : JsonSchema ;
72
+ /** absolute path into JSON Schema, includes $ref for resolved schema */
69
73
spointer : string ;
70
- /** local path within json-schema (not extended by resolving ref) */
74
+ /** local path within JSON Schema (not extended by resolving ref) */
71
75
schemaId : string ;
76
+ /** id created when combining subschemas */
77
+ dynamicId : string ;
78
+ /** reference to parent node (node used to compile this node) */
72
79
parent ?: SchemaNode | undefined ;
73
- /** json-pointer from last $id ~~to this location~~ to resolve $refs to $id#/idLocalPointer */
80
+ /** JSON Pointer from last $id ~~to this location~~ to resolve $refs to $id#/idLocalPointer */
74
81
lastIdPointer : string ;
82
+ /** when reduced schema containing `oneOf` schema, `oneOfIndex` stores `oneOf`-item used for merge */
75
83
oneOfIndex ?: number ;
76
84
77
85
reducers : JsonSchemaReducer [ ] ;
@@ -97,7 +105,7 @@ export interface SchemaNode extends SchemaNodeMethodsType {
97
105
items ?: SchemaNode ;
98
106
not ?: SchemaNode ;
99
107
oneOf ?: SchemaNode [ ] ;
100
- patternProperties ?: { pattern : RegExp ; node : SchemaNode } [ ] ;
108
+ patternProperties ?: { name : string ; pattern : RegExp ; node : SchemaNode } [ ] ;
101
109
properties ?: Record < string , SchemaNode > ;
102
110
propertyNames ?: SchemaNode ;
103
111
then ?: SchemaNode ;
@@ -124,19 +132,41 @@ export type GetSchemaOptions = {
124
132
pointer ?: string ;
125
133
} ;
126
134
135
+ export function joinDynamicId ( a ?: string , b ?: string ) {
136
+ if ( a == b ) {
137
+ return a ?? "" ;
138
+ }
139
+ if ( a == null || b == null ) {
140
+ return a || b ;
141
+ }
142
+ if ( a . startsWith ( b ) ) {
143
+ return a ;
144
+ }
145
+ if ( b . startsWith ( a ) ) {
146
+ return b ;
147
+ }
148
+ return `${ a } +${ b } ` ;
149
+ }
150
+
127
151
export const SchemaNodeMethods = {
128
152
/**
129
153
* Compiles a child-schema of this node to its context
130
154
* @returns SchemaNode representing the passed JSON Schema
131
155
*/
132
- compileSchema ( schema : JsonSchema , spointer : string = this . spointer , schemaId ?: string ) : SchemaNode {
156
+ compileSchema (
157
+ schema : JsonSchema ,
158
+ spointer : string = this . spointer ,
159
+ schemaId ?: string ,
160
+ dynamicId ?: string
161
+ ) : SchemaNode {
133
162
const nextFragment = spointer . split ( "/$ref" ) [ 0 ] ;
134
163
const parentNode = this as SchemaNode ;
135
164
const node : SchemaNode = {
136
165
lastIdPointer : parentNode . lastIdPointer , // ref helper
137
166
context : parentNode . context ,
138
167
parent : parentNode ,
139
168
spointer,
169
+ dynamicId : joinDynamicId ( parentNode . dynamicId , dynamicId ) ,
140
170
schemaId : schemaId ?? join ( parentNode . schemaId , nextFragment ) ,
141
171
reducers : [ ] ,
142
172
resolvers : [ ] ,
@@ -170,9 +200,9 @@ export const SchemaNodeMethods = {
170
200
} ,
171
201
172
202
/**
173
- * Returns a node containing json-schema of a data-json-pointer .
203
+ * Returns a node containing JSON Schema of a data-JSON Pointer .
174
204
*
175
- * To resolve dynamic schema where the type of json-schema is evaluated by
205
+ * To resolve dynamic schema where the type of JSON Schema is evaluated by
176
206
* its value, a data object has to be passed in options.
177
207
*
178
208
* Per default this function will return `undefined` schema for valid properties
@@ -390,7 +420,7 @@ export const SchemaNodeMethods = {
390
420
} ,
391
421
392
422
/**
393
- * Register a json-schema as a remote-schema to be resolved by $ref, $anchor, etc
423
+ * Register a JSON Schema as a remote-schema to be resolved by $ref, $anchor, etc
394
424
* @returns the current node (not the remote schema-node)
395
425
*/
396
426
addRemote ( url : string , schema : JsonSchema ) : SchemaNode {
@@ -403,6 +433,7 @@ export const SchemaNodeMethods = {
403
433
spointer : "#" ,
404
434
lastIdPointer : "#" ,
405
435
schemaId : "#" ,
436
+ dynamicId : "" ,
406
437
reducers : [ ] ,
407
438
resolvers : [ ] ,
408
439
validators : [ ] ,
0 commit comments