stephenberry / glaze

Extremely fast, in memory, JSON and interface library for modern C++
MIT License
1.22k stars 121 forks source link

JSON schema enum value description #1339

Open Chaoses-Ib opened 1 month ago

Chaoses-Ib commented 1 month ago

Is it possible to specify description for enum values? I tried the following code but it doesn't work:

enum A {
    A1 = 1,
    A2 = 2,
    A3 = 3
};

template <>
struct glz::json_schema<A>
{
   schema A1{.description = "a1 desc"};
   schema A2{.description = "a2 desc"};
   schema A3{.description = "a3 desc"};
};
Chaoses-Ib commented 1 month ago

Oh, I just found it's not officially supported by JSON Schema: https://github.com/json-schema-org/json-schema-vocabularies/issues/47. Though there is a workaround:

{
  "docHint": "enum",
  "anyOf": [
    { "const": "a", "description": "A" },
    { "const": "b", "description": "B" },
    { "const": "c", "description": "C" }
  ]
}
stephenberry commented 1 month ago

I'll have to come back to this, but note that Glaze does not automatically reflect your enum. Meaning it will treat your enum values as integers by default.

Use a glz::meta for your enum and the enumerate function to list out your enum values (which can be reflected there). I expect this might then work with your json schema description in Glaze.