Skip to content

Commit 81db5e9

Browse files
committed
remove patternProperties condition
1 parent 9ad163b commit 81db5e9

File tree

4 files changed

+96
-109
lines changed

4 files changed

+96
-109
lines changed

packages/input_schema/src/input_schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ function validateField(validator: Ajv, fieldSchema: Record<string, unknown>, fie
137137
if ((fieldSchema as CommonResourceFieldDefinition<unknown>).resourceType) {
138138
const definition = matchingDefinitions.filter((item) => !!item.properties.resourceType).pop();
139139
if (!definition) throw new Error('Input schema validation failed to find "resource property" definition');
140-
validateAgainstSchemaOrThrow(validator, fieldSchema, definition, `schema.properties.${fieldKey}.resourceType`);
140+
validateAgainstSchemaOrThrow(validator, fieldSchema, definition, `schema.properties.${fieldKey}`);
141141
return;
142142
}
143143
// Otherwise we use the other definition.
144-
const definition = matchingDefinitions.filter((item) => !item.properties.enum).pop();
144+
const definition = matchingDefinitions.filter((item) => !item.properties.enum && !item.properties.resourceType).pop();
145145
if (!definition) throw new Error('Input schema validation failed to find other than "enum property" definition');
146146

147147
validateAgainstSchemaOrThrow(validator, fieldSchema, definition, `schema.properties.${fieldKey}`);

packages/input_schema/src/schema.json

Lines changed: 52 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -32,80 +32,24 @@
3232
"type": "object",
3333
"patternProperties": {
3434
"^": {
35-
"if": {
36-
"properties": {
37-
"resourceType": {
38-
"not": {
39-
"type": "string"
40-
}
41-
}
42-
}
43-
},
44-
"then": {
45-
"oneOf": [
46-
{ "$ref": "#/definitions/stringProperty" },
47-
{ "$ref": "#/definitions/stringEnumProperty" },
48-
{ "$ref": "#/definitions/arrayProperty" },
49-
{ "$ref": "#/definitions/objectProperty" },
50-
{ "$ref": "#/definitions/integerProperty" },
51-
{ "$ref": "#/definitions/booleanProperty" },
52-
{ "$ref": "#/definitions/anyProperty" }
53-
]
54-
},
55-
"else": {
56-
"oneOf": [
57-
{ "$ref": "#/definitions/resourceProperty" },
58-
{ "$ref": "#/definitions/resourceArrayProperty" }
59-
]
60-
}
35+
"oneOf": [
36+
{ "$ref": "#/definitions/stringProperty" },
37+
{ "$ref": "#/definitions/stringEnumProperty" },
38+
{ "$ref": "#/definitions/arrayProperty" },
39+
{ "$ref": "#/definitions/objectProperty" },
40+
{ "$ref": "#/definitions/integerProperty" },
41+
{ "$ref": "#/definitions/booleanProperty" },
42+
{ "$ref": "#/definitions/resourceProperty" },
43+
{ "$ref": "#/definitions/resourceArrayProperty" },
44+
{ "$ref": "#/definitions/anyProperty" }
45+
]
6146
}
6247
}
6348
}
6449
},
6550
"additionalProperties": false,
6651
"required": ["title", "type", "properties", "schemaVersion"],
6752
"definitions": {
68-
"resourceProperty": {
69-
"title": "Resource property",
70-
"type": "object",
71-
"additionalProperties": false,
72-
"properties": {
73-
"type": { "enum": ["string"] },
74-
"title": { "type": "string" },
75-
"description": { "type": "string" },
76-
"editor": { "enum": ["resourcePicker", "hidden"] },
77-
"resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] },
78-
"default": { "type": "string" },
79-
"prefill": { "type": "string" },
80-
"example": { "type": "string" },
81-
"nullable": { "type": "boolean" },
82-
"sectionCaption": { "type": "string" },
83-
"sectionDescription": { "type": "string" }
84-
},
85-
"required": ["type", "title", "description", "resourceType"]
86-
},
87-
"resourceArrayProperty": {
88-
"title": "Resource array property",
89-
"type": "object",
90-
"additionalProperties": false,
91-
"properties": {
92-
"type": { "enum": ["array"] },
93-
"title": { "type": "string" },
94-
"description": { "type": "string" },
95-
"editor": { "enum": ["resourcePicker", "hidden"] },
96-
"default": { "type": "array" },
97-
"prefill": { "type": "array" },
98-
"example": { "type": "array" },
99-
"nullable": { "type": "boolean" },
100-
"minItems": { "type": "integer" },
101-
"maxItems": { "type": "integer" },
102-
"uniqueItems": { "type": "boolean" },
103-
"resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] },
104-
"sectionCaption": { "type": "string" },
105-
"sectionDescription": { "type": "string" }
106-
},
107-
"required": ["type", "title", "description", "resourceType"]
108-
},
10953
"stringEnumProperty": {
11054
"title": "Enum property",
11155
"type": "object",
@@ -348,6 +292,47 @@
348292
},
349293
"required": ["type", "title", "description"]
350294
},
295+
"resourceProperty": {
296+
"title": "Resource property",
297+
"type": "object",
298+
"additionalProperties": false,
299+
"properties": {
300+
"type": { "enum": ["string"] },
301+
"title": { "type": "string" },
302+
"description": { "type": "string" },
303+
"editor": { "enum": ["resourcePicker", "hidden"] },
304+
"resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] },
305+
"default": { "type": "string" },
306+
"prefill": { "type": "string" },
307+
"example": { "type": "string" },
308+
"nullable": { "type": "boolean" },
309+
"sectionCaption": { "type": "string" },
310+
"sectionDescription": { "type": "string" }
311+
},
312+
"required": ["type", "title", "description", "resourceType"]
313+
},
314+
"resourceArrayProperty": {
315+
"title": "Resource array property",
316+
"type": "object",
317+
"additionalProperties": false,
318+
"properties": {
319+
"type": { "enum": ["array"] },
320+
"title": { "type": "string" },
321+
"description": { "type": "string" },
322+
"editor": { "enum": ["resourcePicker", "hidden"] },
323+
"default": { "type": "array" },
324+
"prefill": { "type": "array" },
325+
"example": { "type": "array" },
326+
"nullable": { "type": "boolean" },
327+
"minItems": { "type": "integer" },
328+
"maxItems": { "type": "integer" },
329+
"uniqueItems": { "type": "boolean" },
330+
"resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] },
331+
"sectionCaption": { "type": "string" },
332+
"sectionDescription": { "type": "string" }
333+
},
334+
"required": ["type", "title", "description", "resourceType"]
335+
},
351336
"anyProperty": {
352337
"title": "Any property",
353338
"type": "object",

test/input_schema.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,5 +278,47 @@ describe('input_schema.json', () => {
278278
'Field schema.properties.something does not exist, but it is specified in schema.required. Either define the field or remove it from schema.required.',
279279
);
280280
});
281+
282+
describe('special cases for resourceProperty', () => {
283+
it('should not accept invalid resourceType', () => {
284+
const schema = {
285+
title: 'Test input schema',
286+
type: 'object',
287+
schemaVersion: 1,
288+
properties: {
289+
myField: {
290+
title: 'Field title',
291+
description: 'My test field',
292+
type: 'string',
293+
resourceType: 'xxx',
294+
},
295+
},
296+
};
297+
expect(() => validateInputSchema(validator, schema)).toThrow(
298+
'Input schema is not valid (Field schema.properties.myField.resourceType must be equal to one of the allowed values: '
299+
+ '"dataset", "keyValueStore", "requestQueue")',
300+
);
301+
});
302+
303+
it('should not accept invalid editor', () => {
304+
const schema = {
305+
title: 'Test input schema',
306+
type: 'object',
307+
schemaVersion: 1,
308+
properties: {
309+
myField: {
310+
title: 'Field title',
311+
description: 'My test field',
312+
type: 'string',
313+
resourceType: 'keyValueStore',
314+
editor: 'textfield',
315+
},
316+
},
317+
};
318+
expect(() => validateInputSchema(validator, schema)).toThrow(
319+
'Input schema is not valid (Field schema.properties.myField.editor must be equal to one of the allowed values: "resourcePicker", "hidden")',
320+
);
321+
});
322+
});
281323
});
282324
});

