shinkansen-transmission 2.3.5 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.mts +22 -22
- package/package.json +2 -2
- package/src/transmission/common/index.d.mts +30 -38
- package/src/transmission/common/index.mjs +76 -196
- package/src/transmission/common/transform/index.d.mts +18 -0
- package/src/transmission/common/transform/index.mjs +128 -0
- package/src/transmission/from-document-to-hash/array/index.d.mts +1 -0
- package/src/transmission/from-document-to-hash/array/index.mjs +1 -46
- package/src/transmission/from-document-to-hash/boolean/index.d.mts +5 -0
- package/src/transmission/from-document-to-hash/boolean/index.mjs +7 -7
- package/src/transmission/from-document-to-hash/index.d.mts +10 -3
- package/src/transmission/from-document-to-hash/index.mjs +121 -60
- package/src/transmission/from-document-to-hash/null/index.d.mts +5 -0
- package/src/transmission/from-document-to-hash/null/index.mjs +7 -7
- package/src/transmission/from-document-to-hash/number/index.d.mts +5 -0
- package/src/transmission/from-document-to-hash/number/index.mjs +7 -7
- package/src/transmission/from-document-to-hash/object/index.d.mts +1 -0
- package/src/transmission/from-document-to-hash/object/index.mjs +1 -46
- package/src/transmission/from-document-to-hash/string/index.d.mts +5 -0
- package/src/transmission/from-document-to-hash/string/index.mjs +8 -8
- package/src/transmission/from-hash-to-document/array/index.d.mts +4 -0
- package/src/transmission/from-hash-to-document/array/index.mjs +4 -42
- package/src/transmission/from-hash-to-document/boolean/index.d.mts +8 -0
- package/src/transmission/from-hash-to-document/boolean/index.mjs +84 -3
- package/src/transmission/from-hash-to-document/index.d.mts +28 -21
- package/src/transmission/from-hash-to-document/index.mjs +184 -352
- package/src/transmission/from-hash-to-document/null/index.d.mts +8 -0
- package/src/transmission/from-hash-to-document/null/index.mjs +84 -3
- package/src/transmission/from-hash-to-document/number/index.d.mts +8 -0
- package/src/transmission/from-hash-to-document/number/index.mjs +84 -3
- package/src/transmission/from-hash-to-document/object/index.d.mts +4 -0
- package/src/transmission/from-hash-to-document/object/index.mjs +4 -42
- package/src/transmission/from-hash-to-document/string/index.d.mts +8 -0
- package/src/transmission/from-hash-to-document/string/index.mjs +85 -4
@@ -4,7 +4,7 @@
|
|
4
4
|
* @typedef {TransmissionTypes.ArrayLiteralType} ArrayLiteralType
|
5
5
|
* @typedef {TransmissionTypes.ArrayType} ArrayType
|
6
6
|
*
|
7
|
-
* @typedef {TransmissionTypes.
|
7
|
+
* @typedef {TransmissionTypes.MemberArrayType} MemberArrayType
|
8
8
|
*
|
9
9
|
* @typedef {TransmissionTypes.DocumentType} DocumentType
|
10
10
|
* @typedef {TransmissionTypes.SchemaType} SchemaType
|
@@ -16,7 +16,6 @@ import debug from 'debug'
|
|
16
16
|
import {
|
17
17
|
isArray,
|
18
18
|
isObject,
|
19
|
-
isPrimitive,
|
20
19
|
hasEnum,
|
21
20
|
getEnum,
|
22
21
|
hasAnyOf,
|
@@ -27,96 +26,57 @@ import {
|
|
27
26
|
transformToValue
|
28
27
|
} from '#transmission/transmission/common'
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
/**
|
35
|
-
* @param {unknown} v
|
36
|
-
* @returns {null}
|
37
|
-
*/
|
38
|
-
export function toNull (v) {
|
39
|
-
if (v === null || v === 'null') return null
|
40
|
-
|
41
|
-
throw new Error('Invalid `null`')
|
42
|
-
}
|
29
|
+
import {
|
30
|
+
fromHashToArray
|
31
|
+
} from '#transmission/transmission/common/transform'
|
43
32
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
*/
|
48
|
-
export function toBoolean (v) {
|
49
|
-
if (typeof v === 'boolean') return v
|
50
|
-
if (v === 'true') return true
|
51
|
-
if (v === 'false') return false
|
33
|
+
import {
|
34
|
+
transformString
|
35
|
+
} from './string/index.mjs'
|
52
36
|
|
53
|
-
|
54
|
-
|
37
|
+
import {
|
38
|
+
transformNumber
|
39
|
+
} from './number/index.mjs'
|
55
40
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
*/
|
60
|
-
export function toString (v) {
|
61
|
-
if (typeof v === 'string') return v
|
62
|
-
if (typeof v === 'number') return String(v)
|
41
|
+
import {
|
42
|
+
transformBoolean
|
43
|
+
} from './boolean/index.mjs'
|
63
44
|
|
64
|
-
|
45
|
+
import {
|
46
|
+
transformNull
|
47
|
+
} from './null/index.mjs'
|
48
|
+
|
49
|
+
export { default as transformStringSchema } from './string/index.mjs'
|
50
|
+
export { default as transformNumberSchema } from './number/index.mjs'
|
51
|
+
export { default as transformBooleanSchema } from './boolean/index.mjs'
|
52
|
+
export { default as transformNullSchema } from './null/index.mjs'
|
53
|
+
|
54
|
+
export {
|
55
|
+
transformString,
|
56
|
+
transformNumber,
|
57
|
+
transformBoolean,
|
58
|
+
transformNull
|
65
59
|
}
|
66
60
|
|
67
|
-
|
68
|
-
* @param {unknown} v
|
69
|
-
* @returns {number}
|
70
|
-
*/
|
71
|
-
export function toNumber (v) {
|
72
|
-
if (typeof v === 'number') return v
|
73
|
-
if (typeof v === 'boolean') return Number(v)
|
74
|
-
|
75
|
-
if (v) { // excludes zero-length strings
|
76
|
-
const n = (
|
77
|
-
isPrimitive(v) // excludes objects and arrays
|
78
|
-
? Number(v) // +v // unary operator
|
79
|
-
: NaN
|
80
|
-
)
|
81
|
-
|
82
|
-
if (!isNaN(n)) return n
|
83
|
-
}
|
84
|
-
|
85
|
-
throw new Error('Invalid `number`')
|
86
|
-
}
|
61
|
+
const log = debug('shinkansen-transmission/from-hash-to-document')
|
87
62
|
|
88
|
-
|
63
|
+
log('`shinkansen` is awake')
|
89
64
|
|
90
65
|
export { transformToValue }
|
91
66
|
|
92
67
|
/**
|
93
68
|
* @param {HashType} hash
|
94
|
-
* @param {
|
95
|
-
* @returns {(document: ObjectLiteralType | ObjectType, entry: [key: string, schema: SchemaType]) => ObjectLiteralType | ObjectType}
|
96
|
-
*/
|
97
|
-
function getReducePropertiesEntriesFor (hash, parentUri) {
|
98
|
-
/**
|
99
|
-
* @param {ObjectLiteralType | ObjectType} document
|
100
|
-
* @param {[key: string, schema: SchemaType]} entry
|
101
|
-
* @returns {ObjectLiteralType | ObjectType}
|
102
|
-
*/
|
103
|
-
return function reduce (document, [key, schema]) {
|
104
|
-
const schemaUri = getUri(parentUri, key)
|
105
|
-
|
106
|
-
document[key] = transform(hash, schema, schemaUri, schemaUri)
|
107
|
-
|
108
|
-
return document
|
109
|
-
}
|
110
|
-
}
|
111
|
-
|
112
|
-
/**
|
113
|
-
* @param {HashType} hash
|
114
|
-
* @param {SchemaType[]} items
|
69
|
+
* @param {SchemaType[]} itemSchemas
|
115
70
|
* @param {string} parentUri
|
116
71
|
* @param {string} uri
|
117
72
|
* @returns {(document: ArrayLiteralType | ArrayType, key: string) => ArrayLiteralType | ArrayType}
|
118
73
|
*/
|
119
|
-
function getReduceItemsArrayFor (hash,
|
74
|
+
function getReduceItemsArrayFor (hash, itemSchemas, parentUri, uri) {
|
75
|
+
/**
|
76
|
+
* @param {ArrayLiteralType | ArrayType} document
|
77
|
+
* @param {string} key
|
78
|
+
* @returns {ArrayLiteralType | ArrayType}
|
79
|
+
*/
|
120
80
|
return function reduce (document, key) {
|
121
81
|
const v = uri.endsWith('/') ? key.slice(uri.length) : key.slice(uri.length + 1)
|
122
82
|
const i = v ? Number(v.includes('/') ? v.slice(0, v.indexOf('/')) : v) : NaN
|
@@ -124,7 +84,7 @@ function getReduceItemsArrayFor (hash, items, parentUri, uri) {
|
|
124
84
|
if (!isNaN(i)) {
|
125
85
|
const schemaUri = getUri(parentUri, i)
|
126
86
|
|
127
|
-
document[i] =
|
87
|
+
document[i] = fromHashToDocument(hash, itemSchemas[i], schemaUri, schemaUri) // items
|
128
88
|
}
|
129
89
|
|
130
90
|
return document
|
@@ -133,12 +93,17 @@ function getReduceItemsArrayFor (hash, items, parentUri, uri) {
|
|
133
93
|
|
134
94
|
/**
|
135
95
|
* @param {HashType} hash
|
136
|
-
* @param {SchemaType}
|
96
|
+
* @param {SchemaType} itemSchema
|
137
97
|
* @param {string} parentUri
|
138
98
|
* @param {string} uri
|
139
99
|
* @returns {(document: ArrayLiteralType | ArrayType, key: string) => ArrayLiteralType | ArrayType}
|
140
100
|
*/
|
141
|
-
function getReduceItemsObjectFor (hash,
|
101
|
+
function getReduceItemsObjectFor (hash, itemSchema, parentUri, uri) {
|
102
|
+
/**
|
103
|
+
* @param {ArrayLiteralType | ArrayType} document
|
104
|
+
* @param {string} key
|
105
|
+
* @returns {ArrayLiteralType | ArrayType}
|
106
|
+
*/
|
142
107
|
return function reduce (document, key) {
|
143
108
|
const v = uri.endsWith('/') ? key.slice(uri.length) : key.slice(uri.length + 1)
|
144
109
|
const i = v ? Number(v.includes('/') ? v.slice(0, v.indexOf('/')) : v) : NaN
|
@@ -146,111 +111,21 @@ function getReduceItemsObjectFor (hash, items, parentUri, uri) {
|
|
146
111
|
if (!isNaN(i)) {
|
147
112
|
const schemaUri = getUri(parentUri, i)
|
148
113
|
|
149
|
-
document[i] =
|
114
|
+
document[i] = fromHashToDocument(hash, itemSchema, schemaUri, schemaUri) // items
|
150
115
|
}
|
151
116
|
|
152
117
|
return document
|
153
118
|
}
|
154
119
|
}
|
155
120
|
|
156
|
-
/**
|
157
|
-
* @param {DocumentType} document
|
158
|
-
* @param {ItemsType} [array]
|
159
|
-
* @returns {DocumentType | undefined}
|
160
|
-
*/
|
161
|
-
export function transformToValueFor (document, array = []) {
|
162
|
-
/*
|
163
|
-
* log('transformToValueFor')
|
164
|
-
*/
|
165
|
-
|
166
|
-
const i = toNumber(document)
|
167
|
-
|
168
|
-
if (i in array) { // @ts-ignore
|
169
|
-
const v = array[i]
|
170
|
-
|
171
|
-
/*
|
172
|
-
* Return the document according to the schema `v` or, if `v` is a primitive, return that
|
173
|
-
*/
|
174
|
-
return transformToValue(v)
|
175
|
-
}
|
176
|
-
|
177
|
-
throw new Error('Invalid `document`')
|
178
|
-
}
|
179
|
-
|
180
|
-
/**
|
181
|
-
* @param {HashType} hash
|
182
|
-
* @param {ItemsType} [array]
|
183
|
-
* @param {string} [uri]
|
184
|
-
* @returns {string | number | object | boolean | null | []}
|
185
|
-
*/
|
186
|
-
export function getArrayFor (hash, array = [], uri = '#') {
|
187
|
-
/*
|
188
|
-
* log('getArrayFor')
|
189
|
-
*/
|
190
|
-
|
191
|
-
if (uri in hash) {
|
192
|
-
const v = hash[uri]
|
193
|
-
const i = Number(v)
|
194
|
-
|
195
|
-
if (!isNaN(i)) return array[i]
|
196
|
-
}
|
197
|
-
|
198
|
-
return []
|
199
|
-
}
|
200
|
-
|
201
121
|
/**
|
202
122
|
* @param {HashType} hash
|
203
|
-
* @param {SchemaType} [
|
123
|
+
* @param {SchemaType[]} [itemSchemas]
|
204
124
|
* @param {string} parentUri
|
205
125
|
* @param {string} uri
|
206
|
-
* @returns {DocumentType | undefined}
|
207
|
-
*/
|
208
|
-
export function transformArrayFor (hash, { items } = {}, parentUri = '#', uri = getUri(parentUri)) {
|
209
|
-
/*
|
210
|
-
* log('transformArrayFor')
|
211
|
-
*/
|
212
|
-
|
213
|
-
if (isArray(items)) {
|
214
|
-
return transformItemsArrayFor(hash, items, parentUri, uri)
|
215
|
-
} else {
|
216
|
-
if (isObject(items)) {
|
217
|
-
return transformItemsObjectFor(hash, items, parentUri, uri)
|
218
|
-
}
|
219
|
-
}
|
220
|
-
|
221
|
-
return []
|
222
|
-
}
|
223
|
-
|
224
|
-
/**
|
225
|
-
* @param {HashType} hash
|
226
|
-
* @param {SchemaType} [schema]
|
227
|
-
* @param {string} [parentUri]
|
228
126
|
* @returns {DocumentType}
|
229
127
|
*/
|
230
|
-
|
231
|
-
/*
|
232
|
-
* log('transformObjectFor')
|
233
|
-
*/
|
234
|
-
|
235
|
-
if (isObject(properties)) {
|
236
|
-
return (
|
237
|
-
Object
|
238
|
-
.entries(properties)
|
239
|
-
.reduce(getReducePropertiesEntriesFor(hash, parentUri), {})
|
240
|
-
)
|
241
|
-
}
|
242
|
-
|
243
|
-
return {}
|
244
|
-
}
|
245
|
-
|
246
|
-
/**
|
247
|
-
* @param {HashType} hash
|
248
|
-
* @param {SchemaType[]} [items]
|
249
|
-
* @param {string} parentUri
|
250
|
-
* @param {string} uri
|
251
|
-
* @returns {DocumentType}
|
252
|
-
*/
|
253
|
-
export function transformItemsArrayFor (hash, items = [], parentUri = '#', uri = getUri(parentUri)) {
|
128
|
+
function transformItemsArrayFor (hash, itemSchemas = [], parentUri = '#', uri = getUri(parentUri)) {
|
254
129
|
/*
|
255
130
|
* log('transformItemsArrayFor')
|
256
131
|
*/
|
@@ -259,7 +134,7 @@ export function transformItemsArrayFor (hash, items = [], parentUri = '#', uri =
|
|
259
134
|
const document = hash[uri] // Reflect.get(hash, uri)
|
260
135
|
|
261
136
|
if (isArray(document)) {
|
262
|
-
return
|
137
|
+
return itemSchemas.map((itemSchema, i) => fromHashToDocument(hash, itemSchema, uri, getUri(uri, i)))
|
263
138
|
}
|
264
139
|
}
|
265
140
|
|
@@ -267,18 +142,18 @@ export function transformItemsArrayFor (hash, items = [], parentUri = '#', uri =
|
|
267
142
|
Object
|
268
143
|
.keys(hash)
|
269
144
|
.filter((key) => key.startsWith(uri))
|
270
|
-
.reduce(getReduceItemsArrayFor(hash,
|
145
|
+
.reduce(getReduceItemsArrayFor(hash, itemSchemas, parentUri, uri), [])
|
271
146
|
)
|
272
147
|
}
|
273
148
|
|
274
149
|
/**
|
275
150
|
* @param {HashType} hash
|
276
|
-
* @param {SchemaType} [
|
151
|
+
* @param {SchemaType} [itemSchema]
|
277
152
|
* @param {string} parentUri
|
278
153
|
* @param {string} uri
|
279
154
|
* @returns {DocumentType | undefined}
|
280
155
|
*/
|
281
|
-
|
156
|
+
function transformItemsObjectFor (hash, itemSchema = {}, parentUri = '#', uri = getUri(parentUri)) {
|
282
157
|
/*
|
283
158
|
* log('transformItemsObjectFor')
|
284
159
|
*/
|
@@ -287,7 +162,7 @@ export function transformItemsObjectFor (hash, items = {}, parentUri = '#', uri
|
|
287
162
|
const document = hash[uri] // Reflect.get(hash, uri)
|
288
163
|
|
289
164
|
if (isArray(document)) {
|
290
|
-
return
|
165
|
+
return fromHashToDocument(hash, itemSchema, uri, uri)
|
291
166
|
}
|
292
167
|
}
|
293
168
|
|
@@ -295,102 +170,31 @@ export function transformItemsObjectFor (hash, items = {}, parentUri = '#', uri
|
|
295
170
|
Object
|
296
171
|
.keys(hash)
|
297
172
|
.filter((key) => key.startsWith(uri))
|
298
|
-
.reduce(getReduceItemsObjectFor(hash,
|
173
|
+
.reduce(getReduceItemsObjectFor(hash, itemSchema, parentUri, uri), [])
|
299
174
|
)
|
300
175
|
}
|
301
176
|
|
302
177
|
/**
|
303
178
|
* @param {HashType} hash
|
304
|
-
* @param {SchemaType} schema
|
179
|
+
* @param {SchemaType} [schema]
|
305
180
|
* @param {string} parentUri
|
306
181
|
* @param {string} uri
|
307
182
|
* @returns {DocumentType | undefined}
|
308
183
|
*/
|
309
|
-
export function
|
184
|
+
export function transformArrayFor (hash, { items } = {}, parentUri = '#', uri = getUri(parentUri)) {
|
310
185
|
/*
|
311
|
-
* log('
|
186
|
+
* log('transformArrayFor')
|
312
187
|
*/
|
313
188
|
|
314
|
-
if (
|
315
|
-
|
316
|
-
|
317
|
-
if (
|
318
|
-
|
319
|
-
|
320
|
-
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
321
|
-
return transformToValueFor(document, array)
|
322
|
-
} else {
|
323
|
-
if (hasAnyOf(schema)) {
|
324
|
-
const array = getAnyOf(schema)
|
325
|
-
|
326
|
-
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
327
|
-
return transformToValueFor(document, array)
|
328
|
-
} else {
|
329
|
-
if (hasOneOf(schema)) {
|
330
|
-
const array = getOneOf(schema)
|
331
|
-
|
332
|
-
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
333
|
-
return transformToValueFor(document, array)
|
334
|
-
}
|
335
|
-
}
|
336
|
-
}
|
337
|
-
|
338
|
-
try {
|
339
|
-
if (isArray(document)) return document.map(toNull)
|
340
|
-
return toNull(document)
|
341
|
-
} catch (e) {
|
342
|
-
handleError(e)
|
189
|
+
if (isArray(items)) {
|
190
|
+
return transformItemsArrayFor(hash, items, parentUri, uri)
|
191
|
+
} else {
|
192
|
+
if (isObject(items)) {
|
193
|
+
return transformItemsObjectFor(hash, items, parentUri, uri)
|
343
194
|
}
|
344
|
-
|
345
|
-
return document
|
346
195
|
}
|
347
|
-
}
|
348
|
-
|
349
|
-
/**
|
350
|
-
* @param {HashType} hash
|
351
|
-
* @param {SchemaType} schema
|
352
|
-
* @param {string} parentUri
|
353
|
-
* @param {string} uri
|
354
|
-
* @returns {DocumentType | undefined}
|
355
|
-
*/
|
356
|
-
export function transformBoolean (hash, schema, parentUri, uri) {
|
357
|
-
/*
|
358
|
-
* log('transformBoolean')
|
359
|
-
*/
|
360
196
|
|
361
|
-
|
362
|
-
const document = hash[uri] // Reflect.get(hash, uri)
|
363
|
-
|
364
|
-
if (hasEnum(schema)) {
|
365
|
-
const array = getEnum(schema)
|
366
|
-
|
367
|
-
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
368
|
-
return transformToValueFor(document, array)
|
369
|
-
} else {
|
370
|
-
if (hasAnyOf(schema)) {
|
371
|
-
const array = getAnyOf(schema)
|
372
|
-
|
373
|
-
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
374
|
-
return transformToValueFor(document, array)
|
375
|
-
} else {
|
376
|
-
if (hasOneOf(schema)) {
|
377
|
-
const array = getOneOf(schema)
|
378
|
-
|
379
|
-
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
380
|
-
return transformToValueFor(document, array)
|
381
|
-
}
|
382
|
-
}
|
383
|
-
}
|
384
|
-
|
385
|
-
try {
|
386
|
-
if (isArray(document)) return document.map(toBoolean)
|
387
|
-
return toBoolean(document)
|
388
|
-
} catch (e) {
|
389
|
-
handleError(e)
|
390
|
-
}
|
391
|
-
|
392
|
-
return document
|
393
|
-
}
|
197
|
+
return []
|
394
198
|
}
|
395
199
|
|
396
200
|
/**
|
@@ -400,110 +204,112 @@ export function transformBoolean (hash, schema, parentUri, uri) {
|
|
400
204
|
* @param {string} uri
|
401
205
|
* @returns {DocumentType | undefined}
|
402
206
|
*/
|
403
|
-
export function
|
207
|
+
export function transformArray (hash, schema, parentUri, uri) {
|
404
208
|
/*
|
405
|
-
* log('
|
209
|
+
* log('transformArray')
|
406
210
|
*/
|
407
211
|
|
408
212
|
if (hasEnum(schema)) {
|
409
213
|
const array = getEnum(schema)
|
410
214
|
|
411
|
-
return
|
215
|
+
return (
|
216
|
+
transformToValue(
|
217
|
+
fromHashToArray(hash, uri, array)
|
218
|
+
)
|
219
|
+
)
|
412
220
|
} else {
|
413
221
|
if (hasAnyOf(schema)) {
|
414
222
|
const array = getAnyOf(schema)
|
415
223
|
|
416
|
-
return
|
224
|
+
return (
|
225
|
+
transformToValue(
|
226
|
+
fromHashToArray(hash, uri, array)
|
227
|
+
)
|
228
|
+
)
|
417
229
|
} else {
|
418
230
|
if (hasOneOf(schema)) {
|
419
231
|
const array = getOneOf(schema)
|
420
232
|
|
421
|
-
return
|
233
|
+
return (
|
234
|
+
transformToValue(
|
235
|
+
fromHashToArray(hash, uri, array)
|
236
|
+
)
|
237
|
+
)
|
422
238
|
}
|
423
239
|
}
|
424
240
|
}
|
425
241
|
|
426
|
-
return
|
242
|
+
return transformArrayFor(hash, schema, parentUri, uri)
|
427
243
|
}
|
428
244
|
|
429
245
|
/**
|
430
|
-
*
|
431
|
-
*
|
432
|
-
* @param {
|
433
|
-
* @param {
|
434
|
-
* @
|
246
|
+
* Hash can be `undefined`
|
247
|
+
*
|
248
|
+
* @param {HashType} [hash]
|
249
|
+
* @param {SchemaType} [schema]
|
250
|
+
* @param {ParamsType} [params]
|
251
|
+
* @returns {DocumentType}
|
435
252
|
*/
|
436
|
-
export function
|
437
|
-
|
438
|
-
* log('transformArray')
|
439
|
-
*/
|
440
|
-
|
441
|
-
if (hasEnum(schema)) {
|
442
|
-
const array = getEnum(schema)
|
253
|
+
export function transformArraySchema (hash = {}, schema = {}, params = {}) {
|
254
|
+
log('transformArraySchema')
|
443
255
|
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
const array = getAnyOf(schema)
|
256
|
+
const {
|
257
|
+
type
|
258
|
+
} = schema
|
448
259
|
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
260
|
+
// https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1
|
261
|
+
if (type === 'array') {
|
262
|
+
const {
|
263
|
+
uri: parentUri = '#',
|
264
|
+
index: arrayIndex // number | undefined
|
265
|
+
} = params
|
453
266
|
|
454
|
-
|
455
|
-
}
|
456
|
-
}
|
267
|
+
return transformArray(hash, schema, parentUri, getUri(parentUri, arrayIndex))
|
457
268
|
}
|
458
269
|
|
459
|
-
|
270
|
+
throw new Error('Schema does not conform to Instance Data Model, https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1')
|
460
271
|
}
|
461
272
|
|
462
273
|
/**
|
463
274
|
* @param {HashType} hash
|
464
|
-
* @param {SchemaType} schema
|
465
275
|
* @param {string} parentUri
|
466
|
-
* @
|
467
|
-
* @returns {DocumentType | undefined}
|
276
|
+
* @returns {(document: ObjectLiteralType | ObjectType, entry: [key: string, schema: SchemaType]) => ObjectLiteralType | ObjectType}
|
468
277
|
*/
|
469
|
-
|
470
|
-
|
471
|
-
*
|
278
|
+
function getReducePropertiesEntriesFor (hash, parentUri) {
|
279
|
+
/**
|
280
|
+
* @param {ObjectLiteralType | ObjectType} document
|
281
|
+
* @param {[key: string, schema: SchemaType]} entry
|
282
|
+
* @returns {ObjectLiteralType | ObjectType}
|
472
283
|
*/
|
284
|
+
return function reduce (document, [key, schema]) {
|
285
|
+
const schemaUri = getUri(parentUri, key)
|
473
286
|
|
474
|
-
|
475
|
-
const document = hash[uri] // Reflect.get(hash, uri)
|
476
|
-
|
477
|
-
if (hasEnum(schema)) {
|
478
|
-
const array = getEnum(schema)
|
287
|
+
document[key] = fromHashToDocument(hash, schema, schemaUri, schemaUri)
|
479
288
|
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
if (hasAnyOf(schema)) {
|
484
|
-
const array = getAnyOf(schema)
|
485
|
-
|
486
|
-
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
487
|
-
return transformToValueFor(document, array)
|
488
|
-
} else {
|
489
|
-
if (hasOneOf(schema)) {
|
490
|
-
const array = getOneOf(schema)
|
491
|
-
|
492
|
-
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
493
|
-
return transformToValueFor(document, array)
|
494
|
-
}
|
495
|
-
}
|
496
|
-
}
|
289
|
+
return document
|
290
|
+
}
|
291
|
+
}
|
497
292
|
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
293
|
+
/**
|
294
|
+
* @param {HashType} hash
|
295
|
+
* @param {SchemaType} [schema]
|
296
|
+
* @param {string} [parentUri]
|
297
|
+
* @returns {DocumentType}
|
298
|
+
*/
|
299
|
+
export function transformObjectFor (hash, { properties } = {}, parentUri = '#') { // }, uri = getUri(parentUri)) {
|
300
|
+
/*
|
301
|
+
* log('transformObjectFor')
|
302
|
+
*/
|
504
303
|
|
505
|
-
|
304
|
+
if (isObject(properties)) {
|
305
|
+
return (
|
306
|
+
Object
|
307
|
+
.entries(properties)
|
308
|
+
.reduce(getReducePropertiesEntriesFor(hash, parentUri), {})
|
309
|
+
)
|
506
310
|
}
|
311
|
+
|
312
|
+
return {}
|
507
313
|
}
|
508
314
|
|
509
315
|
/**
|
@@ -513,48 +319,74 @@ export function transformNumber (hash, schema, parentUri, uri) {
|
|
513
319
|
* @param {string} uri
|
514
320
|
* @returns {DocumentType | undefined}
|
515
321
|
*/
|
516
|
-
export function
|
322
|
+
export function transformObject (hash, schema, parentUri, uri) {
|
517
323
|
/*
|
518
|
-
* log('
|
324
|
+
* log('transformObject')
|
519
325
|
*/
|
520
326
|
|
521
|
-
if (
|
522
|
-
const
|
327
|
+
if (hasEnum(schema)) {
|
328
|
+
const array = getEnum(schema)
|
523
329
|
|
524
|
-
|
525
|
-
|
330
|
+
return (
|
331
|
+
transformToValue(
|
332
|
+
fromHashToArray(hash, uri, array)
|
333
|
+
)
|
334
|
+
)
|
335
|
+
} else {
|
336
|
+
if (hasAnyOf(schema)) {
|
337
|
+
const array = getAnyOf(schema)
|
526
338
|
|
527
|
-
|
528
|
-
|
339
|
+
return (
|
340
|
+
transformToValue(
|
341
|
+
fromHashToArray(hash, uri, array)
|
342
|
+
)
|
343
|
+
)
|
529
344
|
} else {
|
530
|
-
if (
|
531
|
-
const array =
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
540
|
-
return transformToValueFor(document, array)
|
541
|
-
}
|
345
|
+
if (hasOneOf(schema)) {
|
346
|
+
const array = getOneOf(schema)
|
347
|
+
|
348
|
+
return (
|
349
|
+
transformToValue(
|
350
|
+
fromHashToArray(hash, uri, array)
|
351
|
+
)
|
352
|
+
)
|
542
353
|
}
|
543
354
|
}
|
355
|
+
}
|
544
356
|
|
545
|
-
|
546
|
-
|
547
|
-
return toString(document)
|
548
|
-
} catch (e) {
|
549
|
-
handleError(e)
|
550
|
-
}
|
357
|
+
return transformObjectFor(hash, schema, parentUri) // , uri)
|
358
|
+
}
|
551
359
|
|
552
|
-
|
360
|
+
/**
|
361
|
+
* Hash can be `undefined`
|
362
|
+
*
|
363
|
+
* @param {HashType} [hash]
|
364
|
+
* @param {SchemaType} [schema]
|
365
|
+
* @param {ParamsType} [params]
|
366
|
+
* @returns {DocumentType}
|
367
|
+
*/
|
368
|
+
export function transformObjectSchema (hash = {}, schema = {}, params = {}) {
|
369
|
+
log('transformObjectSchema')
|
370
|
+
|
371
|
+
const {
|
372
|
+
type
|
373
|
+
} = schema
|
374
|
+
|
375
|
+
// https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1
|
376
|
+
if (type === 'object') {
|
377
|
+
const {
|
378
|
+
uri: parentUri = '#',
|
379
|
+
key: fieldKey // string | undefined
|
380
|
+
} = params
|
381
|
+
|
382
|
+
return transformObject(hash, schema, parentUri, getUri(parentUri, fieldKey))
|
553
383
|
}
|
384
|
+
|
385
|
+
throw new Error('Schema does not conform to Instance Data Model, https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1')
|
554
386
|
}
|
555
387
|
|
556
388
|
/**
|
557
|
-
*
|
389
|
+
* Hash can be `undefined`
|
558
390
|
*
|
559
391
|
* @param {HashType} [hash]
|
560
392
|
* @param {SchemaType} [schema]
|
@@ -562,7 +394,7 @@ export function transformString (hash, schema, parentUri, uri) {
|
|
562
394
|
* @param {string} [uri]
|
563
395
|
* @returns {DocumentType | undefined}
|
564
396
|
*/
|
565
|
-
export default function
|
397
|
+
export default function fromHashToDocument (hash = {}, schema = {}, parentUri = '#', uri = getUri(parentUri)) {
|
566
398
|
log('fromHashToDocument')
|
567
399
|
|
568
400
|
const {
|