Skip to main content

Validation

When you run kosko generate or kosko validate, Kosko will run validate() method of every exported manifests.

Error

For example, when a manifest is invalid.

import { Deployment } from "kubernetes-models/apps/v1/Deployment";

const deployment = new Deployment({
metadata: {
name: "nginx",
namespace: "dev"
},
spec: {
replicas: "INVALID" // This should be a number
}
});

export default [deployment];

Kosko will throw an error as below, which includes error details and location of invalid manifests.

components/nginx.ts - apps/v1/Deployment dev/nginx [0]
------------------- ------- ---------- --- ----- ---
(1) (2) (3) (4) (5) (6)

✖ /spec/replicas must be integer

error - Found 1 error in total
error - Generate failed

The first line is the location of the invalid manifest.

  1. components/nginx.ts is the file path.
  2. apps/v1 is the API version.
  3. Deployment is the kind.
  4. dev is the namespace.
  5. nginx is the name.
  6. [0] is the index of the invalid manifest in a file.

The last line shows the total number of errors and warnings.

Built-in Validation

All classes in kubernetes-models package support validate() method, so you don't need to implement it manually.

Custom Validation

Create a class with validate() method and throws an error when the validation failed.

class Pod {
validate() {
if (!this.metadata.name) {
throw new Error("metadata.name is required");
}
}
}

You can also override validate() method of existing classes.

import { Deployment } from "kubernetes-models/apps/v1/Deployment";

class MyDeployment extends Deployment {
validate() {
super.validate();

if (this.spec.replicas < 1) {
throw new Error("replicas must be at least 1");
}
}
}

Disable Validation

To disable validation on certain manifests, you can call toJSON() method to convert a manifest into a plain object, or just create a plain object instead.

// Use toJSON method,
new Pod().toJSON();

// or just create a plain object.
{
"apiVerison": "v1",
"kind": "Pod"
}

To disable validation on all manifests, run kosko generate with --validate=false option.

kosko generate --validate=false

Lint

Available since
  • kosko v4.1.0

Validation only checks types and formats of a field. You can use lint plugin to enforce more sophisticated rules.