-
Notifications
You must be signed in to change notification settings - Fork 257
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
Export / Import JSON Schema from/to PKL classes #92
Comments
Good timing; we just submitted apple/pkl-pantry#12 that adds support for this! |
That PR has landed. You can generate Pkl schema from JSON Schema; for example:
Note: depending on the input JSON Schema, you might need to tinker with the generated result to make it more useful. Some of JSON Schema is more expressive than Pkl; take a look at the docs for more details: https://pkl-lang.org/package-docs/pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib/current/generate/index.html |
Great to hear! JSON Schema also conditional blocks i.e |
I see apple/pkl-pantry#12 mentions it:
but I'm wondering if one is actually able to convert Pkl classes into a JSON Schema; for example:
I'm describing the scenario above just to be clear about what kind of Pkl code I'm trying to convert to a JSON Schema, in general I don't expect people to do those exact 2 steps. But something like those steps would be helpful for our project to confidently migrate from JSON Schemas to Pkl, and not have to change all the systems to Pkl at once. Thank you! |
Allowing conversion of Pkl to JSON Schema would be useful! |
We don't get have a Pkl -> JSON Schema generator, but we do have one for OpenAPI v3.1 Schema, which is mostly compatible with JSON Schema. See the details here: https://pkl-lang.org/package-docs/pkg.pkl-lang.org/pkl-pantry/org.openapis.v3.contrib/current/SchemaGenerator/index.html#generate%28%29 For example, given: // personSchema.pkl
import "package://pkg.pkl-lang.org/pkl-pantry/org.openapis.v3.contrib@1.0.3#/SchemaGenerator.pkl"
class Person {
/// The person's legal name.
name: String?
}
output = SchemaGenerator.generate(Person).output The command {
"type": "object",
"title": "Person",
"properties": {
"name": {
"type": "string",
"description": "The person's legal name.",
"nullable": true
}
},
"additionalProperties": false
} There's a couple limitations here; see the details in pkldoc for method |
It would be nice to generate PKL classes from JSON Schema definitions. Similarly, it can go the other way around from PKL to JSON Schema. I'm aware that the typing will be lost, but since JSON Schema is integrated pretty much everywhere, it can provide interoperability for the language.
The text was updated successfully, but these errors were encountered: