@@ -31,16 +31,35 @@ function getDraft(drafts, $schema) {
31
31
var _a ;
32
32
return ( _a = drafts . find ( ( d ) => new RegExp ( d . $schemaRegEx ) . test ( $schema ) ) ) !== null && _a !== void 0 ? _a : drafts [ drafts . length - 1 ] ;
33
33
}
34
+ export function joinDynamicId ( a , b ) {
35
+ if ( a == b ) {
36
+ return a !== null && a !== void 0 ? a : "" ;
37
+ }
38
+ if ( a == null || b == null ) {
39
+ return a || b ;
40
+ }
41
+ if ( a . startsWith ( b ) ) {
42
+ return a ;
43
+ }
44
+ if ( b . startsWith ( a ) ) {
45
+ return b ;
46
+ }
47
+ return `${ a } +${ b } ` ;
48
+ }
34
49
export const SchemaNodeMethods = {
35
- /** Compiles a child-schema of this node to its context */
36
- compileSchema ( schema , spointer = this . spointer , schemaId ) {
50
+ /**
51
+ * Compiles a child-schema of this node to its context
52
+ * @returns SchemaNode representing the passed JSON Schema
53
+ */
54
+ compileSchema ( schema , spointer = this . spointer , schemaId , dynamicId ) {
37
55
const nextFragment = spointer . split ( "/$ref" ) [ 0 ] ;
38
56
const parentNode = this ;
39
57
const node = {
40
58
lastIdPointer : parentNode . lastIdPointer , // ref helper
41
59
context : parentNode . context ,
42
60
parent : parentNode ,
43
61
spointer,
62
+ dynamicId : joinDynamicId ( parentNode . dynamicId , dynamicId ) ,
44
63
schemaId : schemaId !== null && schemaId !== void 0 ? schemaId : join ( parentNode . schemaId , nextFragment ) ,
45
64
reducers : [ ] ,
46
65
resolvers : [ ] ,
@@ -60,17 +79,17 @@ export const SchemaNodeMethods = {
60
79
}
61
80
errorMessage = render ( error !== null && error !== void 0 ? error : name , data ) ;
62
81
}
63
- return { type : "error" , name , code : dashCase ( name ) , message : errorMessage , data } ;
82
+ return { type : "error" , code : dashCase ( name ) , message : errorMessage , data } ;
64
83
} ,
65
84
createSchema,
66
85
getChildSchemaSelection ( property ) {
67
86
const node = this ;
68
87
return node . context . methods . getChildSchemaSelection ( node , property ) ;
69
88
} ,
70
89
/**
71
- * Returns a node containing json-schema of a data-json-pointer .
90
+ * Returns a node containing JSON Schema of a data-JSON Pointer .
72
91
*
73
- * To resolve dynamic schema where the type of json-schema is evaluated by
92
+ * To resolve dynamic schema where the type of JSON Schema is evaluated by
74
93
* its value, a data object has to be passed in options.
75
94
*
76
95
* Per default this function will return `undefined` schema for valid properties
@@ -115,10 +134,16 @@ export const SchemaNodeMethods = {
115
134
const result = currentNode . resolveRef ( options ) ;
116
135
return isJsonError ( result ) ? { node : undefined , error : result } : { node : result , error : undefined } ;
117
136
} ,
137
+ /**
138
+ * @returns for $ref, the corresponding SchemaNode or undefined
139
+ */
118
140
getRef ( $ref ) {
119
141
const node = this ;
120
142
return node . compileSchema ( { $ref } ) . resolveRef ( ) ;
121
143
} ,
144
+ /**
145
+ * @returns child node identified by property as SchemaNode
146
+ */
122
147
getChild ( key , data , options = { } ) {
123
148
var _a , _b , _c ;
124
149
options . path = ( _a = options . path ) !== null && _a !== void 0 ? _a : [ ] ;
@@ -158,10 +183,15 @@ export const SchemaNodeMethods = {
158
183
}
159
184
return { node : undefined , error : undefined } ;
160
185
} ,
186
+ /**
187
+ * @returns draft version this JSON Schema is evaluated by
188
+ */
161
189
getDraftVersion ( ) {
162
190
return this . context . version ;
163
191
} ,
164
- /** Creates data that is valid to the schema of this node */
192
+ /**
193
+ * @returns data that is valid to the schema of this node
194
+ */
165
195
getData ( data , options ) {
166
196
const node = this ;
167
197
const opts = {
@@ -172,10 +202,12 @@ export const SchemaNodeMethods = {
172
202
} ;
173
203
return node . context . methods . getData ( node , data , opts ) ;
174
204
} ,
205
+ /**
206
+ * @returns SchemaNode with a reduced JSON Schema matching the given data
207
+ */
175
208
reduceSchema ( data , options = { } ) {
209
+ const node = this ;
176
210
const { key, pointer, path } = options ;
177
- const resolvedNode = { ...this . resolveRef ( { pointer, path } ) } ;
178
- const node = mergeNode ( this , resolvedNode , "$ref" ) ;
179
211
// @ts -expect-error bool schema
180
212
if ( node . schema === false ) {
181
213
return { node, error : undefined } ;
@@ -187,10 +219,12 @@ export const SchemaNodeMethods = {
187
219
return { node : nextNode , error : undefined } ;
188
220
}
189
221
let schema ;
190
- let workingNode = node ;
222
+ // we need to copy node to prevent modification of source
223
+ // @todo does mergeNode break immutability?
224
+ let workingNode = node . compileSchema ( node . schema , node . spointer , node . schemaId ) ;
191
225
const reducers = node . reducers ;
192
226
for ( let i = 0 ; i < reducers . length ; i += 1 ) {
193
- const result = reducers [ i ] ( { data, key, node, pointer } ) ;
227
+ const result = reducers [ i ] ( { data, key, node, pointer, path } ) ;
194
228
if ( isJsonError ( result ) ) {
195
229
return { node : undefined , error : result } ;
196
230
}
@@ -206,7 +240,6 @@ export const SchemaNodeMethods = {
206
240
}
207
241
}
208
242
if ( schema === false ) {
209
- console . log ( "return boolean schema `false`" ) ;
210
243
// @ts -expect-error bool schema
211
244
return { node : { ...node , schema : false , reducers : [ ] } , error : undefined } ;
212
245
}
@@ -219,7 +252,9 @@ export const SchemaNodeMethods = {
219
252
DYNAMIC_PROPERTIES . forEach ( ( prop ) => ( workingNode [ prop ] = undefined ) ) ;
220
253
return { node : workingNode , error : undefined } ;
221
254
} ,
222
- /** Creates a new node with all dynamic schema properties merged according to the passed in data */
255
+ /**
256
+ * @returns validation result of data validated by this node's JSON Schema
257
+ */
223
258
validate ( data , pointer = "#" , path = [ ] ) {
224
259
var _a ;
225
260
const errors = ( _a = validateNode ( this , data , pointer , path ) ) !== null && _a !== void 0 ? _a : [ ] ;
@@ -229,6 +264,9 @@ export const SchemaNodeMethods = {
229
264
errors : flatErrorList
230
265
} ;
231
266
} ,
267
+ /**
268
+ * @returns a promise which resolves to validation-result
269
+ */
232
270
async validateAsync ( data , pointer = "#" , path = [ ] ) {
233
271
var _a ;
234
272
const errors = ( _a = validateNode ( this , data , pointer , path ) ) !== null && _a !== void 0 ? _a : [ ] ;
@@ -240,7 +278,7 @@ export const SchemaNodeMethods = {
240
278
} ;
241
279
} ,
242
280
/**
243
- * Register a json-schema as a remote-schema to be resolved by $ref, $anchor, etc
281
+ * Register a JSON Schema as a remote-schema to be resolved by $ref, $anchor, etc
244
282
* @returns the current node (not the remote schema-node)
245
283
*/
246
284
addRemote ( url , schema ) {
@@ -253,6 +291,7 @@ export const SchemaNodeMethods = {
253
291
spointer : "#" ,
254
292
lastIdPointer : "#" ,
255
293
schemaId : "#" ,
294
+ dynamicId : "" ,
256
295
reducers : [ ] ,
257
296
resolvers : [ ] ,
258
297
validators : [ ] ,
@@ -270,9 +309,15 @@ export const SchemaNodeMethods = {
270
309
addKeywords ( node ) ;
271
310
return this ;
272
311
} ,
312
+ /**
313
+ * @returns a list of all sub-schema as SchemaNode
314
+ */
273
315
toSchemaNodes ( ) {
274
316
return toSchemaNodes ( this ) ;
275
317
} ,
318
+ /**
319
+ * @returns a list of values (including objects and arrays) and their corresponding JSON Schema as SchemaNode
320
+ */
276
321
toDataNodes ( data , pointer ) {
277
322
const node = this ;
278
323
return node . context . methods . toDataNodes ( node , data , pointer ) ;
0 commit comments