<commit_before>import ValidatorUtil from "#SRC/js/utils/ValidatorUtil";
import { findNestedPropertyInObject } from "#SRC/js/utils/Util";
import PlacementConstraintsUtil from "../utils/PlacementConstraintsUtil";
import { PROP_MISSING_ONE, SYNTAX_ERROR } from "../constants/ServiceErrorTypes";

function checkDuplicateOperatorField(constraints, pathPrefix) {
  if (!constraints || constraints.length === 0) {
    return [];
  }

  const uniques = [];
  const duplicates = [];
  constraints.forEach(function(item, index) {
    item = !pathPrefix ? { fieldName: item[0], operator: item[1] } : item;

    const isPresent = uniques.filter(function(elem) {
      return (
        elem.fieldName === item.fieldName && elem.operator === item.operator
      );
    });
    if (isPresent.length === 0) {
      uniques.push(item);
    } else {
      duplicates.push({
        operator: item.operator,
        field: item.fieldName,
        index
      });
    }
  });

  return duplicates.map(function(duplicate) {
    return {
      path: pathPrefix.concat(["constraints", duplicate.index, "fieldName"]),
      message: "Duplicate operator/ field set"
    };
  });
}

const PlacementsValidators = {
  mustHaveUniqueOperatorField(app) {
    let constraints = findNestedPropertyInObject(app, "constraints");
    let pathPrefix = [];

    if (ValidatorUtil.isEmpty(constraints)) {
      constraints = findNestedPropertyInObject(
        app,
        "scheduling.placement.constraints"
      );
      pathPrefix = ["scheduling", "placement"];
    }

    return checkDuplicateOperatorField(constraints, pathPrefix);
  },
  validateConstraints(constraints) {
    if (constraints != null && !Array.isArray(constraints)) {
      return [
        {
          path: [],
          message: "constrains needs to be an array of 2 or 3 element arrays",
          type: "TYPE_NOT_ARRAY"
        }
      ];
    }

    const isRequiredMessage =
      "You must specify a value for operator {{operator}}";
    const isRequiredEmptyMessage =<commit_msg>fix(PlacementValidators): fix placement validator to work for pods<commit_after>import ValidatorUtil from "#SRC/js/utils/ValidatorUtil";
import { findNestedPropertyInObject } from "#SRC/js/utils/Util";
import PlacementConstraintsUtil from "../utils/PlacementConstraintsUtil";
import { PROP_MISSING_ONE, SYNTAX_ERROR } from "../constants/ServiceErrorTypes";

function checkDuplicateOperatorField(constraints) {
  if (!constraints || ValidatorUtil.isEmpty(constraints)) {
    return [];
  }

  const uniques = [];
  const duplicates = [];
  constraints.forEach(function(item, index) {
    item = "operator" in item
      ? item
      : { fieldName: item[0], operator: item[1] };

    const isPresent = uniques.filter(function(elem) {
      return (
        elem.fieldName === item.fieldName && elem.operator === item.operator
      );
    });
    if (isPresent.length === 0) {
      uniques.push(item);
    } else {
      duplicates.push({
        operator: item.operator,
        field: item.fieldName,
        index
      });
    }
  });

  return duplicates.map(function(duplicate) {
    return {
      path: ["constraints", duplicate.index, "fieldName"],
      message: "Duplicate operator/ field set"
    };
  });
}

const PlacementsValidators = {
  mustHaveUniqueOperatorField(app) {
    let constraints = findNestedPropertyInObject(app, "constraints");

    if (ValidatorUtil.isEmpty(constraints)) {
      constraints = findNestedPropertyInObject(
        app,
        "scheduling.placement.constraints"
      );
    }

    return checkDuplicateOperatorField(constraints);
  },
  validateConstraints(constraints) {
    if (constraints != null && !Array.isArray(constraints)) {
      return [
        {
          path: [],
          message: "constrains needs to be an array of 2 or 3 element arrays",
          type: "TYPE_NOT_ARRAY"
        }
      ];
    }

    const isRequiredMessage =
      "You must specify a value for operator {{operator}}";
    const isRequiredEmptyMessage =
{'fraction_non_alphanumeric': 0.12188434695912263, 'fraction_numerical': 0.0027417746759720836, 'mean_word_length': 3.057633973710819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5829073', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1129, 'n_words': 249}