4
4
5
5
const merge = require ( '@fastify/deepmerge' ) ( )
6
6
const clone = require ( 'rfdc' ) ( { proto : true } )
7
- const fjsCloned = Symbol ( 'fast-json-stringify.cloned' )
8
7
const { randomUUID } = require ( 'crypto' )
9
8
10
9
const validate = require ( './schema-validator' )
@@ -581,33 +580,23 @@ function buildObject (location) {
581
580
}
582
581
583
582
function buildArray ( location ) {
584
- let schema = location . schema
585
-
586
- // default to any items type
587
- if ( ! schema . items ) {
588
- schema . items = { }
589
- }
583
+ const schema = location . schema
590
584
591
585
let itemsLocation = mergeLocation ( location , 'items' )
586
+ itemsLocation . schema = itemsLocation . schema || { }
592
587
593
- if ( schema . items . $ref ) {
594
- if ( ! schema [ fjsCloned ] ) {
595
- location . schema = clone ( location . schema )
596
- schema = location . schema
597
- schema [ fjsCloned ] = true
598
- }
599
-
600
- location = resolveRef ( location , schema . items . $ref )
601
- itemsLocation = location
602
- schema . items = location . schema
588
+ if ( itemsLocation . schema . $ref ) {
589
+ itemsLocation = resolveRef ( itemsLocation , itemsLocation . schema . $ref )
603
590
}
604
591
605
- if ( arrayItemsReferenceSerializersMap . has ( schema . items ) ) {
606
- return arrayItemsReferenceSerializersMap . get ( schema . items )
592
+ const itemsSchema = itemsLocation . schema
593
+
594
+ if ( arrayItemsReferenceSerializersMap . has ( itemsSchema ) ) {
595
+ return arrayItemsReferenceSerializersMap . get ( itemsSchema )
607
596
}
608
597
609
598
const functionName = generateFuncName ( )
610
- arrayItemsReferenceSerializersMap . set ( schema . items , functionName )
599
+ arrayItemsReferenceSerializersMap . set ( itemsSchema , functionName )
611
600
612
601
const schemaId = location . schemaId === rootSchemaId ? '' : location . schemaId
613
602
let functionCode = `
@@ -632,8 +621,8 @@ function buildArray (location) {
632
621
633
622
if ( ! schema . additionalItems ) {
634
623
functionCode += `
635
- if (arrayLength > ${ schema . items . length } ) {
636
- throw new Error(\`Item at ${ schema . items . length } does not match schema definition.\`)
624
+ if (arrayLength > ${ itemsSchema . length } ) {
625
+ throw new Error(\`Item at ${ itemsSchema . length } does not match schema definition.\`)
637
626
}
638
627
`
639
628
}
@@ -650,9 +639,9 @@ function buildArray (location) {
650
639
let jsonOutput = ''
651
640
`
652
641
653
- if ( Array . isArray ( schema . items ) ) {
654
- for ( let i = 0 ; i < schema . items . length ; i ++ ) {
655
- const item = schema . items [ i ]
642
+ if ( Array . isArray ( itemsSchema ) ) {
643
+ for ( let i = 0 ; i < itemsSchema . length ; i ++ ) {
644
+ const item = itemsSchema [ i ]
656
645
const tmpRes = buildValue ( mergeLocation ( itemsLocation , i ) , `obj[${ i } ]` )
657
646
functionCode += `
658
647
if (${ i } < arrayLength) {
@@ -672,7 +661,7 @@ function buildArray (location) {
672
661
673
662
if ( schema . additionalItems ) {
674
663
functionCode += `
675
- for (let i = ${ schema . items . length } ; i < arrayLength; i++) {
664
+ for (let i = ${ itemsSchema . length } ; i < arrayLength; i++) {
676
665
let json = JSON.stringify(obj[i])
677
666
jsonOutput += json
678
667
if (i < arrayLength - 1) {
0 commit comments