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" },
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.

ValidationError: data/spec/replicas must be integer, data/spec must be null, data/spec must match exactly one schema in oneOf
- path: "/home/tommy/Projects/kosko/examples/getting-started/components/nginx.js"
- index: [0]
- kind: "apps/v1/Deployment"
- namespace: "default"
- name: "nginx"
NameDescription
pathPath of component file.
indexLocation of manifest in a component. In this example, [0] means it is the first manifest in the component.
kindapiVersion and kind of manifest.
namespacenamespace of manifest.
namename of manifest.

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