23andMe / Yamale

A schema and validator for YAML.
MIT License
672 stars 88 forks source link

Possibility to export to standard json schema #96

Closed AAraKKe closed 4 years ago

AAraKKe commented 4 years ago

Hi,

we are considering using this tool for our yaml schemas validation but we rely on being able to produce a standard json schema from a yamale schema for different extensions we use that needs the schema to be supplied in json.

Would it be possible to do this? From YAML to JSON we have no issue at all, but given than yamale introduces new semantics we cannot do it easily ourselves.

Thanks!

mildebrandt commented 4 years ago

Hi, thanks for taking a look at Yamale.

Can you give an example of the errors you're seeing? Yamale schema is valid YAML, so your current converter should work.

Is the issue with the multiple document format that YAML supports? If that's the case, you'll need to save each document on the source side and reconstruct the YAML on the destination side. For example:

schemas = []
with open('your.schema') as f:
    docs = yaml.safe_load_all(f)
    for d in docs:
        schemas.append(d)

Or, you can just base64 encode the whole file, send it as a string in a JSON blob, and decode it on the other end.

AAraKKe commented 4 years ago

Hi and thanks for the quick response! Sorry I could not come back to it earlier.

We are not seeing any specific errors since we are still researching if this would beneficial for us. Let me explain a bit better.

As we udnerstand yamale, it includes richer syntax to use json schema with YAML file making it easier to define our schemas. Yamale will internally understand this rich syntax and behave as a standard json schema file.

We want to use some IDE extensions (like in VsCode) that receive a json schema and validates the file you are writing against the schema and show a visual mark when the schema is not being followed. We have also some tools in place in our CI pipeline that check all of our yaml file against different schemas.

In both cases pointed out before we need to have plain json schema syntax. When using yaml to define schemas it is not a problem because there is a correspondence 1:1 with the json schema files, we can read or schemas in yaml and create a json for them that both the extension and tools will pick naturally. But with yamale, if we would do that, the json file produced out of the yamale schema files won't be recognizable by json schema because the rich syntax is not supported.

My question is that if based on a yamale schema file (using the richer syntax) we could produce the standard json file to be read by json schema. I am not sure if this is clearer than before, but let me know.

Thanks!

mildebrandt commented 4 years ago

Sorry, I'm still having a hard time understanding what you're trying to do. Are you attempting to:

I don't understand where the JSON schema comes into play....or how and why you're producing JSON files from the Yamale schema files. If you could also explain what you mean by rich syntax, that may help too.

We wrote Yamale to work with YAML only. Using it for JSON validation, if that's what you're trying to do, would not fit the use cases we'd be will to support.

Let me know if I misunderstood anything. :)

AAraKKe commented 4 years ago

Thanks a lot for the comment. You have now answered my question. Yes, somehow we got the impression that Yamale was using JSON schema behind the scenes. We can already validate YAML using JSON schema and we thought that Yamale was adding richer syntax on top of it to make it simpler.

If this is built from the ground up without any ties with JSON schema I do not think it can fit our project.

Just let me explain since I think now understand better what Yamale is. In JSON you can write a schema like this:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/product.schema.json",
  "title": "Product",
  "description": "A product from Acme's catalog",
  "type": "object",
  "properties": {
    "productId": {
      "description": "The unique identifier for a product",
      "type": "integer",
      "minimum": 0,
      "maximum: 10
    },
  "required": [ "productId"]
}

Using JSON schema standard we can validate YAML and even write the schemas themselves in YAML. We thought Yamale was a tool built on top of it to simplify the syntax making things like the productId validation rules easier to read and maintain:

productId: int(min=0, max=10)

We were interested in knowing how we can transform that into the JSON schema standard because we need that JSON schema file for different things.

I understand now that this is not possible. The tool seems pretty cool and I will probably use it in different projects :) sadly for this one we cannot go this way.

Thanks a lot for your time!

mildebrandt commented 4 years ago

Ah, it all makes sense now. :) I'm sorry it won't fit this project, but reach out if you have any questions for integrating into other projects. Thanks!