moon_contractguard

    API contract compatibility classification and response verification

    contractguard
    cli
    moonbit
    Download zip
    Author
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    18 days ago
    Downloads
    33

    No dependencies

    #Moon ContractGuard

    API contract verification, implemented in MoonBit with a JSON CLI and reusable library API.

    • Repository: https://github.com/yyqdbngt/moon-contractguard
    • Package: yyqdbngt/moon_contractguard@0.2.0
    • License: Apache-2.0
    • Release scope: 0.1.0 initial implementation. The broader competition proposal in docs/proposal.md is a reference design, not a claim that every planned capability is implemented.

    #Implemented

    OpenAPI 3.0 JSON operation inventory; local schema references; typed request parameters; JSON request/response schemas; required fields and additionalProperties; enum/nullable; numeric/string/array/object bounds; uniqueItems; allOf/anyOf/oneOf; bounded validation. Compatibility diff detects removed operations and newly required inputs, with other changes classified for review. scripts/http.mjs checks actual HTTP responses.

    #Build and run

    Use MoonBit and Node.js 24. The core library supports JS, wasm, wasm-gc and native; the filesystem/HTTP/process CLI is JS only.

    moon update moon build --target js moon run cmd/main --target js -- examples/scenario-1.json node _build/js/debug/build/cmd/main/main.js examples/scenario-1.json

    Pass - to read a UTF-8 JSON request from stdin. A single request must be at most 16 MiB. Successful requests print one JSON result; invalid requests exit nonzero. The host runner is a separate process and does not edit the input request file.

    #Library use

    moon add yyqdbngt/moon_contractguard@0.2.0

    In the consumer's moon.pkg:

    import {
    "yyqdbngt/moon_contractguard" @engine,
    "moonbitlang/core/json",
    }

    fn example(request : Json) -> Json raise {
    @engine.execute(request)
    }

    execute(Json) -> Json raise is the standard JSON boundary. from_json, Value::to_json, and run(Value) -> Value raise provide a typed semantic value interface. Object ordering is not significant; numeric values use finite Double. Public domain functions are listed in pkg.generated.mbti.

    #Tests

    moon test --target js moon test --target wasm moon test --target wasm-gc moon test --target native # requires a C compiler moon build --target js node scripts/check.mjs python -B scripts/reference.py node scripts/integration.mjs

    There are 14 checked fixture cases in tests/cases.json, executed both in MoonBit white-box tests and through the actual Node CLI. Independent reference checks use Python's standard library or separately written algorithms. Fixtures are synthetic and are not presented as production adoption evidence. See input and output examples and current boundaries.

    #Current boundaries

    A documented OpenAPI 3.0 JSON profile, not complete OpenAPI compliance. Unsupported schema assertions set complete=false; valid=true alone is insufficient. External references, YAML, pattern/format/discriminator/not, response header constraints and non-JSON media are not verified. Compatibility changes with uncertain semantics are review, never automatically declared safe. Path parameter data is path_parameters; path itself is the route template. Query/header parameters must be supplied as typed values matching their schema. Redirects are rejected, response cap 1 MiB, request timeout 10 seconds. HTTP checks can execute supplied methods; use a selected test endpoint.

    See source and dependency attribution. This release does not establish competition eligibility or organizer acceptance.

    #2026-10-18 follow-up

    新增 schema_diff,显式区分 request 和 response:请求允许域缩小可能破坏旧调用,响应允许域扩大可能破坏旧消费者。覆盖数值上下界、类型、可空性、枚举、必填属性及嵌套属性/数组项。带未覆盖约束的变化保留 review,而非自动宣布兼容;引用有深度边界。修复本地引用重复转义和非法转义处理。

    这是结构化 schema 兼容性分析子集,不是完整逻辑蕴含求解器。原有 OpenAPI 文档 diff 仍保守要求语义复核,schema_diff 由调用方选择所比较 schema 及方向;新增属性、复杂组合与未覆盖约束仍需复核。

    Repository release: 0.2.0; installable MoonCakes module: yyqdbngt/moon_contractguard@0.2.0. Earlier repository 1.0.0 labels are retained as history; the module uses the registry-compatible zero-major sequence.

    Current source contains 16 MoonBit test declarations and 14 CLI fixtures (generated fixture tests are included in the declaration count). Run python -B scripts/followup_reference.py for independent follow-up oracles and examples/followup-20261018.json for the new scenario.

    InputError

    pub(all) suberror InputError {
    InputError(String)
    } derive(Eq, Show)

    Validation

    type Validation

    Value

    pub(all) enum Value {
    Null
    Bool(Bool)
    Number(Double)
    String(String)
    Array(Array[Value])
    Object(Map[String, Value])
    } derive(Eq,
    Debug
    )

    Value::stringify

    fn Value::stringify(self : Value) -> String

    Value::to_json

    fn Value::to_json(self : Value) -> Json

    execute

    fn execute(request : Json) -> Json raise

    from_json

    fn from_json(value : Json) -> Value

    run

    fn run(request : Value) -> Value raise

    schema_compatibility

    fn schema_compatibility(request : Value) -> Value raise

    validate_schema

    fn validate_schema(document : Value, schema : Value, value : Value) -> Value raise

    verify_case

    fn verify_case(document : Value, case : Value) -> Value raise