anweiss / cddl

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

Regression: failure to validate CBOR in zero-occurrence case (JSON works fine) #148

Closed itamarst closed 2 years ago

itamarst commented 2 years ago

Consider this schema:

response = {
    "success": bool,
    "data": {* label: [* tstr]}
}
label = tstr

You can have zero labels, which validates fine as JSON:

{"success": true, "data": {}}

Or one label, also validates fine as JSON:

{"success": true, "data": {"hello": ["world"]}}

When generating CBOR with Python's cbor2 for equivalent CBOR serializations, one.cbor validates just fine. zero.cbor fails to validate, however:

[ERROR] Validation of "zero.cbor" failed: error validating at cbor location /"data": map requires entry key of type label

CBOR files are in following zip:

cbor.zip

itamarst commented 2 years ago

Changing the schema to:

response = {
    "success": bool,
    "data": {0* label: [* tstr]}
}
label = tstr

fixes it, as a workaround (and suggestion for what the bug is).

anweiss commented 2 years ago

Thanks @itamarst. I see the issue. Will push up a fix.