Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"dependencies": {
"@dappnode/types": "^0.1.40",
"ajv": "^8.12.0",
"ajv": "^8.17.1",
"semver": "^7.5.0"
}
}
7 changes: 1 addition & 6 deletions packages/schemas/src/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import _Ajv from "ajv";

const Ajv = _Ajv as unknown as typeof _Ajv.default;

// TODO: fix once upstream issue is fixed
// https://github.com/ajv-validator/ajv/issues/2132
import { Ajv } from "ajv";

export const ajv = new Ajv({
strict: false,
Expand Down
1 change: 1 addition & 0 deletions packages/schemas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { validateComposeSchema } from "./validateComposeSchema.js";
export { validateManifestSchema } from "./validateManifestSchema.js";
export { validateSetupWizardSchema } from "./validateSetupWizardSchema.js";
export { validateDappnodeCompose } from "./validateDappnodeCompose.js";
export { validateNotificationsSchema } from "./validateNotificationsSchema.js";
66 changes: 66 additions & 0 deletions packages/schemas/src/schemas/notifications.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/dappnode/DAppNode/raw/schema/notifications.schema.json",
"type": "object",
"title": "Notifications Configuration Schema",
"required": ["endpoints"],
"properties": {
"endpoints": {
"type": "array",
"items": {
"type": "object",
"required": ["name", "enabled", "url", "method", "conditions", "interval", "group", "alerts", "definition"],
"properties": {
"name": { "type": "string" },
"enabled": { "type": "boolean" },
"url": { "type": "string", "pattern": "^(https?|ftp)://[^s/$.?#].[^s]*$" },
"method": { "type": "string", "enum": ["GET", "POST", "PUT", "DELETE"] },
"conditions": {
"type": "array",
"items": { "type": "string" }
},
"interval": { "type": "string", "pattern": "^[0-9]+[smhd]$" },
"group": { "type": "string" },
"alerts": {
"type": "array",
"items": {
"type": "object",
"required": [
"type",
"failure-threshold",
"success-threshold",
"send-on-resolved",
"description",
"enabled"
],
"properties": {
"type": { "type": "string" },
"failure-threshold": { "type": "integer", "minimum": 1 },
"success-threshold": { "type": "integer", "minimum": 1 },
"send-on-resolved": { "type": "boolean" },
"description": { "type": "string" },
"enabled": { "type": "boolean" }
}
}
},
"definition": {
"type": "object",
"required": ["title", "description"],
"properties": {
"title": { "type": "string" },
"description": { "type": "string" }
}
},
"metric": {
"type": "object",
"properties": {
"min": { "type": "number" },
"max": { "type": "number" },
"unit": { "type": "string" }
}
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion packages/schemas/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { ErrorObject } from "ajv";
*/
export function processError(
errorObject: ErrorObject,
releaseFileType: "compose" | "manifest" | "setupWizard"
releaseFileType: "compose" | "manifest" | "setupWizard" | "notifications"
): string {
const { schemaPath, message } = errorObject;
const path = `${releaseFileType}${schemaPath}`.replace(new RegExp("/", "g"), ".");
Expand Down
20 changes: 20 additions & 0 deletions packages/schemas/src/validateNotificationsSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ajv } from "./ajv.js";
import { CliError } from "./error.js";
import { processError } from "./utils.js";
import notificationsSchema from "./schemas/notifications.schema.json" with { type: "json" };
import { GatusConfig } from "@dappnode/types";

/**
* Validates notifications.yaml file with schema
* @param config
*/
export function validateNotificationsSchema(config: GatusConfig): void {
const validateNotifications = ajv.compile(notificationsSchema);
const valid = validateNotifications(config);
if (!valid) {
const errors = validateNotifications.errors
? validateNotifications.errors.map((e) => processError(e, "notifications"))
: [];
throw new CliError(`Invalid notifications configuration: \n${errors.map((msg) => ` - ${msg}`).join("\n")}`);
}
}
162 changes: 160 additions & 2 deletions packages/schemas/test/unit/validateSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { expect } from "chai";
import { validateComposeSchema, validateManifestSchema, validateSetupWizardSchema } from "../../src/index.js";
import {
validateComposeSchema,
validateManifestSchema,
validateSetupWizardSchema,
validateNotificationsSchema
} from "../../src/index.js";
import fs from "fs";
import path from "path";
import { cleanTestDir, testDir } from "../testUtils.js";
import { Manifest, SetupWizard } from "@dappnode/types";
import { Manifest, SetupWizard, GatusConfig, Endpoint } from "@dappnode/types";

describe("schemaValidation", function () {
this.timeout(10000);
Expand Down Expand Up @@ -434,4 +439,157 @@ volumes:
expect(() => validateManifestSchema(manifest)).to.not.throw();
});
});

describe("notifications", () => {
it("should validate a valid notifications configuration", () => {
const validNotifications: GatusConfig = {
endpoints: [
{
name: "example-endpoint",
enabled: true,
url: "http://example.com",
method: "POST",
conditions: ["response-time < 500ms", "status == 200"],
interval: "1m",
group: "example-group",
alerts: [
{
type: "response-time",
"failure-threshold": 3,
"success-threshold": 2,
"send-on-resolved": true,
description: "Response time exceeded",
enabled: true
}
],
definition: {
title: "Example Endpoint",
description: "An example endpoint for testing"
},
metric: {
min: 0,
max: 1000,
unit: "ms"
}
}
]
};

expect(() => validateNotificationsSchema(validNotifications)).to.not.throw();
});

it("should throw an error for missing required fields", () => {
const invalidNotifications: Partial<GatusConfig> = {
endpoints: [
{
name: "example-endpoint",
enabled: true,
url: "http://example.com",
method: "POST"
// Missing required fields like conditions, interval, group, alerts, and definition
} as Endpoint
]
};

expect(() => validateNotificationsSchema(invalidNotifications as GatusConfig)).to.throw(
"Invalid notifications configuration"
);
});

it("should throw an error for invalid URL format", () => {
const invalidNotifications: GatusConfig = {
endpoints: [
{
name: "example-endpoint",
enabled: true,
url: "invalid-url",
method: "POST",
conditions: ["response-time < 500ms"],
interval: "1m",
group: "example-group",
alerts: [
{
type: "response-time",
"failure-threshold": 3,
"success-threshold": 2,
"send-on-resolved": true,
description: "Response time exceeded",
enabled: true
}
],
definition: {
title: "Example Endpoint",
description: "An example endpoint for testing"
}
}
]
};

expect(() => validateNotificationsSchema(invalidNotifications)).to.throw("Invalid notifications configuration");
});

it("should throw an error for invalid interval format", () => {
const invalidNotifications: GatusConfig = {
endpoints: [
{
name: "example-endpoint",
enabled: true,
url: "http://example.com",
method: "POST",
conditions: ["response-time < 500ms"],
interval: "invalid-interval",
group: "example-group",
alerts: [
{
type: "response-time",
"failure-threshold": 3,
"success-threshold": 2,
"send-on-resolved": true,
description: "Response time exceeded",
enabled: true
}
],
definition: {
title: "Example Endpoint",
description: "An example endpoint for testing"
}
}
]
};

expect(() => validateNotificationsSchema(invalidNotifications)).to.throw("Invalid notifications configuration");
});

it("should throw an error for missing alert fields", () => {
const invalidNotifications: GatusConfig = {
endpoints: [
{
name: "example-endpoint",
enabled: true,
url: "http://example.com",
method: "POST",
conditions: ["response-time < 500ms"],
interval: "1m",
group: "example-group",
alerts: [
{
type: "response-time",
"failure-threshold": 3,
// Missing success-threshold and other required fields
"send-on-resolved": true,
description: "Response time exceeded",
enabled: true
}
],
definition: {
title: "Example Endpoint",
description: "An example endpoint for testing"
}
} as Endpoint
]
};

expect(() => validateNotificationsSchema(invalidNotifications)).to.throw("Invalid notifications configuration");
});
});
});
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ __metadata:
dependencies:
"@dappnode/types": "npm:^0.1.40"
"@types/mocha": "npm:^10"
ajv: "npm:^8.12.0"
ajv: "npm:^8.17.1"
mocha: "npm:^10.7.0"
semver: "npm:^7.5.0"
languageName: unknown
Expand Down Expand Up @@ -5193,6 +5193,18 @@ __metadata:
languageName: node
linkType: hard

"ajv@npm:^8.17.1":
version: 8.17.1
resolution: "ajv@npm:8.17.1"
dependencies:
fast-deep-equal: "npm:^3.1.3"
fast-uri: "npm:^3.0.1"
json-schema-traverse: "npm:^1.0.0"
require-from-string: "npm:^2.0.2"
checksum: 10c0/ec3ba10a573c6b60f94639ffc53526275917a2df6810e4ab5a6b959d87459f9ef3f00d5e7865b82677cb7d21590355b34da14d1d0b9c32d75f95a187e76fff35
languageName: node
linkType: hard

"ansi-colors@npm:4.1.1":
version: 4.1.1
resolution: "ansi-colors@npm:4.1.1"
Expand Down Expand Up @@ -8670,6 +8682,13 @@ __metadata:
languageName: node
linkType: hard

"fast-uri@npm:^3.0.1":
version: 3.0.6
resolution: "fast-uri@npm:3.0.6"
checksum: 10c0/74a513c2af0584448aee71ce56005185f81239eab7a2343110e5bad50c39ad4fb19c5a6f99783ead1cac7ccaf3461a6034fda89fffa2b30b6d99b9f21c2f9d29
languageName: node
linkType: hard

"fastq@npm:^1.6.0":
version: 1.15.0
resolution: "fastq@npm:1.15.0"
Expand Down
Loading