MCPcopy
hub / github.com/fastify/fast-json-stringify / buildOneOf

Function buildOneOf

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

Source from the content-addressed store, hash-verified

1116}
1117
1118function buildOneOf (context, location, input) {
1119 context.validatorSchemasIds.add(location.schemaId)
1120
1121 const schema = location.schema
1122
1123 const type = schema.anyOf ? 'anyOf' : 'oneOf'
1124 const { [type]: oneOfs, ...schemaWithoutAnyOf } = location.schema
1125
1126 const locationWithoutOneOf = new Location(
1127 schemaWithoutAnyOf,
1128 location.schemaId,
1129 location.jsonPointer
1130 )
1131 const oneOfsLocation = location.getPropertyLocation(type)
1132
1133 let code = ''
1134
1135 for (let index = 0, oneOfsLength = oneOfs.length; index < oneOfsLength; index++) {
1136 const optionLocation = oneOfsLocation.getPropertyLocation(index)
1137 const optionSchema = optionLocation.schema
1138
1139 let mergedSchemaId = context.mergedSchemasIds.get(optionSchema)
1140 let mergedLocation = null
1141 if (mergedSchemaId) {
1142 mergedLocation = getMergedLocation(context, mergedSchemaId)
1143 } else {
1144 mergedSchemaId = `__fjs_merged_${schemaIdCounter++}`
1145 context.mergedSchemasIds.set(optionSchema, mergedSchemaId)
1146
1147 mergedLocation = mergeLocations(context, mergedSchemaId, [
1148 locationWithoutOneOf,
1149 optionLocation
1150 ])
1151 }
1152
1153 const nestedResult = buildValue(context, mergedLocation, input)
1154 const schemaRef = optionLocation.getSchemaRef()
1155
1156 code += `
1157 ${index === 0 ? 'if' : 'else if'}(validator.validate("${schemaRef}", ${input})) {
1158 ${nestedResult}
1159 }
1160 `
1161 }
1162
1163 code += `
1164 else throw new TypeError(\`The value of '${getSafeSchemaRef(context, location)}' does not match schema definition.\`)
1165 `
1166
1167 return code
1168}
1169
1170function buildIfThenElse (context, location, input) {
1171 context.validatorSchemasIds.add(location.schemaId)

Callers 1

buildValueFunction · 0.85

Calls 6

getMergedLocationFunction · 0.85
mergeLocationsFunction · 0.85
buildValueFunction · 0.85
getSafeSchemaRefFunction · 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…