kcl-lang / kcl-go

KCL Go SDK
https://pkg.go.dev/kcl-lang.io/kcl-go@main
Apache License 2.0
51 stars 25 forks source link

[Enhancement] Add AllOf validations for KCL jsonschema generations #212

Closed Peefy closed 1 month ago

Peefy commented 8 months ago

Before

type validation struct {
    Name             string
    Minimum          *float64
    ExclusiveMinimum bool
    Maximum          *float64
    ExclusiveMaximum bool
    MinLength        *int
    MaxLength        *int
    Regex            *regexp.Regexp
    MultiplyOf       *int
    Unique           bool
}
type validation struct {
    Name             string
    Minimum          *float64
    ExclusiveMinimum bool
    Maximum          *float64
    ExclusiveMaximum bool
    MinLength        *int
    MaxLength        *int
    Regex            *regexp.Regexp
    MultiplyOf       *int
    Unique           bool
        AllOf   []*validation
}
jakezhu9 commented 8 months ago

For allOf keyword, a basic use case coule be:

{
  "allOf": [
    { "type": "string" },
    { "minLength": 2 }
  ]
}

In this case, the desgin to add AllOf validations is great. However, things get complicated if it is:

{
  "allOf": [
    {
      "type": "object",
      "properties": {
        "name": { "type": "string" }
      }
    },
    {
      "type": "object",
      "properties": {
        "address": { "type": "string" }
      }
    }
  ]
}

In this case, just adding some validation rules doesn't seem to be enough. Even worse, in some real-world situations, allOf is used with other conditional keywords (not, if, then...) For example in github workflow schema:

 "allOf": [
    {
        "if": {
            "properties": {
                "type": {
                    "const": "boolean"
                }
            },
            "required": [
                "type"
            ]
        },
        "then": {
            "properties": {
                "default": {
                    "type": "boolean"
                }
            }
        },
        "else": {
            "properties": {
                "default": {
                    "type": "string"
                }
            }
        }
    }
    ...

I'm not quite sure if it can all be perfectly converted to KCL. And more design and transformation rules is needed. I'm currently working on improving the import tool (#200) and have made some progress. Maybe later we can have further communication on how to make it better :)

Peefy commented 8 months ago

Great job 👍 For complex validation combinations, my current idea is to aim to connect 5 typical use cases, such as the GitHub workflow JSON schema. For specific implementation, we can first represent the input and output of some typical cases.

Of course, for some corner cases, there may indeed be conversion difficulties that need to be viewed separately.

Here's some references

shikharish commented 8 months ago

@Peefy Hello, I would like to work on this issue. I am new to the project so can you please give me an overview of the issue.

EraKin575 commented 3 months ago

If this issue is still relevant, I would like to work on it!