Use pkl to validate existing config files #172

Closed
opened 2025-12-30 01:21:44 +01:00 by adam · 2 comments
Owner

Originally created by @derphilipp on GitHub (Jun 25, 2024).

Is it possible to achive:

  • Write a pkl template to validate types
  • Write a Go(lang) program that reads an already existing json file and validate it with the help of the pkl template file

This way golang programs could harness the power of pkl without modifying the user expirience / config files of existing tools

Originally created by @derphilipp on GitHub (Jun 25, 2024). Is it possible to achive: - Write a pkl template to validate types - Write a Go(lang) program that reads an already existing json file and validate it with the help of the pkl template file This way golang programs could harness the power of pkl without modifying the user expirience / config files of existing tools
adam closed this issue 2025-12-30 01:21:44 +01:00
Author
Owner

@holzensp commented on GitHub (Jun 25, 2024):

[aside: I'd prefer questions like this under Discussions]

Yes. You don't even need to write a Go program for the validation. That said, I think our editing experience is better than most JSON experiences. I guess "existing tools" might be a deal-breaker. Anyway, yes, you can always add the following to any template;

import "pkl:json"
import "package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1.0.2#/deepToTyped.pkl"

output {
  value = deepToTyped.apply(module.getClass(), new json.Parser {}.parse(read(read("prop:JsonPath"))))
}

Now, if you invoke Pkl with a value for external property JsonPath to point to your .json file, you can consume output.value as an instance of the module it's defined in.

@holzensp commented on GitHub (Jun 25, 2024): [aside: I'd prefer questions like this under Discussions] Yes. You don't even need to write a Go program for the validation. That said, I think our editing experience is better than most JSON experiences. I guess "existing tools" might be a deal-breaker. Anyway, yes, you can always add the following to any template; ``` import "pkl:json" import "package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1.0.2#/deepToTyped.pkl" output { value = deepToTyped.apply(module.getClass(), new json.Parser {}.parse(read(read("prop:JsonPath")))) } ``` Now, if you invoke Pkl with a value for external property `JsonPath` to point to your `.json` file, you can consume `output.value` as an instance of the module it's defined in.
Author
Owner

@derphilipp commented on GitHub (Jun 25, 2024):

Thank you very much and you are absolutely right, this should be under 'Discussion'!

@derphilipp commented on GitHub (Jun 25, 2024): Thank you very much and you are absolutely right, this should be under 'Discussion'!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#172