function isRelevantError()

in src/schema/validator.js [27:105]


function isRelevantError({
  error,
  manifest_version,
  allowedManifestVersionsRange,
}) {
  // The errors related to the manifest_version are always relevant,
  // if an error has been collected for it then it is because the
  // addon manifest_version is outside or the allowed range.
  if (error.instancePath === '/manifest_version') {
    return true;
  }

  const { minimum, maximum } = allowedManifestVersionsRange;

  let errorMinManifestVersion =
    error.params?.min_manifest_version ??
    error.parentSchema?.min_manifest_version ??
    minimum;

  const errorMaxManifestVersion =
    error.params?.max_manifest_version ??
    error.parentSchema?.max_manifest_version ??
    maximum;

  // Make sure the computed error max version is always >= to the computed min version.
  errorMinManifestVersion = Math.min(
    errorMinManifestVersion,
    errorMaxManifestVersion
  );

  const isTopLevelManifestKey =
    error.instancePath.split('/').filter((s) => s.length).length === 1;
  const errorFromAnyOf = error.schemaPath.includes('/anyOf/');
  // Skip the error if it is not in range, only when the error is:
  //
  // - not related to a top level manifest key (e.g. we still want to have a linting error
  //   if "action" or "browser_action" is being used in the wrong manifest version)
  //
  // - or part of a group of anyOf schema definitions (e.g. we don't need the errors related to
  //   web_accessible_resources schema definition that is only valid on a certain manifest
  //   version).
  const skipIfNotInRange = !isTopLevelManifestKey || errorFromAnyOf;

  // Omit errors related to a schema fragment that are not relevant
  // for the given manifest version (and also if its parent schema
  // is not relevant for the given manifest version), but only if
  // the manifest key nesting level is > 1 (so that we still include
  // errors related to top level manifest keys that are only supported
  // in specific manifest versions)
  if (
    skipIfNotInRange &&
    (manifest_version < errorMinManifestVersion ||
      manifest_version > errorMaxManifestVersion)
  ) {
    return false;
  }

  // An error collected by an `anyOf` schema entry is relevant only if its the schema
  // entries are relevant for the given addon manifest_version.
  if (error.keyword === 'anyOf') {
    const anyOfSchemaEntries = error.schema?.filter((schema) => {
      const min = schema.min_manifest_version ?? minimum;
      const max = schema.max_manifest_version ?? maximum;

      return manifest_version >= min && manifest_version <= max;
    });

    // The error is irrelevant if:
    // - there is no anyOf entry that is relevant for the given addon manifest_version
    // - there is only one relevant entry (in that case an error for that entry would
    //   have been already collected and there is no need to report it again as part
    //   of the error collected by anyOf.
    if (anyOfSchemaEntries?.length <= 1) {
      return false;
    }
  }

  return true;
}