-
-
Notifications
You must be signed in to change notification settings - Fork 116
Add description support for schema keys #501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Add description() method to Macros::DSL for setting field descriptions - Store descriptions in type metadata - Preserve descriptions when types are replaced during macro chaining - Include descriptions in JSON schema output via json_schema extension - Support nested schema descriptions
| # @api public | ||
| def json_schema(loose: false) | ||
| compiler = SchemaCompiler.new(root: true, loose: loose) | ||
| compiler = SchemaCompiler.new(root: true, loose: loose, types: types) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to pipe types down as those are where most of the meta / description info lives.
- Simplify description method in macros/dsl.rb - Extract current_meta variable in json_schema compiler - Reformat visit_set method for clarity
timriley
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good to me! Thank you @baweaver.
I've left just one question for you, which I think would be good to address before merging.
@flash-gordon Do you have any thoughts about this one?
lib/dry/schema/dsl.rb
Outdated
|
|
||
| current_meta = @types[name]&.meta | ||
|
|
||
| new_meta[:description] ||= current_meta[:description] if current_meta&.key?(:description) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get why this is here. It's so you can have a line like this, where the real type for the key doesn't get set until after the description is given:
required(:first_name).description("First name of the user").filled(:string)This is a good thing! But it's also the only special-cased line in this method.
It feels like it at least warrants a comment explaining why it is so. Reckon you could add one?
(In fact, I wonder if there is a way to generalise it?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified and generalized to merge a wider range of metadata, added comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @baweaver, that’s very nice!
- Extract metadata preservation logic into dedicated method - Preserve all user-defined metadata (not just description) - Clearly separate system-managed keys from user-defined keys - Add comprehensive documentation explaining fluent API enablement - All tests passing (3226 examples, 0 failures)
|
This looks like a reasonable improvement of the DSL. Should we, though, add |
Adds a
.description(text)method to schema key definitions that stores descriptions in typemetadata and includes them in JSON schema output.
Usage
Implementation
• Descriptions stored in type metadata (
:descriptionkey)• Works with all macro types (required, optional, filled, maybe, hash, array, etc.)
• Preserved when types are replaced during macro chaining
• JSON schema compiler extracts descriptions from type metadata for both top-level and nested
keys
• Improved
set_typeto preserve all incoming type metadata, not just schema-specific metaChanges
•
lib/dry/schema/macros/dsl.rb- Added description method•
lib/dry/schema/dsl.rb- Updatedset_typeto preserve type metadata•
lib/dry/schema/extensions/json_schema.rb- Pass types to compiler•
lib/dry/schema/extensions/json_schema/schema_compiler.rb- Extract and include descriptions•
spec/integration/schema/description_spec.rb- Test coverageBackwards compatible with existing schemas.