valijson icon indicating copy to clipboard operation
valijson copied to clipboard

Implement population of "default" values in documents

Open carlsmedstad opened this issue 2 years ago • 0 comments

Firstly, thanks for a great library!

When we implemented this in our code base one useful feature that came to mind was population of default values for missing keys / values.

I imaging the interface would look something like this:

json myDocument;
valijson::Schema mySchema;

std::cout << "Before:" << std::endl;
std::cout << myDocument.dump(2) << std::endl;

valijson::defaultPopulator;
defaultPopulator.populate(myDocument, mySchema);

std::cout << "After:" << std::endl;
std::cout << myDocument.dump(2) << std::endl;

This would output:

Before:
{
  "input" : 123
}
After:
{
  "input": 123,
  "my_default": "asd"
}

When using a schema like this:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/product.schema.json",
  "title": "example",
  "type": "object",
  "properties": {
    "my_default": {
      "type": "string",
      "default": "asd"
    }
  }
}

I'd be happy to try to implement this if this sounds like a good idea. Thanks!

carlsmedstad avatar Mar 25 '22 11:03 carlsmedstad