exportdrift

    Check recurring CSV exports against a versioned header contract.

    csv
    data-quality
    schema
    etl
    Download zip
    Version
    0.1.0
    License
    MIT
    Last updated
    12 hours ago
    Downloads
    9

    No dependencies

    #ExportDrift · CSV 表头变更检查

    ExportDrift 是一个 MoonBit 库和命令行工具,在定期导入 CSV 前检查表头。它能按明确声明的别名识别合法改名,发现缺列、重名、额外列、顺序变更和多列争用同一字段,并给出下游可用的列位置映射。比如银行导出文件同时包含「交易编号」和 Payment ID,而两个名字都映射到同一个规范字段时,它会阻止导入脚本从两列中随意取一个。

    #安装和运行

    安装 MoonBit 工具链(moonc 1.0.0-rc1 或更新版)和 Node.js 22.4+,然后从仓库根目录运行:

    node scripts/check-version.mjs moon check --target js moon build --target js moon test --target js node scripts/smoke.mjs

    用合成样例验证合法变更和应拦截的歧义:

    moon run --target js cmd/exportdrift -- snapshot examples/payments-baseline.csv moon run --target js cmd/exportdrift -- check examples/payments-contract.json examples/payments-renamed.csv --json moon run --target js cmd/exportdrift -- check examples/payments-contract.json examples/payments-broken.csv

    命令预期结果
    snapshot输出严格的 JSON 契约,列默认必填、禁止额外列、要求相对顺序
    check … payments-renamed.csv --json三个规范字段分别映射到输入列 2、1、3,存在别名、额外列和重排,退出码为 0
    check … payments-broken.csv两列都指向 transaction_id,且缺少 amount,退出码为 1

    可将 snapshot 的标准输出保存为 UTF-8 JSON,再添加允许的 aliases,按需设置额外列和顺序规则。snapshot 接受 comma、semicolon 或 tab 作为可选分隔符。check 的退出码 0 表示兼容,1 表示契约不兼容,2 表示参数或输入无效。JSON 报告的位置从 0 开始,终端显示从 1 开始。下游应仅在 compatible 为 true 时使用映射。

    #使用场景

    数据来源操作预期结果
    财务平台每周导出流水在契约中指定交易编号、金额和入账日期,登记已确认的中英文别名正常切换导出语言可继续对账,编号重复或金额列丢失会拦截导入
    仓库定期导出库存用于按位置同步从一份已知正确的报表生成严格契约,每次同步前运行 check即使列名都保留,库存列顺序变化也会报错
    多供应商月度报表接入 ETL登记每家供应商的列名变体,允许备注缺席,读取 JSON 映射能兼容已约定的表头差异,并阻止新表头造成静默错列

    #契约格式

    版本 1 包含 version、delimiter、columns、allow_extra 和 enforce_order。每列包含 name、aliases、required。规范名和别名不能重复。匹配区分大小写并保留空格,别名要显式声明。

    enforce_order 只检查已匹配字段的相对顺序。允许的额外列可以插在中间,可选列可以缺席。空白表头、重名和多列争用同一字段都会使检查失败。

    #MoonBit 库

    发布后可用 moon add alexander627gyx-dev/exportdrift@0.1.0 安装,并在调用方的 moon.pkg 中导入:

    import { "alexander627gyx-dev/exportdrift" @drift, }

    fn validate_export(contract_json : String, csv : String) -> @drift.Report raise {
    let contract = @drift.parse_contract(contract_json)
    let header = @drift.parse_header(csv, contract.separator())
    @drift.check_header(contract, header)
    }

    header.mbt 解析首个逻辑记录,支持 UTF-8 BOM、带引号的分隔符、转义引号和列名内换行。contract.mbt 校验契约,check.mbt 判断兼容性,cmd/exportdrift 提供本地文件和命令行适配。CLI 最多读 65,537 字节来判断表头是否超过 64 KiB,仅解析首个逻辑记录。数据类型、行内容及业务规则由下游导入流程检查。

    GitHub Actions 运行编译器版本检查、静态检查、构建、单元测试、集成测试和打包检查。

    #开源与参考

    原始实现,采用 MIT 许可证。引号和转义规则参考 RFC 4180,另外支持分号和制表符格式。仓库样例均为合成数据。

    ContractError

    pub suberror ContractError {
    InvalidContract(String)
    }

    HeaderError

    pub suberror HeaderError {
    EmptyHeader
    UnexpectedQuote(Int)
    TextAfterQuote(Int)
    UnclosedQuote
    IncompleteHeader
    NulInHeader(Int)
    }

    HeaderError::description

    fn HeaderError::description(self : HeaderError) -> String

    Binding

    pub(all) struct Binding {
    name : String
    observed : String
    index : Int
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    Binding::equal

    fn Binding::equal(Binding, Binding) -> Bool
    automatically derived

    Binding::not_equal

    fn Binding::not_equal(x : Binding, y : Binding) -> Bool
    automatically derived

    Binding::to_json

    automatically derived

    Binding::to_repr

    automatically derived

    Column

    pub(all) struct Column {
    name : String
    aliases : Array[String]
    required : Bool
    } derive(Eq,
    Debug
    ,
    FromJson
    ,
    ToJson
    )

    One canonical column, with explicitly permitted names used by exporters.

    Column::equal

    fn Column::equal(Column, Column) -> Bool
    automatically derived

    Column::not_equal

    fn Column::not_equal(x : Column, y : Column) -> Bool
    automatically derived

    Column::to_json

    automatically derived

    Column::to_repr

    automatically derived

    Contract

    pub(all) struct Contract {
    version : Int
    delimiter : String
    columns : Array[Column]
    allow_extra : Bool
    enforce_order : Bool
    } derive(Eq,
    Debug
    ,
    FromJson
    ,
    ToJson
    )

    JSON schema version 1. Delimiters are "comma", "semicolon", and "tab".

    Contract::equal

    fn Contract::equal(Contract, Contract) -> Bool
    automatically derived

    Contract::not_equal

    fn Contract::not_equal(x : Contract, y : Contract) -> Bool
    automatically derived

    Contract::separator

    fn Contract::separator(self : Contract) -> Delimiter raise ContractError

    Contract::to_json

    automatically derived

    Contract::to_repr

    automatically derived

    Contract::validate

    fn Contract::validate(self : Contract) -> Unit raise ContractError

    Delimiter

    pub(all) enum Delimiter {
    Comma
    Semicolon
    Tab
    } derive(Eq,
    Debug
    )

    Delimiter::character

    fn Delimiter::character(self : Delimiter) -> Char

    Delimiter::equal

    fn Delimiter::equal(Delimiter, Delimiter) -> Bool
    automatically derived

    Delimiter::not_equal

    fn Delimiter::not_equal(x : Delimiter, y : Delimiter) -> Bool
    automatically derived

    Delimiter::to_repr

    automatically derived

    Issue

    pub(all) struct Issue {
    code : String
    name : String
    indices : Array[Int]
    blocking : Bool
    } derive(
    ToJson
    )

    Issue::to_json

    automatically derived

    Report

    pub(all) struct Report {
    compatible : Bool
    mapping : Array[Binding]
    issues : Array[Issue]
    } derive(
    ToJson
    )

    Report::to_json

    automatically derived

    check_header

    fn check_header(contract : Contract, header : Array[String]) -> Report raise ContractError

    Compare header fields under the contract. Indices are zero-based. Mapping is ordered by canonical schema position and lists only unique matches.

    header_length

    fn header_length(input : String, delimiter : Delimiter, eof~ : Bool) -> Int raise HeaderError

    Return the header's length in Unicode scalars, excluding the record terminator. Set eof=false for a bounded prefix that may have been cut off mid-record.

    parse_contract

    fn parse_contract(input : String) -> Contract raise

    Decode and validate a JSON contract before using it for an import.

    parse_header

    fn parse_header(input : String, delimiter : Delimiter) -> Array[String] raise HeaderError

    Parse only the first logical record, including CSV quoting and an optional BOM. Column names retain their original case and whitespace.

    snapshot_header

    fn snapshot_header(header : Array[String], delimiter : Delimiter) -> Contract raise ContractError

    Build a strict baseline from a trusted sample header.