MCPcopy Index your code
hub / github.com/fastify/fast-json-stringify / buildArray

Function buildArray

index.js:603–804  ·  view source on GitHub ↗
(context, location, input)

Source from the content-addressed store, hash-verified

601}
602
603function buildArray (context, location, input) {
604 const schema = location.schema
605
606 let itemsLocation = location.getPropertyLocation('items')
607 itemsLocation.schema = itemsLocation.schema || {}
608
609 if (itemsLocation.schema.$ref) {
610 itemsLocation = resolveRef(context, itemsLocation)
611 }
612
613 const itemsSchema = itemsLocation.schema
614
615 if (context.functionsNamesBySchema.has(schema)) {
616 const funcName = context.functionsNamesBySchema.get(schema)
617 return `json += ${funcName}(${input})`
618 }
619
620 const nullable = schema.nullable === true
621
622 const schemaId = location.schemaId || ''
623 const jsonPointer = location.jsonPointer || ''
624 const fullPath = `${schemaId}#${jsonPointer}`
625
626 if (context.recursivePaths.has(fullPath) || context.buildingSet.has(schema) || schemaId !== '') {
627 const functionName = generateFuncName(context)
628 context.functionsNamesBySchema.set(schema, functionName)
629
630 const schemaRef = getSafeSchemaRef(context, location)
631
632 let functionCode = `
633 function ${functionName} (obj) {
634 // ${schemaRef}
635 let json = ''
636 `
637
638 functionCode += `
639 if (obj === null) return ${nullable ? 'JSON_STR_NULL' : 'JSON_STR_EMPTY_ARRAY'}
640 if (!Array.isArray(obj)) {
641 throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
642 }
643 const arrayLength = obj.length
644 `
645
646 if (!schema.additionalItems && Array.isArray(itemsSchema)) {
647 functionCode += `
648 if (arrayLength > ${itemsSchema.length}) {
649 throw new Error(\`Item at ${itemsSchema.length} does not match schema definition.\`)
650 }
651 `
652 }
653
654 if (largeArrayMechanism === 'json-stringify') {
655 functionCode += `if (arrayLength >= ${largeArraySize}) return JSON.stringify(obj)\n`
656 }
657
658 functionCode += `
659 json += JSON_STR_BEGIN_ARRAY
660 `

Callers 1

Calls 6

resolveRefFunction · 0.85
generateFuncNameFunction · 0.85
getSafeSchemaRefFunction · 0.85
buildValueFunction · 0.85
buildArrayTypeConditionFunction · 0.85
getPropertyLocationMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…