Import data into DevRevDefine the external schema

Discover custom fields without changing their identity

Build runtime metadata and normalized values from the same stable external field definitions.

Use immutable provider field IDs for EDM keys and display labels for name. Reuse the same keys in normalized data; deriving keys from labels breaks renames and can merge distinct fields.

Build fields and values

This example supports text, numeric and single-select fields from validated provider responses. It hex-encodes printable ASCII IDs of 1–32 characters, unique within a record type. Adapt that policy to the provider and schema limits without truncating IDs.

import type { Field } from "@devrev/airsync-sdk";

export type SourceField = {
  id: string;
  name: string;
  kind: "text" | "number" | "select";
  options?: { id: string; name: string }[];
};

export function customFieldKey(id: string): string {
  if (!/^[\x21-\x7e]{1,32}$/.test(id)) {
    throw new Error("Unsupported custom field identity");
  }
  return (
    "cf_" +
    Array.from(id, (c) => c.charCodeAt(0).toString(16).padStart(2, "0")).join(
      "",
    )
  );
}

export function customFields(
  definitions: SourceField[],
): Record<string, Field> {
  const fields: Record<string, Field> = {};
  for (const definition of definitions) {
    const key = customFieldKey(definition.id);
    if (Object.hasOwn(fields, key))
      throw new Error("Duplicate custom field ID");
    let field: Field;
    switch (definition.kind) {
      case "text":
        field = { type: "text" };
        break;
      case "number":
        field = { type: "float" };
        break;
      case "select": {
        const options = definition.options;
        if (
          !options ||
          options.some((option) => !option.id) ||
          new Set(options.map((option) => option.id)).size !== options.length
        ) {
          throw new Error("Invalid custom field options");
        }
        field = {
          type: "enum",
          enum: { values: options.map(({ id, name }) => ({ key: id, name })) },
        };
        break;
      }
      default:
        throw new Error("Unsupported custom field type");
    }
    fields[key] = { ...field, name: definition.name };
  }
  return fields;
}

export function customValues(
  fields: Record<string, Field>,
  values: Record<string, unknown>,
): Record<string, string | number> {
  const normalized: Record<string, string | number> = {};
  for (const [id, value] of Object.entries(values)) {
    if (value === null || value === undefined) continue;
    const key = customFieldKey(id);
    const field = fields[key];
    if (!field) throw new Error("Item contains an undeclared custom field");
    const valid =
      (field.type === "text" && typeof value === "string") ||
      (field.type === "float" &&
        typeof value === "number" &&
        Number.isFinite(value)) ||
      (field.type === "enum" &&
        typeof value === "string" &&
        field.enum?.values.some((option) => option.key === value));
    if (!valid) throw new Error("Custom value does not match runtime metadata");
    normalized[key] = value as string | number;
  }
  return normalized;
}

Select values use option IDs. Missing/null values are omitted; empty text is preserved.

Reuse the captured schema

  1. Discover all field definitions for the selected external sync unit before publishing metadata.
  2. Merge customFields(definitions) into a fresh clone of the stock EDM, rejecting stock-key collisions. Upload it through the metadata repository.
  3. Persist the exact field map for data extraction, using an artifact reference in connector state. Do not independently rediscover fields while normalizing records.
  4. Merge customValues(fields, source.custom_fields) into each normalized item's data.

Discover schema changes on a new run. Within a run, unknown fields, changed types or removed enum choices require explicit recovery against the captured schema. Validate the offered field mappings; discovery alone does not create a DevRev custom field.

Verify stable identities across field/option renames, similar labels and punctuation; reject duplicate IDs. Exercise incomplete discovery and schema changes between and within runs.

Last updated on