Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding maxItems to a custom array type in JSON schema #144

Open
LazarenkoA opened this issue May 29, 2024 · 2 comments
Open

Adding maxItems to a custom array type in JSON schema #144

LazarenkoA opened this issue May 29, 2024 · 2 comments

Comments

@LazarenkoA
Copy link

I have the following Go structure:

type CustomTypes []CustomType

type test struct {
    Field1 CustomTypes `json:"field1"`
}

This generates the following JSON schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://stash.sigma.sbrf.ru/scpl/core.agentserver/pkg/app/test",
  "$ref": "#/$defs/test",
  "$defs": {
    "CustomType": {
      "properties": {},
      "additionalProperties": false,
      "type": "object"
    },
    "CustomTypes": {
      "items": {
        "$ref": "#/$defs/CustomType"
      },
      "type": "array"
    },
    "test": {
      "properties": {
        "field1": {
          "$ref": "#/$defs/CustomTypes"
        }
      },
      "additionalProperties": false,
      "type": "object",
      "required": [
        "field1"
      ]
    }
  }
}

I need to add a maxItems constraint to the CustomTypes array, for example:

"CustomTypes": {
      "items": {
        "$ref": "#/$defs/CustomType"
      },
      "type": "array",
      "maxItems": 10
    }

How can I achieve this? I can't add a tag to the type definition in Go.

@Rosswell
Copy link

Rosswell commented Jun 4, 2024

@LazarenkoA - i believe this is working - try to add this tags to your struct:

type CustomTypes []CustomType

type test struct {
    Field1 CustomTypes `json:"field1" jsonschema:"maxItems=10"`
}

it also supports minItems in the same way

@LazarenkoA
Copy link
Author

LazarenkoA commented Jun 7, 2024

@Rosswell
so it adds here
"field1": {
"$ref": "#/$defs/CustomTypes",
"maxItems": 10
}

but it needs to be in the type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants