pwall567 / json-kotlin-schema

Kotlin implementation of JSON Schema (Draft 07)
MIT License
90 stars 13 forks source link

Validate YAML #18

Open mikaelstaldal opened 8 months ago

mikaelstaldal commented 8 months ago

It would be useful to be able to validate YAML documents as well as JSON documents.

pwall567 commented 8 months ago

Hi @mikaelstaldal , thanks for your interest in the project.

I think you'll find that the use of YAML works fine – see the section in the README. Just provide a file with a ".yaml" or ".yml" extension, and it will be read as YAML.

Regards, Peter

mikaelstaldal commented 8 months ago

Yes, I know it's possible to read the schema as YAML. But I was referring to the document to be validated as YAML.

mikaelstaldal commented 8 months ago

BTW, how do you read the schema as YAML when you use JSONSchema.parse(String)?

pwall567 commented 8 months ago

Hi @mikaelstaldal , sorry about the delay. . This is a duplicate of an earlier issue #11 . From the response to that issue:

... 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,

Vampire commented 2 months ago

Is there a timeline when the rewrite using the new yaml library will be available? In my case I need support for anchors and aliases and yaml-simple is not capable of that.

Alternatively or additionally, you could also add support for other YAML libraries already supporting it like https://github.com/charleskorn/kaml/ or https://github.com/krzema12/snakeyaml-engine-kmp

pwall567 commented 2 months ago

Hi @Vampire, I assume your question refers to the new Schema library that I have been working on for some time. Unfortunately, work on that project keeps getting interrupted by other work (including my day job), and it's still some way off completion. But I think I may have a solution for you.

The json-kotlin-schema and json-kotlin-schema-codegen libraries are built on an old Java JSON library (jsonutil), but all my newer work is based around a later all-Kotlin JSON library (kjson-core). Because I keep needing to work on the existing JSON Schema libraries with the old implementation of JSON, I had already decided to investigate retro-fitting the new JSON library into the old Schema libraries. That turned out to be reasonably straightforward, and I expect to have a new version of json-kotlin-schema using the new JSON library available in a matter of days.

This means that you will be able to use kjson-yaml – a full implementation of YAML (including anchors and aliases) which uses the JSON classes from kjson-core, in conjunction with json-kotlin-schema.

Vampire commented 2 months ago

Yes, that was what I meant. :-) Thanks, will maybe give it a try then. For now I switched to io.github.optimumcode:json-schema-validator with the result of it.krzeminski:snakeyaml-engine-kmp transformed to JsonElement as I have it.krzeminski:snakeyaml-engine-kmp and org.jetbrains.kotlinx:kotlinx-serialization-json in the classpath already anyway. :-)

pwall567 commented 1 month ago

Hi @Vampire, I have only just noticed that I had prepared this response to your issue several weeks ago, and then neglected to click the button to send it. My apologies for that.

The latest version of json-kotlin-schema (0.49) uses kjson-core as it's base JSON library, so you should be able to use kjson-yaml for your YAML parsing.

I hope this meets your requirements.

Vampire commented 1 month ago

Thanks, but I think for now I stay with json-schema-validator, especially as the maintainer is currently also working on directly supporting snakeyaml-engine-kmp and maybe also KMP :-)