Skip to content
Discussion options

You must be logged in to vote

By default, glz::read_json does not error on missing keys — any fields absent from the JSON are left at their default-constructed values. This is why your payload with only "Name" passes without error.

To enforce that all fields are present, use glz::read with the error_on_missing_keys option:

CreateAccountSchema AccountData;
constexpr my_opts = glz::opts{.error_on_missing_keys = true};
if (auto JsonError = glz::read<my_opts>(AccountData, Request.body)) {
   Response.status(static_cast<int>(BadRequest))
       .json({{"error", "Invalid JSON"}});
   return;
}

This will return glz::error_code::missing_key if any non-nullable field is missing from the input JSON.

A few additional notes:

  • Nul…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by stephenberry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants