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

Validating JSON document when schema has bytes #101

Open itamarst opened 2 years ago

itamarst commented 2 years ago

Hi,

Continuing investigation of cddl—support for same schema with both JSON and CBOR is great, but there's the problem of bytes. The CDDL RFC says "don't support bytes in schema language", which OK, that's an approach. But another alternative is to say "if schema says bytes, expectation is that in JSON document this will be base64-encoded bytes in a string." And then you could validate JSON documents even with a schema that had bytes, by converting to bytes as part of validation.

I imagine this would have to be a two-step process:

  1. Notice it's supposed to be bytes, check if it's string and convert to bytes.
  2. Then after deserialization to bytes apply any additional controls/constraints, e.g. .size.

Since this not quite compatible with the RFC (arguably it is compatible, in that RFC says "don't use bstr in schema" so this is a superset), might want such a mode hidden behind an option. Some questions:

  1. Would you be amenable to such a feature, if I provided it? Or some other alternative?
  2. Any sense of how difficult it would be to implement?

(Still investigating if this is an actual requirement for the project, or just a nice to have; if this takes more than 60 seconds to answer "not sure" is a fine answer for both questions.)

anweiss commented 2 years ago

Thanks for the feedback @itamarst. While the JSON validator already supports CDDL byte values, to your point, it does not support CDDL byte data types (e.g. bstr and bytes). Also IMHO tool implementors can loosely interpret the following statement from Appendix E of the RFC:

Roughly speaking, this means leaving out byte strings ...

This would be easy to implement. Will mark this as a feature request and if you feel that this is more than just a nice-to-have, let me know and I'll work on it.