---
title: Transformers
section: Core Concepts
description: Auto-heal and format your data using the transformers.
---

More often than not, the data provided by your end-users won't be in perfect shape. Extra trailing spaces, incorrectly formatted phone numbers, and other frequent irregularities that take time and effort to resolve.

importOK transformers are designed specifically to resolve this, by allowing you to apply common fixes. The transformation happens right after the file is uploaded and before the validation.

Let's see how we can extend our previous example, to make sure that all values have no leading or trailing spaces. For that purpose, we will be using the `trim` transformer.

```json
[
  {
    "label": "First name",
    "transformers": "trim"// [tl! highlight]
  },
  {
    "label": "Last name",
    "transformers": "trim"// [tl! highlight]
  },
  {
    "label": "Phone",
    "transformers": "trim"// [tl! highlight]
  },
  {
    "label": "Email",
    "transformers": "trim"// [tl! highlight]
  },

]
```

## Chaining transformers

Let's see how we can add more transformers. We are looking to capitalize the first and last name, make sure that emails are always in lower case and that the country code is always in upper case. You can chain multiple transformers using the pipe syntax.

```json
[
  {
    "label": "First name",
    "transformers": "trim|capitalize"// [tl! highlight]
  },
  {
    "label": "Last name",
    "transformers": "trim|capitalize"// [tl! highlight]
  },
  {
    "label": "Phone",
    "transformers": "trim"
  },
  {
    "label": "Email",
    "transformers": "trim|lowercase"// [tl! highlight]
  },

]
```

## Available transformers

| Transformer | Description |
|-------------|-------------|
| `as:*provider*` | Uses the specified [data provider](/docs/data-providers) to convert a value to the right label, or even finding the right value if a label was provided. |
| `capitalize` | Capitalizes the first letter of the string. |
| `date` | Converts a date from Excel format or Unix timestamp into `YYYY-MM-DD`. |
| `integer` | Converts the string to a numeric string, by removing any non digits. In case the string contains no digits, it remains unchanged. |
| `lowercase` | Converts the string to lowercase letters. |
| `number` | Converts the string to a numeric string (with floating-point), by removing any non digits. In case the string contains no digits, it remains unchanged. |
| `replace:*a*,*b*` | Replaces all matches of `a` with `b` |
| `trim` | Removes trailing and leading spaces from the string. |
| `uppercase` | Converts the string to uppercase letters. |

## Custom transformers

On top of the built-in transformers, you can always build your own transformers so that you can auto heal and format the data based on your requirements. To do this you will need to name your transformer and associate it with a callback to be called every time it is used. You can add as many transformers as necessary.

```javascript
{
  "transformers": {
    "my-transformer": function(record, field) {
      ...
    },
    "my-transformer-with-single-arg": function(record, field, arg1) {
      ...
    },
    "my-transformer-with-multiple-args": function(record, field, arg1, arg2, arg3) {
      ...
    }
  }
}
```

Let's see how we can add a transformer called `phone` to reformat phone numbers using the `google-libphonenumber` library. Please note that you cannot overwrite a transformer that already exists and you will get an error if you try to do that.

```javascript
{
  "transformers": {
    "phone": function (record, key) {
      const value = record.get(key);
      try {
        const parsedValue = parsePhoneNumberWithError(value, {
          defaultCountry: 'US'
        });
        if (!parsedValue.isValid()) {
          return value;
        }

        if (parsedValue.country === defaultCountry) {
          return parsedValue.formatNational();
        }

        return parsedValue.formatInternational();
      } catch (e) {
        return value;
      }
  }
}
```

That's all! You can use the pipe syntax to use the new transformer.

```json
{
  "fields": [
    {
      "label": "First name",
      "transformers": "trim"
    },
    {
      "label": "Last name",
      "transformers": "trim"
    },
    {
      "label": "Phone",
      "transformers": "trim|phone"
    },

  ]
}
```

## Passing additional arguments

Transformers can be made more dynamic by accepting additional arguments. Let's enhance our example to accept a country code as the first argument. If no argument is provided we will default to `US`.

```javascript
{
  "transformers": {
    "phone": function (record, key, defaultCountry) {
      const value = record.get(key);
      try {
        const parsedValue = parsePhoneNumberWithError(value, {
          defaultCountry: defaultCountry || 'US'
        });
        if (!parsedValue.isValid()) {
          return value;
        }

        if (parsedValue.country === defaultCountry) {
          return parsedValue.formatNational();
        }

        return parsedValue.formatInternational();
      } catch (e) {
        return value;
      }
  }
}
```

You can now pass the country code as indicated below.

```json
{
  "fields": [
    {
      "label": "First name",
      "transformers": "trim"
    },
    {
      "label": "Last name",
      "transformers": "trim"
    },
    {
      "label": "Phone",
      "transformers": "trim|phone:UK"
    },

  ]
}
```

## Merging and Splitting Fields

Advanced transformers can read and write multiple fields on a record, enabling powerful data manipulation patterns. This allows you to combine multiple source columns into one field (merge) or break one source column into multiple fields (split).

Since every transformer receives the full `ImportRecord` as its first argument, you can:
- Read other fields via `record.get(fieldKey)` to build combined values
- Write to other fields via `record.set(fieldKey, value)` to populate derived fields

### Merging multiple fields into one

When your source file has separate fields that need to be combined, create a custom transformer to merge them.

For example, to combine separate `first_name` and `last_name` columns into a single `full_name` field:

```javascript
ImportOK.add('importok-wizard', {
  fields: {
    first_name: {
      label: 'First Name',
      transformers: 'trim',
    },
    last_name: {
      label: 'Last Name',
      transformers: 'trim',
    },
    full_name: {
      label: 'Full Name',
      transformers: 'concat:first_name,last_name',
    },
  },

  transformers: {
    concat: (record, _key, ...sourceFields) => {
      return sourceFields
        .map(f => record.get(f) ?? '')
        .filter(Boolean)
        .join(' ');
    },
  },
});
```

The `full_name` field can be left unmapped in the mapping step since its value is derived entirely from the source fields.

#### Field definition order

Transformers execute in the order fields are defined. When merging fields, define source fields **before** the merged field. This ensures the merge transformer reads already-transformed values.

```javascript
fields: {
  first_name: { transformers: 'trim' },   // runs first
  last_name:  { transformers: 'trim' },   // runs second
  full_name:  { transformers: '...' },    // reads trimmed values
}
```

### Splitting one field into multiple

When your source file has a combined field that needs to be separated, create a custom transformer to split it.

For example, to split a single `full_name` column into separate `first_name` and `last_name` fields:

```javascript
ImportOK.add('importok-wizard', {
  fields: {
    full_name: {
      label: 'Full Name',
      transformers: 'split_into:first_name,last_name',
    },
    first_name: {
      label: 'First Name',
    },
    last_name: {
      label: 'Last Name',
    },
  },

  transformers: {
    split_into: (record, key, ...targetFields) => {
      const parts = (record.get(key) ?? '').split(' ');
      targetFields.forEach((field, i) => record.set(field, parts[i] ?? ''));
      return record.get(key) ?? '';
    },
  },
});
```

In the mapping step, map `full_name` to the source column and leave `first_name` and `last_name` unmapped since they're populated automatically by the transformer. Don't add a `required` validator to unmapped target fields.

[Let's define some validators &rarr;](/docs/validators)
