dhall-lang icon indicating copy to clipboard operation
dhall-lang copied to clipboard

Dhall as a 'schema' for another format?

Open vojtechkral opened this issue 5 years ago • 6 comments
trafficstars

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 avatar Jan 20 '20 11:01 vojtechkral

@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

Gabriella439 avatar Jan 20 '20 16:01 Gabriella439

@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 avatar Jan 20 '20 16:01 vojtechkral

@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

Gabriella439 avatar Jan 20 '20 16:01 Gabriella439

@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 avatar Jan 20 '20 16:01 vojtechkral

@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

Gabriella439 avatar Jan 20 '20 16:01 Gabriella439

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.

vojtechkral avatar Jan 20 '20 21:01 vojtechkral