Skip to main content
Version: 0.13

Validation

Introduction​

The KCL Validation tool supports basic configuration data verification capabilities. You can write a KCL schema to verify the type and value of the input JSON/YAML files.

How to use​

There is a JSON format file data.json:

{
"name": "Alice",
"age": 18,
"message": "This is Alice",
"data": {
"id": 1,
"value": "value1"
},
"labels": {
"key": "value"
},
"hc": [1, 2, 3]
}

Build a validated KCL file schema.k:

schema User:
name: str
age: int
message?: str
data: Data
labels: {str:}
hc: [int]

check:
age > 10

schema Data:
id: int
value: str

Execute the following command:

kcl vet data.json schema.k

Specify the schema for validation​

When multiple schema definitions exist in the KCL file, by default, the KCL Validation tool will use the first schema to check. If you need to specify a schema for verification, you can use the -s|--schema parameter

kcl vet data.json schema.k -s User

Args​

This command validates the data file using the kcl code.

Usage:
kcl vet [flags]

Examples:
# Validate the JSON data using the kcl code
kcl vet data.json code.k

# Validate the YAML data using the kcl code
kcl vet data.yaml code.k --format yaml

# Validate the JSON data using the kcl code with the schema name
kcl vet data.json code.k -s Schema

# Validate and output results as JSON for CI/CD integration
kcl vet data.json code.k --output json

# Validate against a schema that imports an external KCL package
kcl vet data.yaml schema.k -E my_pkg=./vendor/my_pkg

Flags:
-a, --attribute_name string Specify the validate config attribute name.
-E, --external stringArray Specify the mapping of package name and path where the package is located, e.g. my_pkg=./vendor/my_pkg
--format string Specify the validate data format. e.g., yaml, json. Default is json
-h, --help help for vet
--output string Specify the output format. e.g., text, json. Default is text (default "text")
-s, --schema string Specify the validate schema.