Different pipes. One standard.

Type to search this site.

Search runs in your browser against a static index built with this site (Pagefind). Nothing you type is sent to a server.

Recipe

CLI and standard pipes: Available — v0.0.1 (experimental)no extra runtime

Deduplicate and validate records

Three standard pipes: normalise the key, keep the first record per key with std:distinct, route every record by a JSON Schema with std:validate-schema. Invalid records come out on their own output with the reasons.

How do I remove duplicate records and reject the invalid ones?

A sign-up list has the same person twice under different letter case, a plan that does not exist and a record with a missing field. You want one record per address, and the invalid ones in a separate file with the reason, not silently gone.

Both jobs are standard pipes. The only decision that is yours is the key and the schema.

A pipe is a data-processing step with declared inputs and outputs; a pipeline connects pipes.

What you need

  • The openflange command, release v0.0.1:
    bash
    go install github.com/openflange/openflange-go/cmd/openflange@v0.0.1
  • An empty directory. Create the files below in it under exactly these names and run every command from that directory.
  • Nothing else: every step of this recipe runs inside the runner. No network, no account, no key.

Steps

1. Save the input file

Seven sign-ups. Records 1 and 3 are the same address in different case, record 5 repeats record 2 exactly, record 4 has an unknown plan and zero seats, record 6 has no seats.

signups.ndjson
{"email":"Ada@Example.com","plan":"pro","seats":5}
{"email":"bob@example.org","plan":"free","seats":1}
{"email":"ada@example.com","plan":"free","seats":1}
{"email":"carol@example.net","plan":"enterprise","seats":0}
{"email":"bob@example.org","plan":"free","seats":1}
{"email":"dave@example.com","plan":"team"}
{"email":"erin@example.com","plan":"team","seats":12}

2. Describe the pipeline and its contract

normalise makes the key comparable, dedupe keeps the first record for every address, check routes by the schema. The upper bound on remembered keys is exposed as the parameter maxKeys.

pipeline.yaml
# records ─▶ normalise ─▶ dedupe ─▶ check ─┬─▶ valid
#                                          └─▶ rejected
spec: openflange.org/pipeline/v0.1
nodes:
  normalise:
    pipe: std:map                 # the key must be comparable: lower-case the e-mail first
    inputs:
      in: {from: inputs.records}
    parameters:
      expr: 'dict(item, email=item["email"].strip().lower())'
  dedupe:
    pipe: std:distinct            # the first record for every e-mail wins
    inputs:
      in: {from: normalise.outputs.out}
    parameters:
      fields: [email]
      maxKeys: {$param: maxKeys}
  check:
    pipe: std:validate-schema     # invalid records go to `rejected`, with the reasons
    inputs:
      in: {from: dedupe.outputs.out}
    parameters:
      schema:
        type: object
        required: [email, plan, seats]
        properties:
          email: {type: string, pattern: "^[^@ ]+@[^@ ]+$"}
          plan: {enum: [free, team, pro]}
          seats: {type: integer, minimum: 1}
outputs:
  valid: {from: check.outputs.valid}
  rejected: {from: check.outputs.rejected}
OPENFLANGE.md
---
spec: openflange.org/pipe/v0.1
id: deduplicate-validate
version: 0.1.0
title: Deduplicate and validate records
description: Keeps the first record for every e-mail address, then routes each record by validity against a JSON Schema.
license: Apache-2.0
keywords: [deduplicate, distinct, validate, json-schema, recipe]
maturity: experimental
scope: record
inputs:
  records:
    description: 'Sign-up records: {"email", "plan", "seats"}; `email` must be a string.'
outputs:
  valid:
    description: The first record for every e-mail address that matches the schema, in input order, with the e-mail lower-cased.
  rejected:
    required: false
    description: 'Records that do not match the schema, as {"item", "errors"}. If not connected, the first invalid record fails the run.'
parameters:
  maxKeys:
    type: integer
    default: 1000000
    minimum: 1
    description: Upper bound on the number of distinct e-mail addresses remembered; one more fails the run with E_RESOURCE.
implementation:
  type: pipeline
  path: pipeline.yaml
---
# Deduplicate and validate records

