dhall-lang
dhall-lang copied to clipboard
Dhall as a 'schema' for another format?
Hi. I was wondering if it's possible / feasible / practical to use Dhall as a 'schema' while using other (more common) formats such as JSON etc.
That is, whether an application could load a JSON configuration file for example and then validate it using Dhall.
Thanks!
@vojtechkral: Yes, you can do this using the json-to-dhall utility. The utility was written to convert JSON to Dhall given a Dhall schema but can also be used to just check that the JSON expression matches the Dhall schema (by discarding the converted Dhall expression)
For example:
$ json-to-dhall 'List Bool' <<< '[ true, false ]'
[ True, False ]
$ json-to-dhall 'List Bool' <<< '[ true, 1 ]'
Error: Dhall type expression and JSON value do not match:
Expected Dhall type:
Bool
JSON:
1
@Gabriel439 Thank you, but can this also be done entirely inside the application as part of loading the configuration? (Ie. without invoking an external tool such as json-to-dhall.)
@vojtechkral: If your application is written in Haskell then you can use the dhall-json package which exposes the same functionality as a Haskell API. This is the relevant function:
https://hackage.haskell.org/package/dhall-json-1.6.1/docs/Dhall-JSONToDhall.html#v:dhallFromJSON
@Gabriel439 Cool. In point of fact, I'm not much of a Haskell programmer, but still the Haskell code having this feature is important to me, because it can be reasonably expected that other implementations will follow suit (or at least won't be opposed to the feature).
Thanks!
@vojtechkral: Note that other implementations might not necessarily support this feature since JSON/YAML integration is something somewhat unique to the Haskell implementation. However, there is an open issue to standardize language support for importing JSON given a Dhall schema (e.g. essentially what json-to-dhall does as a language feature), which is more likely to solve what you are looking for
I assume you're referring to #121 . Sounds cool. Althought for my purposes it would probably be enough that the implementation exposes data types for AST / in-memory representation and allows validating that against a schema, I could then load data from arbitrary formats, convert/map them to Dhall AST and validate.