function getDiagnosticsForOneOfSchema()

in src/validation/utils/getDiagnosticsForNode.ts [47:92]


function getDiagnosticsForOneOfSchema(
  rootNode: ObjectASTNode,
  document: TextDocument,
  schemaPart: SchemaObject,
  oneOfSchema: string,
) {
  const mutuallyExclusiveProperties: unknown = referenceTypes[oneOfSchema]
  const mutuallyExclusivePropertiesPresent: { propNode: PropertyASTNode; schemaValue: unknown }[] = []
  let diagnostics: Diagnostic[] = []

  rootNode.properties.forEach((prop) => {
    if (!isObject(mutuallyExclusiveProperties)) {
      return
    }

    const propName = prop.keyNode.value
    const propertySchema = mutuallyExclusiveProperties[propName]

    // If the property is one of mutually exclusive properties
    if (propertySchema) {
      mutuallyExclusivePropertiesPresent.push({ propNode: prop, schemaValue: propertySchema })
      // If the property is neither in the set nor in the schema props
    } else if (!schemaPart[propName]) {
      diagnostics.push(getPropertyNodeDiagnostic(prop, document, MESSAGES.INVALID_PROPERTY_NAME))
    }
  })

  // if there is more than one item mark them all as invalid
  if (mutuallyExclusivePropertiesPresent.length > 1) {
    mutuallyExclusivePropertiesPresent.forEach((oneOfProp) => {
      diagnostics.push(
        getPropertyNodeDiagnostic(oneOfProp.propNode, document, MESSAGES.MUTUALLY_EXCLUSIVE_CHOICE_PROPERTIES),
      )
    })
    // if there is only one item and it is an object
    // recursively continue on with validation
  } else if (mutuallyExclusivePropertiesPresent.length) {
    const { schemaValue, propNode } = mutuallyExclusivePropertiesPresent[0]
    const { valueNode } = propNode
    if (valueNode && isObject(schemaValue)) {
      diagnostics = diagnostics.concat(getDiagnosticsForNode(valueNode, document, schemaValue as SchemaObject))
    }
  }

  return diagnostics
}