contract

    Compare flat JSON contracts and rehearse explicit migrations on supplied records.

    Download zip
    Version
    0.1.11
    License
    Apache-2.0
    Last updated
    8 hours ago
    Downloads
    74

    No dependencies

    #MoonContract

    MoonContract 用 MoonBit 分析扁平 JSON 记录契约。它可以判定旧版本接受的记录是否也被新版本接受,并为每类不兼容变化提供可重新校验的反例。

    #快速运行

    moon build cmd/main --target js --release --deny-warn node _build/js/release/build/cmd/main/main.js examples/add-optional.json node _build/js/release/build/cmd/main/main.js examples/tighten.json node _build/js/release/build/cmd/main/main.js examples/rolling-upgrade.json node _build/js/release/build/cmd/main/main.js examples/check-config.json

    样例预期结果
    add-optional.json旧配置可以读取,退出 0
    tighten.json缩小端口范围、删除枚举值和添加必填项各产生反例,退出 1
    rolling-upgrade.json新版接受旧记录,旧版拒绝带新字段的记录,退出 1
    check-config.json按一份契约验证现有配置,退出 0

    输入使用 mode 区分 compare、both 和 validate。前两种接收 source 与 target,后者接收 contract 与 record。退出 2 表示命令、请求或契约本身无效。用 - 可从标准输入读取。库入口为 parse_contract、validate、compare 和 execute。

    #契约范围

    fields 必填,最多 64 个字段。每个字段指定 type。required: true 要求字段出现,nullable: true 允许显式的 null。两项均默认关闭。整数类型 int 支持闭区间 min 和 max,默认范围 −1,000,000 至 1,000,000,文本输入采用十进制整数写法。字符串类型 string 支持有限集合 allowed。布尔类型为 bool。open 默认关闭,开启时未声明字段允许任意 JSON 值。未知契约关键字会报错。嵌套对象约束和任意 JSON Schema 在当前契约范围之外。

    #本地验证

    安装 toolchain.json 锁定的 MoonBit 与 Node.js 后,运行 node scripts/verify.mjs。脚本执行 Debug、Release 单测和命令行样例,生成 _build/verification-summary.json 和本地发布包。验收证据见 docs/acceptance.md。

    #Mooncakes 安装

    在使用方项目执行 moon add JingLan0v0/contract@0.1.10,然后在 moon.pkg 中导入 "JingLan0v0/contract"。

    #批量配置迁移演练

    ContractError

    pub(all) suberror ContractError {
    ContractError(String)
    }

    Contract

    pub struct Contract {
    // private fields
    }

    Finding

    pub(all) struct Finding {
    field : String
    reason : String
    witness :
    Json

    }

    Rehearsal

    pub struct Rehearsal {
    // private fields
    }

    Rehearsal::run

    fn Rehearsal::run(self : Rehearsal, records : Array[
    Json
    ], diagnostics? : Bool) -> (
    Json
    , Bool) raise ContractError

    Apply the prepared rules and return the report and readiness verdict.

    Violation

    pub(all) struct Violation {
    field : String
    reason : String
    }

    compare

    fn compare(source : Contract, target : Contract) -> Array[Finding]

    Check inclusion: every record admitted by source must be admitted by target. Each finding carries a record admitted by source and rejected by target.

    parse_contract

    fn parse_contract(value :
    Json
    ) -> Contract raise ContractError

    parse_json

    fn parse_json(source : String) ->
    Json
    raise ContractError

    prepare_rehearsal

    fn prepare_rehearsal(source : Contract, target : Contract, migration :
    Json
    ) -> Rehearsal raise ContractError

    Validate and retain the rules for repeated batches.

    validate

    fn validate(contract : Contract, record :
    Json
    ) -> Array[Violation]