Implements the [OpenFlange](https://openflange.org/) specification.

Normalise the key, keep the first record per key, validate what is left.
Every node is a standard pipe and runs in-process; no permissions are needed.
Memory grows with the number of distinct keys, up to `maxKeys`.

3. Validate and run

The input comes from a file and the two outputs go to two files.

bash
openflange validate .
Output
valid: . (3 nodes, 5 edges)
bash
openflange run . --input records=signups.ndjson --output valid=clean.ndjson --output rejected=rejected.ndjson
Output
completed in 481µs — valid: 3 records, rejected: 2 records
clean.ndjson (written by the command)
{"email":"ada@example.com","plan":"pro","seats":5}
{"email":"bob@example.org","plan":"free","seats":1}
{"email":"erin@example.com","plan":"team","seats":12}
rejected.ndjson (written by the command)
{"item":{"email":"carol@example.net","plan":"enterprise","seats":0},"errors":[{"path":"/plan","message":"value must be one of 'free', 'team', 'pro'"},{"path":"/seats","message":"minimum: got 0, want 1"}]}
{"item":{"email":"dave@example.com","plan":"team"},"errors":[{"path":"","message":"missing property 'seats'"}]}

Three records are valid and unique. Two were refused with the reason. The two duplicates were consumed by std:distinct: removing them is what that pipe is for.

4. See the memory bound

The keys seen so far are the only state. With maxKeys=3 the fourth distinct address stops the run: keys are never forgotten to make room.

bash
openflange run . --param maxKeys=3 --input records=signups.ndjson --output valid=/dev/null --output rejected=/dev/null
Output (exit status 1)
E_RESOURCE: std:distinct saw more than maxKeys=3 distinct keys (node dedupe)
  hint: raise `maxKeys`, or reduce cardinality upstream; state must stay bounded
failed in 225µs — valid: 0 records, rejected: 0 records

5. Test it

The tests cover the duplicate in another case, the invalid records, the bound and an empty input.

tests/deduplicate-validate.test.yaml
spec: openflange.org/test/v0.1
pipe: ..
tests:
  - name: the first record for an e-mail wins, whatever its letter case
    inputs:
      records:
        values:
          - {email: "Ada@Example.com", plan: pro, seats: 5}
          - {email: "ada@example.com", plan: free, seats: 1}
          - {email: "bob@example.org", plan: free, seats: 1}
    expect:
      outputs:
        valid:
          values:
            - {email: "ada@example.com", plan: pro, seats: 5}
            - {email: "bob@example.org", plan: free, seats: 1}
        rejected: {count: 0}

  - name: invalid records are routed to rejected with the reasons
    inputs:
      records:
        values:
          - {email: "carol@example.net", plan: enterprise, seats: 0}
          - {email: "dave@example.com", plan: team}
    expect:
      outputs:
        valid: {count: 0}
        rejected:
          count: 2
          schema:
            type: object
            required: [item, errors]
            properties:
              errors: {type: array, minItems: 1, items: {type: object, required: [path, message]}}

  - name: more distinct keys than maxKeys fails the run instead of forgetting keys
    parameters: {maxKeys: 2}
    inputs:
      records:
        values:
          - {email: "a@example.com", plan: free, seats: 1}
          - {email: "b@example.com", plan: free, seats: 1}
          - {email: "c@example.com", plan: free, seats: 1}
    expect:
      status: failed
      error: {code: E_RESOURCE}

  - name: without a rejected consumer an invalid record fails the run
    inputs:
      records:
        values:
          - {email: "dave@example.com", plan: team}
    expect:
      status: failed
      error: {code: E_RUNTIME}
      outputs:
        rejected: {disabled: true}

  - name: empty input
    inputs:
      records: {values: []}
    expect:
      outputs:
        valid: {count: 0}
        rejected: {count: 0}
bash
openflange test .
Output
tests/deduplicate-validate.test.yaml
  ok    the first record for an e-mail wins, whatever its letter case
  ok    invalid records are routed to rejected with the reasons
  ok    more distinct keys than maxKeys fails the run instead of forgetting keys
  ok    without a rejected consumer an invalid record fails the run
  ok    empty input
ok 5 tests passed in 1 file (12ms)

Limits

  • The first record wins. There is no "keep the latest" and no merging of duplicates; std 0.1 has no sorting pipe, so order the input before it reaches the pipeline if the newest must win.
  • Keys are compared exactly, as JSON values, after your own normalisation. There is no fuzzy matching and no Unicode normalisation.
  • Memory grows with the number of distinct keys, up to maxKeys (default 1,000,000; keys longer than 32 bytes are kept as SHA-256 digests). One key more fails the run with E_RESOURCE.
  • Duplicates are found within one run. Nothing is remembered between runs.
  • The order of the nodes matters: here an invalid first record hides a valid later duplicate. Put check before dedupe if the first valid record should win.
  • JSON Schema format is not checked and external $refs are refused: the schema must be self-contained.

Every command on this page was run on Linux with openflange 0.0.1 (go1.27.1 linux/amd64), built from openflange-go commit 11cf8cc5378d70858724b16aa937d7f3b98c085d. The files and the recorded output are kept in the site repository under sites/dev/test/fixtures/recipes/deduplicate-validate/, and a test compares this page with them byte for byte.