test/input_schema_definition.test.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -501,46 +501,6 @@ describe('input_schema.json', () => {
501501
},
502502
})).toBe(true);
503503
});
504-
505-
it('should not accept invalid resourceType', () => {
506-
expect(ajv.validate(inputSchema, {
507-
title: 'Test input schema',
508-
type: 'object',
509-
schemaVersion: 1,
510-
properties: {
511-
myField: {
512-
title: 'Field title',
513-
description: 'My test field',
514-
type: 'string',
515-
resourceType: 'xxx',
516-
},
517-
},
518-
})).toBe(false);
519-
expect(ajv.errorsText()).toContain('data/properties/myField/resourceType must be equal to one of the allowed values');
520-
expect(parseAjvError(ajv.errors![0], 'schema.properties.myField')?.message)
521-
.toEqual('Field schema.properties.myField.resourceType must be equal to one of the allowed values: '
522-
+ '"dataset", "keyValueStore", "requestQueue"');
523-
});
524-
525-
it('should not accept invalid editor', () => {
526-
expect(ajv.validate(inputSchema, {
527-
title: 'Test input schema',
528-
type: 'object',
529-
schemaVersion: 1,
530-
properties: {
531-
myField: {
532-
title: 'Field title',
533-
description: 'My test field',
534-
type: 'string',
535-
resourceType: 'keyValueStore',
536-
editor: 'textfield',
537-
},
538-
},
539-
})).toBe(false);
540-
expect(ajv.errorsText()).toContain('data/properties/myField/editor must be equal to one of the allowed values');
541-
expect(parseAjvError(ajv.errors![0], 'schema.properties.myField')?.message)
542-
.toEqual('Field schema.properties.myField.editor must be equal to one of the allowed values: "resourcePicker", "hidden"');
543-
});
544504
});
545505
});
546506
});

0 commit comments

Comments
 (0)