A good way to model this might be to separate your serialization objects from the actual domain models, that is have all optional fields in the serialization models and then have a constructor for your domain models that take an instance of the serialization model and validate its fields for presence (and possibly other things), potentially normalize the values, fallback to default values and emit warnings as doing so. For writing back to the file the domain model could then emit back an instance of the serialization model populated from the internal consistent data.
Here is a script which will tell you if the key is in a json file.
json = JSON.parse(File.read("<file>"))
def search(key_word, json)
json.as_h.each do |key, value|
json.as_a.each do |value|
p search("status", json)
I don’t know the performance of this, but this will search through an entire json file.
It will return true if it found it, otherwise nil.
Either way, if there is no crystal tooling, you might be able to solve the problem with some other tooling.
A json schema, besides defining the properties you want and getting a validation error, it allows you to define a default value. This default value is rarely (or never) used by validation. But other tools might used. In practice it seems to highlight for API what value would be semantically equivalent to not provide the given property.
I think that if the validation error is a missing object that happens to have a default value, then you can automate a fix.