Matching Patterns

Introduction

To associate an attribute with a specific pattern, utilize matching_validators. These validators confirm matches based on the specified criteria.

The matching_validators directive is a crucial part of the App/settings.

{
  "matching_validators": {
    "main": {
      "validator": "filename_validator",
      "attribute": "images",
      "match": "m",
      "separator": "_"
    }
  }
}
Best Practices

Before delving into each validator, consider these best practices:

  • Prioritize strict validators like filename or directory to avoid false positives.
  • The order of operation matters; the first match takes precedence.
  • Choose a separator that doesn’t conflict with your filenames.
  • If pattern matching proves challenging, use a metadata file.
filename_validator

This common validator identifies matches within the filename.

<identifier><separator><match>.jpg

{
  "matching_validators": {
    "main": {
      "validator": "filename_validator",
      "attribute": "main_image",
      "match": "main",
      "separator": "__"
    }
  }
}

MATCH 1234__main.jpg

directory_validator

This validator matches on directories within the incoming directory.

<match>/<identifier>.jpg

{
  "matching_validators": {
    "main": {
      "validator": "directory_validator",
      "attribute": "images",
      "match": "main",
      "separator": "__"
    }
  }
}

MATCH main/1234.jpg

MATCH main/1234__filename.jpg

regex_validator

Use regular expressions to match.

<identifier><separator><match>.jpg

{
  "matching_validators": {
    "main": {
      "validator": "regex_validator",
      "attribute": "images",
      "match": "/^DC\\d{6}$/",
      "separator": "__"
    }
  }
}

MATCH 1234__DC000134.jpg

extension_validator

Match based on the custom extension.

<identifier><separator>.<match>

{
  "matching_validators": {
    "main": {
      "validator": "extension_validator",
      "attribute": "images",
      "match": "LIBX",
      "separator": "__"
    }
  }
}

MATCH 1234.LIBX

mapping_validator

This type of validator does not just match the product(-model) identifier, this validator also maps the attribute. For example we have the filename TestPROD_123456_P011_NL_tech_sheet_ecommerce_20240105.pdf and we want to map the attribute technical-sheet to the filename, on all locales that start with nl.

Important to note is that currently only the mapping_validator supports the locale*.

The order in which the validator works is the following:

  1. Go over all the mapping items and string replace them in the identifier. This results in the following: TestPROD_123456_P011_nl*_technical-sheet_ecommerce_20240105.pdf
  2. Then we explode the identifier on the separator, in this case _, and we get the following parts:
    • TestPROD
    • 123456
    • P011
    • nl*
    • technical-sheet
    • ecommerce
    • 20240105
  3. Now we loop over the match array and we check if the part is a match.
    • none means we don’t care about this part
    • identifier means we want to store this part in the identifier
    • locale means we want to store this part in the locale
    • attribute means we want to store this part in the attribute
    • If duplicate parts are found, the parts are concatenated with a separator, in this case _
    • The result of this step is:
      • identifier = 123456_P011
      • locale = nl*
      • attribute = technical-sheet
    • If one of the concatenated strings are included in the mapping, these parts are replaced with the mapped value.
  4. The result of the match is stored on the product(-model).
  "matching_validators": {
    "primary_validator": {
      "validator": "mapping_validator",
      "mapping": {
        "1": {
          "part_of_string_in_filename": "attribute"
        },
        "2": {
          "NL": "nl*"
        }
      },
      "match": {
        "1": "identifier",
        "2": "attribute",
        "3": "locale"
      },
      "separator": "-"
    }
  }

Matching Locales

When dealing with localizable attributes, find a localizable pattern. By default:

If the filename ends with a language like _en, store the file in all en locales. If the filename ends with a locale like nl_be, store the file in 1 locale nl_BE.

To customize locales, use the following mapping:

{
  "locale_matcher": {
    "FRBE": [
      "fr_BE"
    ]
  },
  "locale_match_offset": 6
}