json-kotlin-schema icon indicating copy to clipboard operation
json-kotlin-schema copied to clipboard

Validate YAML

Open mikaelstaldal opened this issue 1 year ago • 7 comments

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

mikaelstaldal avatar Feb 21 '24 16:02 mikaelstaldal

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

pwall567 avatar Feb 21 '24 21:02 pwall567

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 avatar Feb 22 '24 07:02 mikaelstaldal

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

mikaelstaldal avatar Feb 22 '24 07:02 mikaelstaldal

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,

pwall567 avatar Mar 11 '24 07:03 pwall567

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

Vampire avatar Aug 28 '24 10:08 Vampire

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.

pwall567 avatar Aug 29 '24 12:08 pwall567

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. :-)

Vampire avatar Aug 29 '24 14:08 Vampire