JSON schemas: Basic support for patternProperties & schemas as additionalProperties #340
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR changes the
json_schema()method to add basic support for patternProperties and for schemas as additionalProperties.A good example of what I wanted to achieve with these changes can be seen here: https://json-schema.org/learn/file-system#referencing-entry
Previously when calling
json_schema()additionalPropertiescould only be set to true or false.With this change the generated schemas are a bit more accurate and can be used to validate (or write) data objects containing variable string keys with specified values.
I noticed that previously
additionalPropertieswas set to true when aobjectkey was present, but that doesn't really make sense for a JSON schema (at least if I'm not missing anything), so nowadditionalPropertiesis only set to true ifignore_extra_keysis true.To set
additionalPropertiesto a schema,ignore_extra_keysmust be false and only onestrkey must be present.I added tests and adapted the
README.rstaccordingly, but I don't have much experience with rst, so if something looks weird, please let me know.