@@ -6,13 +6,15 @@ import { toSchemaNodes } from "./methods/toSchemaNodes";
6
6
import { getValue } from "./utils/getValue" ;
7
7
import { isJsonError } from "./types" ;
8
8
import { isObject } from "./utils/isObject" ;
9
- import { join , split } from "@sagold/json-pointer" ;
9
+ import { join } from "@sagold/json-pointer" ;
10
10
import { joinId } from "./utils/joinId" ;
11
11
import { mergeNode } from "./mergeNode" ;
12
12
import { omit } from "./utils/omit" ;
13
13
import { pick } from "./utils/pick" ;
14
14
import { render } from "./errors/render" ;
15
15
import { validateNode } from "./validateNode" ;
16
+ import { hasProperty } from "./utils/hasProperty" ;
17
+ import { getNode } from "./getNode" ;
16
18
const { DYNAMIC_PROPERTIES } = settings ;
17
19
export function isSchemaNode ( value ) {
18
20
return isObject ( value ) && Array . isArray ( value === null || value === void 0 ? void 0 : value . reducers ) && Array . isArray ( value === null || value === void 0 ? void 0 : value . resolvers ) ;
@@ -81,70 +83,34 @@ export const SchemaNodeMethods = {
81
83
return { type : "error" , code, message : errorMessage , data } ;
82
84
} ,
83
85
createSchema,
84
- getChildSchemaSelection ( property ) {
86
+ getChildSelection ( property ) {
85
87
const node = this ;
86
- return node . context . methods . getChildSchemaSelection ( node , property ) ;
87
- } ,
88
- /**
89
- * Returns a node containing JSON Schema of a data JSON Pointer.
90
- *
91
- * To resolve dynamic schema where the type of JSON Schema is evaluated by
92
- * its value, a data object has to be passed in options.
93
- *
94
- * Per default this function will return `undefined` schema for valid properties
95
- * that do not have a defined schema. Use the option `withSchemaWarning: true` to
96
- * receive an error with `code: schema-warning` containing the location of its
97
- * last evaluated json-schema.
98
- *
99
- * @returns { node } or { error } where node can also be undefined (valid but undefined)
100
- */
101
- getNode ( pointer , data , options = { } ) {
102
- var _a , _b , _c ;
103
- options . path = ( _a = options . path ) !== null && _a !== void 0 ? _a : [ ] ;
104
- options . withSchemaWarning = ( _b = options . withSchemaWarning ) !== null && _b !== void 0 ? _b : false ;
105
- options . pointer = ( _c = options . pointer ) !== null && _c !== void 0 ? _c : "#" ;
106
- const node = this ;
107
- const keys = split ( pointer ) ;
108
- if ( keys . length === 0 ) {
109
- const result = node . resolveRef ( options ) ;
110
- return isJsonError ( result ) ? { node : undefined , error : result } : { node : result , error : undefined } ;
111
- }
112
- let currentPointer = "#" ;
113
- let currentNode = node ;
114
- for ( let i = 0 , l = keys . length ; i < l ; i += 1 ) {
115
- currentPointer = `${ currentPointer } /${ keys [ i ] } ` ;
116
- const result = currentNode . getChild ( keys [ i ] , data , { ...options , pointer : currentPointer } ) ;
117
- if ( result . error ) {
118
- return result ;
119
- }
120
- if ( result . node == null ) {
121
- return result ;
122
- }
123
- currentNode = result . node ;
124
- data = getValue ( data , keys [ i ] ) ;
125
- }
126
- const result = currentNode . resolveRef ( options ) ;
127
- return isJsonError ( result ) ? { node : undefined , error : result } : { node : result , error : undefined } ;
88
+ return node . context . methods . getChildSelection ( node , property ) ;
128
89
} ,
90
+ getNode,
129
91
/**
130
92
* @returns for $ref, the corresponding SchemaNode or undefined
131
93
*/
132
- getRef ( $ref ) {
94
+ getNodeRef ( $ref ) {
133
95
const node = this ;
134
96
return node . compileSchema ( { $ref } ) . resolveRef ( ) ;
135
97
} ,
98
+ getNodeRoot ( ) {
99
+ const node = this ;
100
+ return node . context . rootNode ;
101
+ } ,
136
102
/**
137
103
* @returns child node identified by property as SchemaNode
138
104
*/
139
- getChild ( key , data , options = { } ) {
105
+ getNodeChild ( key , data , options = { } ) {
140
106
var _a , _b , _c ;
141
107
options . path = ( _a = options . path ) !== null && _a !== void 0 ? _a : [ ] ;
142
108
options . withSchemaWarning = ( _b = options . withSchemaWarning ) !== null && _b !== void 0 ? _b : false ;
143
109
options . pointer = ( _c = options . pointer ) !== null && _c !== void 0 ? _c : "#" ;
144
110
const { path, pointer } = options ;
145
111
let node = this ;
146
112
if ( node . reducers . length ) {
147
- const result = node . reduceSchema ( data , { key, path, pointer } ) ;
113
+ const result = node . reduceNode ( data , { key, path, pointer } ) ;
148
114
if ( result . error ) {
149
115
return result ;
150
116
}
@@ -163,7 +129,7 @@ export const SchemaNodeMethods = {
163
129
}
164
130
const referencedNode = node . resolveRef ( { path } ) ;
165
131
if ( referencedNode !== node ) {
166
- return referencedNode . getChild ( key , data , options ) ;
132
+ return referencedNode . getNodeChild ( key , data , options ) ;
167
133
}
168
134
if ( options . createSchema === true ) {
169
135
const newNode = node . compileSchema ( createSchema ( getValue ( data , key ) ) , `${ node . spointer } /additional` , `${ node . schemaId } /additional` ) ;
@@ -197,7 +163,7 @@ export const SchemaNodeMethods = {
197
163
/**
198
164
* @returns SchemaNode with a reduced JSON Schema matching the given data
199
165
*/
200
- reduceSchema ( data , options = { } ) {
166
+ reduceNode ( data , options = { } ) {
201
167
const node = this ;
202
168
const { key, pointer, path } = options ;
203
169
// @ts -expect-error bool schema
@@ -273,7 +239,7 @@ export const SchemaNodeMethods = {
273
239
* Register a JSON Schema as a remote-schema to be resolved by $ref, $anchor, etc
274
240
* @returns the current node (not the remote schema-node)
275
241
*/
276
- addRemote ( url , schema ) {
242
+ addRemoteSchema ( url , schema ) {
277
243
var _a ;
278
244
// @draft >= 6
279
245
schema . $id = joinId ( schema . $id || url ) ;
0 commit comments