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

Function buildIfThenElse

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

Source from the content-addressed store, hash-verified

1168}
1169
1170function buildIfThenElse (context, location, input) {
1171 context.validatorSchemasIds.add(location.schemaId)
1172
1173 const {
1174 if: ifSchema,
1175 then: thenSchema,
1176 else: elseSchema,
1177 ...schemaWithoutIfThenElse
1178 } = location.schema
1179
1180 const rootLocation = new Location(
1181 schemaWithoutIfThenElse,
1182 location.schemaId,
1183 location.jsonPointer
1184 )
1185
1186 const ifLocation = location.getPropertyLocation('if')
1187 const ifSchemaRef = ifLocation.getSchemaRef()
1188
1189 const thenLocation = location.getPropertyLocation('then')
1190 let thenMergedSchemaId = context.mergedSchemasIds.get(thenSchema)
1191 let thenMergedLocation = null
1192 if (thenMergedSchemaId) {
1193 thenMergedLocation = getMergedLocation(context, thenMergedSchemaId)
1194 } else {
1195 thenMergedSchemaId = `__fjs_merged_${schemaIdCounter++}`
1196 context.mergedSchemasIds.set(thenSchema, thenMergedSchemaId)
1197
1198 thenMergedLocation = mergeLocations(context, thenMergedSchemaId, [
1199 rootLocation,
1200 thenLocation
1201 ])
1202 }
1203
1204 if (!elseSchema) {
1205 return `
1206 if (validator.validate("${ifSchemaRef}", ${input})) {
1207 ${buildValue(context, thenMergedLocation, input)}
1208 } else {
1209 ${buildValue(context, rootLocation, input)}
1210 }
1211 `
1212 }
1213
1214 const elseLocation = location.getPropertyLocation('else')
1215 let elseMergedSchemaId = context.mergedSchemasIds.get(elseSchema)
1216 let elseMergedLocation = null
1217 if (elseMergedSchemaId) {
1218 elseMergedLocation = getMergedLocation(context, elseMergedSchemaId)
1219 } else {
1220 elseMergedSchemaId = `__fjs_merged_${schemaIdCounter++}`
1221 context.mergedSchemasIds.set(elseSchema, elseMergedSchemaId)
1222
1223 elseMergedLocation = mergeLocations(context, elseMergedSchemaId, [
1224 rootLocation,
1225 elseLocation
1226 ])
1227 }

Callers 1

buildValueFunction · 0.85

Calls 5

getMergedLocationFunction · 0.85
mergeLocationsFunction · 0.85
buildValueFunction · 0.85
getPropertyLocationMethod · 0.80
getSchemaRefMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…