Skip to content

Commit 1446c6a

Browse files
committed
feat: Updated new match schema. This is used to match resources when multiple exists
1 parent b1b0432 commit 1446c6a

9 files changed

Lines changed: 229 additions & 28 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-schemas",
3-
"version": "1.0.69",
3+
"version": "1.0.73",
44
"description": "",
55
"type": "module",
66
"main": "dist/index.js",

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import GetResourceInfoRequestDataSchema from './messages/get-resource-info-reque
1010
import GetResourceInfoResponseDataSchema from './messages/get-resource-info-response-data-schema.json' with {type: 'json'}
1111
import ImportRequestDataSchema from './messages/import-request-data-schema.json' with {type: 'json'}
1212
import ImportResponseDataSchema from './messages/import-response-data-schema.json' with {type: 'json'}
13+
import MatchRequestDataSchema from './messages/match-request-data-schema.json' with {type: 'json'}
14+
import MatchResponseDataSchema from './messages/match-response-data-schema.json' with {type: 'json'}
1315
import PlanRequestDataSchema from './messages/plan-request-data-schema.json' with {type: 'json'}
1416
import PlanResponseDataSchema from './messages/plan-response-data-schema.json' with {type: 'json'}
1517
import ValidateRequestDataSchema from './messages/validate-request-data-schema.json' with {type: 'json'}
@@ -32,6 +34,8 @@ export {
3234
GetResourceInfoResponseDataSchema,
3335
ImportRequestDataSchema,
3436
ImportResponseDataSchema,
37+
MatchRequestDataSchema,
38+
MatchResponseDataSchema,
3539
PlanRequestDataSchema,
3640
PlanResponseDataSchema,
3741
ValidateRequestDataSchema,

src/messages/get-resource-info-response-data-schema.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"importAndDestroy": {
3737
"type": "object",
3838
"properties": {
39-
"requiredProperties": {
39+
"requiredParameters": {
4040
"type": "array",
4141
"items": {
4242
"type": "string"
@@ -50,16 +50,7 @@
5050
}
5151
},
5252
"allowMultiple": {
53-
"type": "object",
54-
"properties": {
55-
"requiredProperties": {
56-
"type": "array",
57-
"items": {
58-
"type": "string"
59-
}
60-
}
61-
},
62-
"required": ["requiredProperties"]
53+
"type": "boolean"
6354
}
6455
},
6556
"required": ["type", "plugin"]

src/messages/get-resource-info-response-data-schema.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ describe('Get resources response data schema', () => {
5353
},
5454
importAndDestroy: {
5555
preventImport: true,
56-
requiredProperties: ['plugin']
56+
requiredParameters: ['plugin']
5757
},
58-
allowMultiple: {
59-
requiredProperties: ['plugin']
60-
}
58+
allowMultiple: true,
6159
})).to.be.true;
6260

6361
expect(validate({
@@ -106,9 +104,7 @@ describe('Get resources response data schema', () => {
106104
preventImport: true,
107105
requiredParameters: ['plugin'],
108106
},
109-
allowMultiple: {
110-
requiredParameters: ['plugin']
111-
}
107+
allowMultiple: false,
112108
}
113109
})
114110

@@ -143,6 +139,12 @@ describe('Get resources response data schema', () => {
143139
}
144140

145141
})).to.be.false;
142+
143+
expect(validate({
144+
type: 'type',
145+
plugin: 'core-plugin',
146+
allowMultiple: {}
147+
})).to.be.false;
146148
})
147149

