export function validateSdkSuppressionsFile()

in eng/tools/sdk-suppressions/src/sdkSuppressions.ts [30:85]


export function validateSdkSuppressionsFile(
  suppressionContent: string | object | undefined | null,
): {
  result: boolean;
  message: string;
} {
  if (suppressionContent === null) {
    exitWithError("This suppression file is a empty file");
  }

  if (!suppressionContent) {
    exitWithError("This suppression file is not a valid yaml. Refer to https://aka.ms/azsdk/sdk-suppression for more information.");
  }

  const suppressionFileSchema = {
    type: "object",
    properties: {
      suppressions: {
        type: "object",
        propertyNames: {
          enum: Object.keys(sdkLabels),
        },
        patternProperties: {
          "^.*$": {
            type: "array",
            items: {
              type: "object",
              properties: {
                package: { type: "string" },
                "breaking-changes": { type: "array", items: { type: "string" } },
              },
              required: ["package", "breaking-changes"],
              additionalProperties: false,
            },
          },
        },
      },
    },
    required: ["suppressions"],
    additionalProperties: false,
  };

  const suppressionAjv = new Ajv({ allErrors: true });
  const suppressionAjvCompile = suppressionAjv.compile(suppressionFileSchema);

  const isValid = suppressionAjvCompile(suppressionContent);

  if (isValid) {
    return {
      result: true,
      message: "This suppression file is a valid yaml.",
    };
  } else {
    exitWithError("This suppression file is a valid yaml but the schema is wrong: " + suppressionAjv.errorsText(suppressionAjvCompile.errors, { separator: "\n" }));
  }
}