Skip to content

Commit

Permalink
Improved JSON validation
Browse files Browse the repository at this point in the history
  • Loading branch information
koenekelschot committed May 1, 2020
1 parent 0ebc1f2 commit 7459456
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 5 deletions.
4 changes: 4 additions & 0 deletions releases/chrome/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.1.1
### Changed
- Improved JSON validation

## 0.1.0
### Added
- JSON is validated before copying
Expand Down
Binary file added releases/chrome/datadog-to-yaml-0.1.1.zip
Binary file not shown.
4 changes: 4 additions & 0 deletions releases/vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.1.1
### Changed
- Improved JSON validation

## 0.1.0
### Added
- JSON is validated before pasting
Expand Down
Binary file added releases/vscode/datadog-to-yaml-0.1.1.vsix
Binary file not shown.
2 changes: 1 addition & 1 deletion src/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Datadog to YAML",
"description": "Converts Datadog monitor JSON into YAML",
"version": "0.1.0",
"version": "0.1.1",
"icons": {
"32": "icon32.png",
"48": "icon48.png",
Expand Down
7 changes: 5 additions & 2 deletions src/monitorValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class MonitorValidator implements IMonitorValidator {
private servicethresholdsSchema: Schema = {
id: this.serviceThresholdsSchemaId,
type: "object",
required: ["ok", "critical", "warning", "unknown"],
required: ["ok", "critical", "warning"],
additionalProperties: false,
properties: {
ok: {
Expand Down Expand Up @@ -103,6 +103,9 @@ export class MonitorValidator implements IMonitorValidator {
timeout_h: {
type: "integer"
},
silenced: {
type: "object" //no idea what the possible contents are
},
new_host_delay: {
type: "integer"
},
Expand All @@ -113,7 +116,7 @@ export class MonitorValidator implements IMonitorValidator {
type: "boolean"
},
renotify_interval: {
type: "string", //should be an integer but the Datadog export returns a string
type: ["string", "integer"], //should be an integer but the Datadog export sometimes returns a string
pattern: "^\\d+$"
},
escalation_message: {
Expand Down
2 changes: 1 addition & 1 deletion src/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "datadog-to-yaml",
"displayName": "Datadog to YAML",
"description": "Converts Datadog monitor JSON into YAML",
"version": "0.1.0",
"version": "0.1.1",
"publisher": "koenekelschot",
"repository": "https://github.com/peckham/Datadog-to-YAML",
"engines": {
Expand Down
41 changes: 40 additions & 1 deletion test/monitorValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ describe("validate", () => {
expect(result.valid).toBe(true);
});

it("should validate when renotify_interval is an integer", () => {
const input = getDatadogExport();
input.options.renotify_interval = 4;

const result = validator.validate(input);

expect(result.valid).toBe(true);
});

it("should validate when renotify_interval is an integer string", () => {
const input = getDatadogExport();
input.options.renotify_interval = "4";
Expand Down Expand Up @@ -151,9 +160,12 @@ describe("validate", () => {
expect(result.errors[0].property).toBe("instance.options.thresholds");
});

it("should validate the Datadog UI JSON export", () => {
it("should validate the Datadog UI JSON exports", () => {
const result = validator.validate(getDatadogExport());
expect(result.valid).toBe(true);

const result2 = validator.validate(getDatadogExport2());
expect(result2.valid).toBe(true);
});
});

Expand Down Expand Up @@ -195,4 +207,31 @@ function getDatadogExport(): any {
},
"restricted_roles": null
}`);
}

function getDatadogExport2(): any {
return JSON.parse(`{
"id": 2,
"name": "Some other monitor name",
"type": "service check",
"query": "\\"datadog.agent.up\\".over(\\"*\\").by(\\"host\\").last(2).count_by_status()",
"message": "Shit has stopped working",
"tags": [],
"options": {
"notify_audit": false,
"locked": false,
"timeout_h": 0,
"silenced": {},
"include_tags": true,
"thresholds": {
"warning": 1,
"ok": 1,
"critical": 1
},
"new_host_delay": 300,
"notify_no_data": true,
"renotify_interval": 0,
"no_data_timeframe": 3
}
}`);
}

0 comments on commit 7459456

Please sign in to comment.