Add description support for schema keys#501
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.
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.
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?
|
|
||
| current_meta = @types[name]&.meta | ||
|
|
||
| new_meta[:description] ||= current_meta[:description] if current_meta&.key?(:description) |
There was a problem hiding this comment.
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.
Modified and generalized to merge a wider range of metadata, added comment.
- 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 |
|
Hey what's the status here? I was just looking for this but it seems it was abandoned 😢 |
|
Thanks for chiming in here, @stefanoc! I'm keen to see this go in. Let's use your use case as input to the design. If you were to generate your Zod schemas, is that happening via JSON Schema-formatted JSON, or would you take some other approach? |
|
Zod natively supports JSON Schema since 4.0 so that would be the easiest route, but it's still flagged as experimental so who knows... ideally I'd like to go with a direct Ruby-to-Typescript conversion (so I guess something like the JSON schema extension, but which would emit a string instead of a Hash, if that makes sense). |
|
I'm also curious about the status. I'm currently using It would be ideal to define the schema with In practice the descriptions can be pretty long multi-line strings though. A fluent API might not be super ergonomical (although definitely better than not having the feature at all). |
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.