export function isEquivalent()

in packages/extensions/openapi-to-typespec/src/utils/library-type-mapping.ts [62:102]


export function isEquivalent(schema: Schema, expected: LibraryType): boolean {
  if (expected.type === "object") {
    if (!isObjectSchema(schema)) return false;
    if (schema.properties?.length !== Object.keys(expected.properties).length) return false;

    for (const property of schema.properties ?? []) {
      if (property.serializedName in expected.properties) {
        const expectedProperty = expected.properties[property.serializedName];
        if ((property.required ?? false) !== expectedProperty.required) return false;

        if (expectedProperty.type === "primitive") {
          if (expectedProperty.typeName !== property.schema.type) return false;
        } else {
          if (!isEquivalent(property.schema, expectedProperty.schema())) return false;
        }
      } else return false;
    }
  } else if (expected.type === "enum") {
    if (expected.sealed && !isSealedChoiceSchema(schema)) return false;
    if (!expected.sealed && !isChoiceSchema(schema)) return false;

    const actualSchema = schema as SealedChoiceSchema | ChoiceSchema;
    if (actualSchema.choices.length !== Object.keys(expected.values).length) return false;

    for (const choice of actualSchema.choices) {
      if (choice.language.default.name in expected.values) {
        if (expected.values[choice.language.default.name] !== choice.value) return false;
      } else return false;
    }
  } else if (expected.type === "array") {
    if (!isArraySchema(schema)) return false;
    if (!isEquivalent(schema.elementType, expected.elementType())) return false;
  } else if (expected.type === "primitive") {
    if (!expected.schema(schema)) return false;
  } else if (expected.type === "dictionary") {
    if (!isDictionarySchema(schema)) return false;
    if (!isEquivalent(schema.elementType, expected.valueType())) return false;
  }

  return true;
}