pwall567 / json-kotlin-schema

Kotlin implementation of JSON Schema (Draft 07)
MIT License
88 stars 12 forks source link

Validate yaml data #11

Closed kxmpxtxnt closed 11 months ago

kxmpxtxnt commented 1 year ago

Are there any plans to implement this but with yaml content and not only json? Not in the schema in the data

alexvoss commented 1 year ago

I am just stumbling across the same issue. There is a version of JSONSchema. validate() that takes a JSONValue as the first argument. It might be possible to parse the YAML with kjson-yaml and get the JSONValue that is in the YamlDocument? Would love to hear from @pwall567 if that is a sensible option? If so then all that would be needed perhaps would be the addition of a convenience method to json-kotlin-schema.

pwall567 commented 1 year ago

@alexvoss is correct, but he has identified the wrong library. The kjson-yaml library works with a new all-Kotlin JSON library, but json-kotlin-schema uses an older Java library for the basic JSON functionality. (There is a new version of the schema library in development, and that will use the new JSON library, but it is all still some way off.)

In the meantime, you can use the yaml-simple library to parse a YAML document. The function YAMLSimple.process() takes a File, an InputStream or a Reader, and produces a YAMLDocument. This contains a property rootNode, which is of type YAMLNode – a derived class of JSONValue.

So to validate a YAML file, the following code should be what you need:

    val result = schema.validate(YAMLSimple.process(File("my.yaml")).rootNode)

I hope this is what you were looking for,

Peter Wall

alexvoss commented 1 year ago

Thanks, that is very useful and may well work for my use case. I can confirm that it works in the code I have at the moment (just come unit tests at the mo).

kxmpxtxnt commented 11 months ago

sorry forgot about this issue, yes tysm this helped!