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

move schema to pkg and split in smaller files #24

Merged
merged 14 commits into from
Dec 10, 2023
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
with:
version: v3.12.1
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: '1.20'
cache: false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: '1.20'
- name: Run GoReleaser
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/losisin/helm-values-schema-json
rev: v0.2.0
rev: v1.0.0
hooks:
- id: helm-schema
args:
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
[![GitHub release (with filter)](https://img.shields.io/github/v/release/losisin/helm-values-schema-json)](https://github.com/losisin/helm-values-schema-json/releases)


Helm plugin for generating `values.schema.json` from single or multiple values files. Works only with Helm3 charts.
Helm plugin for generating `values.schema.json` from single or multiple values files. Schema can be enriched by reading annotations from comments. Works only with Helm3 charts.

## Install
## Installation

```bash
$ helm plugin install https://github.com/losisin/helm-values-schema-json.git
Expand All @@ -20,7 +20,8 @@ Installed plugin: schema

- Add multiple values files and merge them together - required
- Save output with custom name and location - default is values.schema.json in current working directory
- Change schema draft version - default is draft 2020-12
- Use preferred schema draft version - default is draft 2020
- Read annotations from comments. See [docs](https://github.com/losisin/helm-values-schema-json/tree/main/docs) for more info or checkout example yaml files in [testdata](https://github.com/losisin/helm-values-schema-json/tree/main/testdata).

## Integrations

Expand Down Expand Up @@ -56,7 +57,7 @@ First [install pre-commit](https://pre-commit.com/#install) and then create or u
```yaml
repos:
- repo: https://github.com/losisin/helm-values-schema-json
rev: v0.2.0
rev: v1.0.0
hooks:
- id: helm-schema
args: ["-input", "values.yaml"]
Expand Down
331 changes: 331 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
# Annotations from comments

JSON schema is partially implemented in this tool. It uses line comments to add annotations for the schema because head comments are frequently used by humans and tools like helm-docs. Multiple annotations can be added to a single line sepparated by semi-colon. For example:

```yaml
nameOverride: "myapp" # @schema maxLength:10;patter:^[a-z]+$
```

This will generate following schema:

```json
"nameOverride": {
"maxLength": 10,
"type": "string"
}
```

The following annotations are supported:

* [Validation Keywords for Any Instance Type](#validation-keywords-for-any-instance-type)
* [Type](#type)
* [Multiple types](#multiple-types)
* [Enum](#enum)
* [Strings](#strings)
* [maxLength](#maxlength)
* [minLength](#minlength)
* [pattern](#pattern)
* [Numbers](#numbers)
* [multipleOf](#multipleof)
* [maximum](#maximum)
* [minimum](#minimum)
* [Arrays](#arrays)
* [maxItems](#maxitems)
* [minItems](#minitems)
* [uniqueItems](#uniqueitems)
* [Objects](#objects)
* [minProperties](#minproperties)
* [maxProperties](#maxproperties)
* [required](#required)
* [Nulls](#nulls)

## Validation Keywords for Any Instance Type

### Type

The `type` keyword is used to restrict a value to a specific primitive type. There are several possible values for `type`:

* `string`
* `number`
* `integer`
* `boolean`
* `object`
* `array`
* `null`

### Multiple types

Default behaviour returns always a string unless annotation is used. In that case, it returns array of strings. Useful for keys without any value declared for documentation purposes. [section 6.1.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.1)

```yaml
# -- (int) replica count
replicaCount: # @schema type:[integer, null]
```

```json
"replicaCount": {
"type": [
"integer",
"null"
]
}
```

### Enum

Always returns array of strings. Special case is `null` where instead of string, it is treated as valid inpput type. [section 6.1.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.2)

```yaml
service: ClusterIP # @schema enum:[ClusterIP, LoadBalancer, null]
```

```json
"service": {
"enum": [
"ClusterIP",
"LoadBalancer",
null
],
"type": "string"
}
```

## Strings

### maxLength

Non-negative integer. [section 6.3.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.3.1)

```yaml
nameOverride: "myapp" # @schema maxLength:10
```

This will generate following schema:

```json
"nameOverride": {
"maxLength": 10,
"type": "string"
}
```

### minLength

Non-negative integer. [section 6.3.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.3.2)

```yaml
nameOverride: "myapp" # @schema minLength:3
```

This will generate following schema:

```json
"nameOverride": {
"minLength": 3,
"type": "string"
}
```

### pattern

String that is valid regular expression, according to the ECMA-262 regular expression dialect. [section 6.3.3](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.3.3)

```yaml
nameOverride: "myapp" # @schema patter:^[a-z]+$
```

This will generate following schema:

```json
"nameOverride": {
"type": "string"
}
```

## Numbers

### multipleOf

Number greater than `0`. [section 6.2.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.2.1)

```yaml
replicas: 2 # @schema multipleOf:2
```

```json
"replicas": {
"multipleOf": 2,
"type": "integer"
}
```

### maximum

Number. [section 6.2.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.2.2)

```yaml
replicas: 2 # @schema maximum:10
```

```json
"replicas": {
"maximum": 10,
"type": "integer"
}
```

### minimum

Number. [section 6.2.4](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.2.4)

```yaml
replicas: 5 # @schema minimum:2
```

```json
"replicas": {
"minimum": 2,
"type": "integer"
}
```

## Arrays

### maxItems

Non-negative integer. [section 6.4.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.1)

```yaml
dummyList: # @schema maxItems:5
- "item1"
- "item2"
- "item3"
```

```json
"dummyList": {
"items": {
"type": "string"
},
"maxItems": 5,
"type": "array"
}
```

### minItems

Non-negative integer. [section 6.4.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.2)

```yaml
dummyList: # @schema minItems:2
- "item1"
- "item2"
- "item3"
```

```json
"dummyList": {
"items": {
"type": "string"
},
"minItems": 2,
"type": "array"
}
```

### uniqueItems

Boolean. [section 6.4.3](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.3)

```yaml
dummyList: # @schema uniqueItems:true
- "item1"
- "item2"
- "item3"
```

```json
"dummyList": {
"items": {
"type": "string"
},
"type": "array",
"uniqueItems": true
}
```

## Objects

### maxProperties

Non-negative integer. [section 6.5.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.1)

```yaml
nodeSelector: # @schema maxProperties:10
kubernetes.io/hostname: "my-node"
```

```json
"nodeSelector": {
"maxProperties": 10,
"properties": {
"kubernetes.io/hostname": {
"type": "string"
}
},
"type": "object"
}
```

### minProperties

Non-negative integer. [section 6.5.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.2)

```yaml
nodeSelector: # @schema minProperties:1
kubernetes.io/hostname: "my-node"
```

```json
"nodeSelector": {
"minProperties": 1,
"properties": {
"kubernetes.io/hostname": {
"type": "string"
}
},
"type": "object"
}
```

### required

Array of unique strings appened to the parent node. [section 6.5.3](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.3)

```yaml
image: # @schema required:[repository, tag]
repository: "nginx"
tag: "latest"
imagePullPolicy: "IfNotPresent"
```

```json
"image": {
"properties": {
"imagePullPolicy": {
"type": "string"
},
"repository": {
"type": "string"
},
"tag": {
"type": "string"
}
},
"required": [
"repository",
"tag"
],
"type": "object"
}
```
Loading