Skip to content

Commit aa3b387

Browse files
committed
Added new json schema repo
1 parent 9c7cf80 commit aa3b387

13 files changed

Lines changed: 421 additions & 0 deletions

codify-schemas/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
**/.DS_Store
2+
*-debug.log
3+
*-error.log
4+
/.idea
5+
/.nyc_output
6+
/dist
7+
/lib
8+
/package-lock.json
9+
/tmp
10+
/yarn.lock
11+
node_modules
12+
oclif.manifest.json

codify-schemas/.mocharc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"require": "ts-node/register",
3+
"extensions": ["ts"],
4+
"spec": [
5+
"src/**/*.test.ts"
6+
]
7+
}

codify-schemas/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "codify-schemas",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"ajv": "^8.12.0"
14+
},
15+
"devDependencies": {
16+
"@types/chai": "^4.3.11",
17+
"@types/mocha": "^10.0.6",
18+
"chai": "^5.0.3",
19+
"mocha": "^10.2.0",
20+
"ts-node": "^10.9.2",
21+
"typescript": "^5.3.3"
22+
}
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "https://www.codify.com/config-file-schema.json",
4+
"title": "Config file Schema",
5+
"type": "array",
6+
"items": {
7+
"$ref": "resource-schema.json"
8+
}
9+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Ajv2020 from "ajv/dist/2020";
2+
import configSchema from './config-file-schema.json';
3+
import resourceSchema from './resource-schema.json';
4+
import assert from "node:assert";
5+
6+
const ajv = new Ajv2020({
7+
strict: true,
8+
})
9+
ajv.addSchema(resourceSchema);
10+
11+
describe("config file schema tests", () => {
12+
it('compiles', () => {
13+
ajv.compile(configSchema);
14+
})
15+
16+
it('accepts resource blocks', () => {
17+
const validator = ajv.compile(configSchema);
18+
19+
assert.equal(validator([
20+
{
21+
"type": "resource1",
22+
},
23+
{
24+
"type": "resource2",
25+
"name": "abc",
26+
"prop1": {
27+
"a": "b",
28+
},
29+
"prop2": "c"
30+
}
31+
]), true)
32+
33+
assert.equal(validator([
34+
{
35+
"type": "resource1",
36+
},
37+
{}
38+
]), false)
39+
40+
assert.equal(validator([
41+
{
42+
"type": "project",
43+
},
44+
{
45+
"type": "resource2"
46+
}
47+
]), true)
48+
49+
})
50+
51+
52+
})

