Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Paste code macro
languagegroovy
titleinput_skus
def matrix = api.inputMatrix("SKUs", "sku")

// Front End payload validation
if (!api.syntaxCheck) {
  validate(matrix) // throws exception if invalid - this validation is totally optional, feel free to drop or tailor to your own needs
}

// test value
if (matrix == null) {
  matrix = [[sku: "MB-0007"]]
}

matrix*.sku



def validate(def matrix) {
  if (matrix == null) {
    exception(matrix, "is missing or null")
  }
  if (matrix.size() == 0) {
    exception(matrix, "should not be empty")
  }
  if (! (matrix instanceof List) || ! (matrix[0] instanceof Map)) {
    exception(matrix, "should be an Array of Maps")
  }
  if (matrix[0].keySet() == ["sku"] as Set) {
    // that's expected for backend action
  }
  else if (matrix[0].keySet() == ["sku", "selected"] as Set) {
    // that's expected for UI InputMatrix
  }
  else {
    exception(matrix, "should have only 'sku' values")
  }
  if (matrix*.sku.find {!it instanceof String}) {
     exception(matrix, "should have'sku' values of type String")
  }
}

def exception(def matrix, def msg) {
  api.throwException("'SKUs' InputMatrix " + msg + " - " + api.toJson(matrix))
}

...