148150
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "https://www.codifycli.com/match-request-data-schema.json",
4+
"title": "Match resources request data",
5+
"type": "object",
6+
"properties": {
7+
"resource": {
8+
"type": "object",
9+
"properties": {
10+
"core": {
11+
"$ref": "resource-schema.json"
12+
},
13+
"parameters": {
14+
"type": "object"
15+
}
16+
},
17+
"required": ["core", "parameters"]
18+
},
19+
"array": {
20+
"type": "array",
21+
"items": {
22+
"type": "object",
23+
"properties": {
24+
"core": {
25+
"$ref": "resource-schema.json"
26+
},
27+
"parameters": {
28+
"type": "object"
29+
}
30+
},
31+
"required": ["core", "parameters"]
32+
}
33+
}
34+
},
35+
"required": ["resource", "array"]
36+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import schema from './match-request-data-schema.json';
2+
import resourceSchema from '../resource-schema.json'
3+
import { describe, it, expect } from 'vitest'
4+
import Ajv from 'ajv'
5+
6+
const ajv = new Ajv({
7+
strict: true,
8+
strictRequired: false,
9+
})
10+
ajv.addSchema(resourceSchema);
11+
12+
describe('Matches request data schema', () => {
13+
it('compiles', () => {
14+
ajv.compile(schema);
15+
})
16+
17+
it("Validates a correct request", () => {
18+
const validate = ajv.compile(schema);
19+
expect(validate({
20+
resource: {
21+
core: {
22+
type: "type"
23+
},
24+
parameters: {
25+
param1: 'a',
26+
param2: 2
27+
}
28+
},
29+
array: [
30+
{
31+
core: {
32+
type: "type"
33+
},
34+
parameters: {
35+
param1: 'a',
36+
param2: 2
37+
}
38+
},
39+
{
40+
core: {
41+
type: "type2"
42+
},
43+
parameters: {
44+
param1: 'a',
45+
param2: 2
46+
}
47+
}
48+
]
49+
})).to.be.true;
50+
})
51+
52+
it("Rejects an invalid request", () => {
53+
const validate = ajv.compile(schema);
54+
expect(validate({
55+
resource: {
56+
core: {
57+
type: "type"
58+
},
59+
parameters: {
60+
param1: 'a',
61+
param2: 2
62+
}
63+
},
64+
array: [
65+
{
66+
core: {
67+
type: "type"
68+
},
69+
parameters: {
70+
param1: 'a',
71+
param2: 2
72+
}
73+
},
74+
{
75+
core: {
76+
},
77+
parameters: {
78+
param1: 'a',
79+
param2: 2
80+
}
81+
}
82+
]
83+
})).to.be.false;
84+
})
85+
86+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "https://www.codifycli.com/match-response-data-schema.json",
4+
"title": "Match resources response data",
5+
"type": "object",
6+
"properties": {
7+
"match": {
8+
"type": "object",
9+
"properties": {
10+
"core": {
11+
"$ref": "resource-schema.json"
12+
},
13+
"parameters": {
14+
"type": "object"
15+
}
16+
},
17+
"required": ["core", "parameters"]
18+
}
19+
}
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import schema from './match-response-data-schema.json';
2+
import resourceSchema from '../resource-schema.json'
3+
import { describe, it, expect } from 'vitest'
4+
import Ajv from 'ajv'
5+
6+
const ajv = new Ajv({
7+
strict: true,
8+
strictRequired: false,
9+
})
10+
ajv.addSchema(resourceSchema);
11+
12+
describe('Match response data schema', () => {
13+
it('compiles', () => {
14+
ajv.compile(schema);
15+
})
16+
17+
it("Validates a correct response", () => {
18+
const validate = ajv.compile(schema);
19+
expect(validate({
20+
match: {
21+
core: {
22+
type: "type"
23+
},
24+
parameters: {
25+
param1: 'a',
26+
param2: 2
27+
}
28+
}
29+
})).to.be.true;
30+
})
31+
32+
it("Rejects an invalid response", () => {
33+
const validate = ajv.compile(schema);
34+
expect(validate({
35+
match: {
36+
core: {
37+
},
38+
parameters: {
39+
param1: 'a',
40+
param2: 2
41+
}
42+
}
43+
})).to.be.false;
44+
})
45+
46+
})

src/types/index.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,36 @@ export interface GetResourceInfoRequestData {
104104
export interface GetResourceInfoResponseData {
105105
plugin: string;
106106
type: string;
107-
schema?: Record<string, unknown>,
108-
dependencies?: string[],
107+
schema?: Record<string, unknown>;
108+
dependencies?: string[];
109109
/**
110110
* @deprecated: Use import and destroy instead.
111111
*/
112112
import?: {
113-
requiredParameters: string[] | null,
113+
requiredParameters: string[] | null;
114114
},
115115
importAndDestroy?: {
116-
requiredParameters: string[] | null,
117-
preventImport?: boolean,
116+
requiredParameters: string[] | null;
117+
preventImport?: boolean;
118118
},
119-
allowMultiple?: {
120-
requiredParameters: string[]
119+
allowMultiple: boolean;
120+
}
121+
122+
export interface MatchRequestData {
123+
resource: {
124+
core: ResourceConfig;
125+
parameters: Record<string, unknown>;
126+
};
127+
array: Array<{
128+
core: ResourceConfig;
129+
parameters: Record<string, unknown>;
130+
}>
131+
}
132+
133+
export interface MatchResponseData {
134+
match?: {
135+
core: ResourceConfig;
136+
parameters: Record<string, unknown>;
121137
}
122138
}
123139

@@ -127,8 +143,8 @@ export interface ImportRequestData {
127143
}
128144

129145
export interface ImportResponseData {
130-
request: ResourceJson
131-
result: Array<ResourceJson>,
146+
request: ResourceJson;
147+
result: Array<ResourceJson>;
132148
}
133149

134150
export interface ApplyRequestData {

0 commit comments

Comments
 (0)