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.
- TypeScript
- JavaScript (ESM)
- JavaScript (CJS)
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];
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];
const { Deployment } = require("kubernetes-models/apps/v1/Deployment");
const deployment = new Deployment({
metadata: {
name: "nginx",
namespace: "dev"
},
spec: {
replicas: "INVALID" // This should be a number
}
});
module.exports = [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.
components/nginx.ts
is the file path.apps/v1
is the API version.Deployment
is the kind.dev
is the namespace.nginx
is the name.[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.
- TypeScript
- JavaScript (ESM)
- JavaScript (CJS)
class Pod {
validate() {
if (!this.metadata.name) {
throw new Error("metadata.name is required");
}
}
}
class Pod {
validate() {
if (!this.metadata.name) {
throw new Error("metadata.name is required");
}
}
}
class Pod {
validate() {
if (!this.metadata.name) {
throw new Error("metadata.name is required");
}
}
}
You can also override validate()
method of existing classes.
- TypeScript
- JavaScript (ESM)
- JavaScript (CJS)
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");
}
}
}
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");
}
}
}
const { Deployment } = require("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
- kosko v4.1.0
Validation only checks types and formats of a field. You can use lint plugin to enforce more sophisticated rules.