codify-schemas/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { default as ConfigFileSchema } from './config-file-schema.json';
2+
export { default as ProjectSchema } from './project-schema.json';
3+
export { default as ResourceMetaSchema } from './resource-meta-schema.json';
4+
export { default as ResourceSchema } from './resource-schema.json';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "https://www.codify.com/resource-schema.json",
4+
"title": "Resource Schema",
5+
"type": "object",
6+
"properties": {
7+
"type": {
8+
"description": "All project configs are of the type project",
9+
"type": "string",
10+
"const": "project"
11+
},
12+
"version": {
13+
"description": "Semver version. Codify will throw an error if this is not satisfied",
14+
"type": "string",
15+
"pattern": "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z-]+)?$"
16+
},
17+
"plugins": {
18+
"type": "object",
19+
"patternProperties": {
20+
".*": {
21+
"type": "string"
22+
}
23+
}
24+
},
25+
"description": {
26+
"description": "An optional description of the codify project",
27+
"type": "string"
28+
}
29+
},
30+
"additionalProperties": false,
31+
"required": ["type"]
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Ajv2020 from "ajv/dist/2020";
2+
import schema from './project-schema.json';
3+
import assert from "node:assert";
4+
5+
const ajv = new Ajv2020({
6+
strict: true,
7+
})
8+
9+
describe("project file schema tests", () => {
10+
it('compiles', () => {
11+
ajv.compile(schema);
12+
})
13+
14+
it("must have type project", () => {
15+
const validator = ajv.compile(schema);
16+
assert.equal(validator({ type: 'project' }), true)
17+
assert.equal(validator({}), false)
18+
assert.equal(validator({ type: 'resource' }), false)
19+
})
20+
21+
it("plugins must be <string, string>", () => {
22+
const validator = ajv.compile(schema);
23+
assert.equal(validator({
24+
type: 'project',
25+
plugins: {
26+
"plugin1": "3.2.3"
27+
}
28+
}), true)
29+
30+
assert.equal(validator({
31+
type: 'project',
32+
plugins: {
33+
"plugin1": 1,
34+
}
35+
}), false)
36+
37+
assert.equal(validator({
38+
type: 'project',
39+
plugins: {
40+
"plugins2": "https://link.to.plugin.com"
41+
}
42+
}), true)
43+
})
44+
45+
46+
47+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "Config file Schema",
4+
"type": "object",
5+
"properties": {
6+
"$id": {
7+
"type": "string",
8+
"pattern": "^https://www.codify.com/"
9+
},
10+
"type": {
11+
"const": "object"
12+
},
13+
"properties": {
14+
"type": "object",
15+
"properties": {
16+
"type": false,
17+
"name": false,
18+
"dependsOn": false,
19+
"$ref": {
20+
"const": "https://www.codify.com/resource-schema.json#properties"
21+
}
22+
}
23+
},
24+
"$ref": {
25+
"const": "https://www.codify.com/resource-schema.json"
26+
},
27+
"unevaluatedProperties": {
28+
"const": false
29+
}
30+
},
31+
"required": ["unevaluatedProperties", "$ref"]
32+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import Ajv2020 from "ajv/dist/2020";
2+
import { describe, it } from 'mocha';
3+
import schema from './resource-meta-schema.json';
4+
import resourceSchema from './resource-schema.json';
5+
import assert from "node:assert";
6+
7+
const ajv = new Ajv2020({
8+
strict: true,
9+
})
10+
11+
describe("Resource meta schema tests", () => {
12+
it("compiles", () => {
13+
ajv.compile(schema);
14+
})
15+
16+
it("must have additional properties and $ref to resource-schema specified", () => {
17+
const validate = ajv.compile(schema);
18+
19+
assert.equal(validate({
20+
"$schema": "https://json-schema.org/draft/2020-12/schema",
21+
"type": "object",
22+
"properties": {
23+
"prop1": {
24+
"type": "string"
25+
}
26+
}
27+
}), false);
28+
29+
assert.equal(validate({
30+
"$schema": "https://json-schema.org/draft/2020-12/schema",
31+
"type": "object",
32+
"properties": {
33+
"prop1": {
34+
"type": "string"
35+
}
36+
},
37+
"additionalProperties": false,
38+
"$ref": "https://www.codify.com/resource-schema.json"
39+
}), true);
40+
})
41+
42+
it("does not allow resource keywords to be specified", () => {
43+
const validate = ajv.compile(schema);
44+
45+
assert.equal(validate({
46+
"$schema": "https://json-schema.org/draft/2020-12/schema",
47+
"type": "object",
48+
"properties": {
49+
"type": {
50+
"type": "string"
51+
}
52+
},
53+
"additionalProperties": false,
54+
"$ref": "https://www.codify.com/resource-schema.json"
55+
}), false);
56+
57+
assert.equal(validate({
58+
"$schema": "https://json-schema.org/draft/2020-12/schema",
59+
"type": "object",
60+
"properties": {
61+
"name": {
62+
"type": "string"
63+
}
64+
},
65+
"additionalProperties": false,
66+
"$ref": "https://www.codify.com/resource-schema.json"
67+
}), false);
68+
69+
assert.equal(validate({
70+
"$schema": "https://json-schema.org/draft/2020-12/schema",
71+
"type": "object",
72+
"properties": {
73+
"dependsOn": {
74+
"type": "array"
75+
}
76+
},
77+
"additionalProperties": false,
78+
"$ref": "https://www.codify.com/resource-schema.json"
79+
}), false);
80+
})
81+
82+
it("allows for schema composition with base resource schema", () => {
83+
const resourceMetaSchema = schema;
84+
const propertySchema = {
85+
$id: "https://www.codify.com/property-schema.json",
86+
type: "object",
87+
properties: {
88+
prop1: {
89+
type: "string"
90+
},
91+
prop2: {
92+
type: "string"
93+
},
94+
},
95+
$ref: "https://www.codify.com/resource-schema.json",
96+
unevaluatedProperties: false,
97+
}
98+
99+
const resourceConfigInvalid = {
100+
prop1: "a",
101+
}
102+
103+
const resourceConfigValid = {
104+
type: "resource-config",
105+
prop1: "a",
106+
dependsOn: ["resource-config-2"]
107+
}
108+
109+
const ajv = new Ajv2020({
110+
strict: true,
111+
schemas: [resourceSchema]
112+
});
113+
114+
const metaSchemaValidator = ajv.compile(resourceMetaSchema);
115+
assert.equal(metaSchemaValidator(propertySchema), true)
116+
117+
const resourceValidator = ajv.compile(propertySchema);
118+
assert.equal(resourceValidator(resourceConfigValid), true);
119+
assert.equal(resourceValidator(resourceConfigInvalid), false);
120+
})
121+
});

0 commit comments

Comments
 (0)