anweiss / cddl

Concise data definition language (RFC 8610) implementation and JSON and CBOR validator in Rust
https://cddl.anweiss.tech
MIT License
90 stars 12 forks source link

Validation results differ on equivalen JSON and CBOR input #219

Open chrysn opened 7 months ago

chrysn commented 7 months ago

In an example in which different branches allow different types, validation of an AIU valid data structure succeeds when processing JSON, but fails when processing CBOR.

Inputs

features.json:

{"Foo": {}}

features.cbor:

a1 63 46 6f 6f a0

schema.cddl:

root = group

group = {
    ? "help" => tstr,
    ? "features" => feature-set,
    * group-label => group,
    }

feature-set = {
    + feature-label => feature-help
}

group-label = tstr

feature-label = tstr
feature-help = tstr

Failure mode

Works for JSON:

$ cddl validate --cddl schema.cddl --json features.json
[INFO] Root type for validation: root
[INFO] Validation of "features.json" is successful

Fails for CBOR:

$ cddl validate --cddl schema.cddl --cbor features.cbor
[INFO] Root type for validation: root
[ERROR] Validation of "features.cbor" failed: error validating at cbor location /Map([]): expected object value of type tstr, got object

Further information / context

I'm aware that this is not great CDDL -- it's capturing a particular snapshot of development, and we'll move on to something that's easier to parse as https://github.com/RIOT-OS/RIOT/pull/20366 progresses. Nonetheless, AIU this is a bug in the CDDL implementation (which I'm actually using through pycddl, and thus can't even work around by using JSON for the data that's originally YAML anyway).