From 52ed2ba282752139f72f300ccff71aad6fa683fc Mon Sep 17 00:00:00 2001 From: martinforejt Date: Tue, 22 Oct 2024 20:03:10 +0200 Subject: [PATCH 1/4] feat(input_schema): Resource properties --- packages/input_schema/src/schema.json | 45 ++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/input_schema/src/schema.json b/packages/input_schema/src/schema.json index ce0f332d..15fd83b7 100644 --- a/packages/input_schema/src/schema.json +++ b/packages/input_schema/src/schema.json @@ -39,6 +39,8 @@ { "$ref": "#/definitions/objectProperty" }, { "$ref": "#/definitions/integerProperty" }, { "$ref": "#/definitions/booleanProperty" }, + { "$ref": "#/definitions/resourceProperty" }, + { "$ref": "#/definitions/resourceArrayProperty" }, { "$ref": "#/definitions/anyProperty" } ] } @@ -86,7 +88,7 @@ "title": { "type": "string" }, "description": { "type": "string" }, "nullable": { "type": "boolean" }, - "editor": { "enum": ["javascript", "python", "textfield", "textarea", "datepicker", "hidden", "dataset", "keyValueStore", "requestQueue"] }, + "editor": { "enum": ["javascript", "python", "textfield", "textarea", "datepicker", "hidden"] }, "isSecret": { "type": "boolean" } }, "required": ["type", "title", "description", "editor"], @@ -290,6 +292,47 @@ }, "required": ["type", "title", "description"] }, + "resourceProperty": { + "title": "Resource property", + "type": "object", + "additionalProperties": false, + "properties": { + "type": { "enum": ["string"] }, + "title": { "type": "string" }, + "description": { "type": "string" }, + "editor": { "enum": ["resourcePicker", "hidden"] }, + "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, + "default": { "type": "string" }, + "prefill": { "type": "string" }, + "example": { "type": "string" }, + "nullable": { "type": "boolean" }, + "sectionCaption": { "type": "string" }, + "sectionDescription": { "type": "string" } + }, + "required": ["type", "title", "description", "resourceType"] + }, + "resourceArrayProperty": { + "title": "Resource array property", + "type": "object", + "additionalProperties": false, + "properties": { + "type": { "enum": ["array"] }, + "title": { "type": "string" }, + "description": { "type": "string" }, + "editor": { "enum": ["resourcePicker", "hidden"] }, + "default": { "type": "array" }, + "prefill": { "type": "array" }, + "example": { "type": "array" }, + "nullable": { "type": "boolean" }, + "minItems": { "type": "integer" }, + "maxItems": { "type": "integer" }, + "uniqueItems": { "type": "boolean" }, + "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, + "sectionCaption": { "type": "string" }, + "sectionDescription": { "type": "string" } + }, + "required": ["type", "title", "description", "resourceType"] + }, "anyProperty": { "title": "Any property", "type": "object", From 9ad163bdc4f9118610a324892a282af0250c6693 Mon Sep 17 00:00:00 2001 From: martinforejt Date: Thu, 24 Oct 2024 23:45:27 +0200 Subject: [PATCH 2/4] feat(input_schema): Resource properties --- packages/input_schema/src/input_schema.ts | 10 +- packages/input_schema/src/schema.json | 121 ++++++++++++---------- packages/input_schema/src/types.ts | 17 ++- test/input_schema.test.ts | 2 +- test/input_schema_definition.test.ts | 91 ++++++++++++++++ test/utilities.client.test.ts | 68 ++++++++++++ 6 files changed, 253 insertions(+), 56 deletions(-) diff --git a/packages/input_schema/src/input_schema.ts b/packages/input_schema/src/input_schema.ts index 99439cf4..c65aaaea 100644 --- a/packages/input_schema/src/input_schema.ts +++ b/packages/input_schema/src/input_schema.ts @@ -3,6 +3,7 @@ import Ajv, { ErrorObject, Schema } from 'ajv'; import { m } from './intl'; import schema from './schema.json'; import { + CommonResourceFieldDefinition, FieldDefinition, InputSchema, InputSchemaBaseChecked, @@ -124,7 +125,7 @@ function validateField(validator: Ajv, fieldSchema: Record, fie return; } - // If there are more matching definitions then the type is string, and we need to get the right one. + // If there are more matching definitions then we need to get the right one. // If the definition contains "enum" property then it's enum type. if ((fieldSchema as StringFieldDefinition).enum) { const definition = matchingDefinitions.filter((item) => !!item.properties.enum).pop(); @@ -132,6 +133,13 @@ function validateField(validator: Ajv, fieldSchema: Record, fie validateAgainstSchemaOrThrow(validator, fieldSchema, definition, `schema.properties.${fieldKey}.enum`); return; } + // If the definition contains "resourceType" property then it's resource type. + if ((fieldSchema as CommonResourceFieldDefinition).resourceType) { + const definition = matchingDefinitions.filter((item) => !!item.properties.resourceType).pop(); + if (!definition) throw new Error('Input schema validation failed to find "resource property" definition'); + validateAgainstSchemaOrThrow(validator, fieldSchema, definition, `schema.properties.${fieldKey}.resourceType`); + return; + } // Otherwise we use the other definition. const definition = matchingDefinitions.filter((item) => !item.properties.enum).pop(); if (!definition) throw new Error('Input schema validation failed to find other than "enum property" definition'); diff --git a/packages/input_schema/src/schema.json b/packages/input_schema/src/schema.json index 15fd83b7..ec8279ef 100644 --- a/packages/input_schema/src/schema.json +++ b/packages/input_schema/src/schema.json @@ -32,17 +32,32 @@ "type": "object", "patternProperties": { "^": { - "oneOf": [ - { "$ref": "#/definitions/stringProperty" }, - { "$ref": "#/definitions/stringEnumProperty" }, - { "$ref": "#/definitions/arrayProperty" }, - { "$ref": "#/definitions/objectProperty" }, - { "$ref": "#/definitions/integerProperty" }, - { "$ref": "#/definitions/booleanProperty" }, - { "$ref": "#/definitions/resourceProperty" }, - { "$ref": "#/definitions/resourceArrayProperty" }, - { "$ref": "#/definitions/anyProperty" } - ] + "if": { + "properties": { + "resourceType": { + "not": { + "type": "string" + } + } + } + }, + "then": { + "oneOf": [ + { "$ref": "#/definitions/stringProperty" }, + { "$ref": "#/definitions/stringEnumProperty" }, + { "$ref": "#/definitions/arrayProperty" }, + { "$ref": "#/definitions/objectProperty" }, + { "$ref": "#/definitions/integerProperty" }, + { "$ref": "#/definitions/booleanProperty" }, + { "$ref": "#/definitions/anyProperty" } + ] + }, + "else": { + "oneOf": [ + { "$ref": "#/definitions/resourceProperty" }, + { "$ref": "#/definitions/resourceArrayProperty" } + ] + } } } } @@ -50,6 +65,47 @@ "additionalProperties": false, "required": ["title", "type", "properties", "schemaVersion"], "definitions": { + "resourceProperty": { + "title": "Resource property", + "type": "object", + "additionalProperties": false, + "properties": { + "type": { "enum": ["string"] }, + "title": { "type": "string" }, + "description": { "type": "string" }, + "editor": { "enum": ["resourcePicker", "hidden"] }, + "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, + "default": { "type": "string" }, + "prefill": { "type": "string" }, + "example": { "type": "string" }, + "nullable": { "type": "boolean" }, + "sectionCaption": { "type": "string" }, + "sectionDescription": { "type": "string" } + }, + "required": ["type", "title", "description", "resourceType"] + }, + "resourceArrayProperty": { + "title": "Resource array property", + "type": "object", + "additionalProperties": false, + "properties": { + "type": { "enum": ["array"] }, + "title": { "type": "string" }, + "description": { "type": "string" }, + "editor": { "enum": ["resourcePicker", "hidden"] }, + "default": { "type": "array" }, + "prefill": { "type": "array" }, + "example": { "type": "array" }, + "nullable": { "type": "boolean" }, + "minItems": { "type": "integer" }, + "maxItems": { "type": "integer" }, + "uniqueItems": { "type": "boolean" }, + "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, + "sectionCaption": { "type": "string" }, + "sectionDescription": { "type": "string" } + }, + "required": ["type", "title", "description", "resourceType"] + }, "stringEnumProperty": { "title": "Enum property", "type": "object", @@ -149,7 +205,7 @@ "nullable": { "type": "boolean" }, "minLength": { "type": "integer" }, "maxLength": { "type": "integer" }, - "editor": { "enum": ["javascript", "python", "textfield", "textarea", "datepicker", "hidden", "dataset", "keyValueStore", "requestQueue"] }, + "editor": { "enum": ["javascript", "python", "textfield", "textarea", "datepicker", "hidden"] }, "isSecret": { "type": "boolean" }, "sectionCaption": { "type": "string" }, "sectionDescription": { "type": "string" } @@ -292,47 +348,6 @@ }, "required": ["type", "title", "description"] }, - "resourceProperty": { - "title": "Resource property", - "type": "object", - "additionalProperties": false, - "properties": { - "type": { "enum": ["string"] }, - "title": { "type": "string" }, - "description": { "type": "string" }, - "editor": { "enum": ["resourcePicker", "hidden"] }, - "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, - "default": { "type": "string" }, - "prefill": { "type": "string" }, - "example": { "type": "string" }, - "nullable": { "type": "boolean" }, - "sectionCaption": { "type": "string" }, - "sectionDescription": { "type": "string" } - }, - "required": ["type", "title", "description", "resourceType"] - }, - "resourceArrayProperty": { - "title": "Resource array property", - "type": "object", - "additionalProperties": false, - "properties": { - "type": { "enum": ["array"] }, - "title": { "type": "string" }, - "description": { "type": "string" }, - "editor": { "enum": ["resourcePicker", "hidden"] }, - "default": { "type": "array" }, - "prefill": { "type": "array" }, - "example": { "type": "array" }, - "nullable": { "type": "boolean" }, - "minItems": { "type": "integer" }, - "maxItems": { "type": "integer" }, - "uniqueItems": { "type": "boolean" }, - "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, - "sectionCaption": { "type": "string" }, - "sectionDescription": { "type": "string" } - }, - "required": ["type", "title", "description", "resourceType"] - }, "anyProperty": { "title": "Any property", "type": "object", diff --git a/packages/input_schema/src/types.ts b/packages/input_schema/src/types.ts index 1b573cc4..aa3bd2e3 100644 --- a/packages/input_schema/src/types.ts +++ b/packages/input_schema/src/types.ts @@ -11,7 +11,7 @@ type CommonFieldDefinition = { export type StringFieldDefinition = CommonFieldDefinition & { type: 'string' - editor: 'textfield' | 'textarea' | 'javascript' | 'python' | 'select' | 'datepicker' | 'hidden' | 'json' | 'dataset' | 'keyValueStore' | 'requestQueue'; + editor: 'textfield' | 'textarea' | 'javascript' | 'python' | 'select' | 'datepicker' | 'hidden' | 'json'; pattern?: string; minLength?: number; maxLength?: number; @@ -61,6 +61,19 @@ export type ArrayFieldDefinition = CommonFieldDefinition> & { items?: unknown; } +export type CommonResourceFieldDefinition = CommonFieldDefinition & { + editor?: 'resourcePicker' | 'hidden'; + resourceType: 'dataset' | 'keyValueStore' | 'requestQueue' +} + +export type ResourceFieldDefinition = CommonResourceFieldDefinition & { + type: 'string' +} + +export type ResourceArrayFieldDefinition = CommonResourceFieldDefinition> & { + type: 'array' +} + type AllTypes = StringFieldDefinition['type'] | BooleanFieldDefinition['type'] | IntegerFieldDefinition['type'] @@ -78,6 +91,8 @@ export type FieldDefinition = StringFieldDefinition | ObjectFieldDefinition | ArrayFieldDefinition | MixedFieldDefinition + | ResourceFieldDefinition + | ResourceArrayFieldDefinition /** * Type with checked base, but not properties diff --git a/test/input_schema.test.ts b/test/input_schema.test.ts index 321768fc..80a1abbd 100644 --- a/test/input_schema.test.ts +++ b/test/input_schema.test.ts @@ -156,7 +156,7 @@ describe('input_schema.json', () => { expect(() => validateInputSchema(validator, schema)).toThrow( 'Input schema is not valid (Field schema.properties.myField.editor must be equal to one of the allowed values: ' - + '"javascript", "python", "textfield", "textarea", "datepicker", "hidden", "dataset", "keyValueStore", "requestQueue")', + + '"javascript", "python", "textfield", "textarea", "datepicker", "hidden")', ); }); diff --git a/test/input_schema_definition.test.ts b/test/input_schema_definition.test.ts index 0b049d7c..21db93f7 100644 --- a/test/input_schema_definition.test.ts +++ b/test/input_schema_definition.test.ts @@ -451,5 +451,96 @@ describe('input_schema.json', () => { + 'Set "allowAbsolute", "allowRelative" or both properties.'); }); }); + + describe('special cases for resourceProperty', () => { + it('should accept resourceType with editor', () => { + expect(ajv.validate(inputSchema, { + title: 'Test input schema', + type: 'object', + schemaVersion: 1, + properties: { + myField: { + title: 'Field title', + description: 'My test field', + type: 'string', + resourceType: 'dataset', + editor: 'resourcePicker', + }, + }, + })).toBe(true); + }); + + it('should accept resourceType without editor', () => { + expect(ajv.validate(inputSchema, { + title: 'Test input schema', + type: 'object', + schemaVersion: 1, + properties: { + myField: { + title: 'Field title', + description: 'My test field', + type: 'string', + resourceType: 'dataset', + }, + }, + })).toBe(true); + }); + + it('should accept array resourceType', () => { + expect(ajv.validate(inputSchema, { + title: 'Test input schema', + type: 'object', + schemaVersion: 1, + properties: { + myField: { + title: 'Field title', + description: 'My test field', + type: 'array', + resourceType: 'dataset', + }, + }, + })).toBe(true); + }); + + it('should not accept invalid resourceType', () => { + expect(ajv.validate(inputSchema, { + title: 'Test input schema', + type: 'object', + schemaVersion: 1, + properties: { + myField: { + title: 'Field title', + description: 'My test field', + type: 'string', + resourceType: 'xxx', + }, + }, + })).toBe(false); + expect(ajv.errorsText()).toContain('data/properties/myField/resourceType must be equal to one of the allowed values'); + expect(parseAjvError(ajv.errors![0], 'schema.properties.myField')?.message) + .toEqual('Field schema.properties.myField.resourceType must be equal to one of the allowed values: ' + + '"dataset", "keyValueStore", "requestQueue"'); + }); + + it('should not accept invalid editor', () => { + expect(ajv.validate(inputSchema, { + title: 'Test input schema', + type: 'object', + schemaVersion: 1, + properties: { + myField: { + title: 'Field title', + description: 'My test field', + type: 'string', + resourceType: 'keyValueStore', + editor: 'textfield', + }, + }, + })).toBe(false); + expect(ajv.errorsText()).toContain('data/properties/myField/editor must be equal to one of the allowed values'); + expect(parseAjvError(ajv.errors![0], 'schema.properties.myField')?.message) + .toEqual('Field schema.properties.myField.editor must be equal to one of the allowed values: "resourcePicker", "hidden"'); + }); + }); }); }); diff --git a/test/utilities.client.test.ts b/test/utilities.client.test.ts index 4083567e..8511f04e 100644 --- a/test/utilities.client.test.ts +++ b/test/utilities.client.test.ts @@ -1019,6 +1019,74 @@ describe('utilities.client', () => { }); }); + describe('special cases for resource property', () => { + it('should allow string value for single resource', () => { + const { inputSchema, validator } = buildInputSchema({ + field: { + title: 'Field title', + description: 'My test field', + type: 'string', + resourceType: 'dataset', + nullable: true, + }, + }); + const inputs = [ + // 2 invalid inputs + { field: [] }, + { field: {} }, + // Valid + { field: 'DATASET_ID' }, + { field: null }, + ]; + + const results = inputs + .map((input) => validateInputUsingValidator(validator, inputSchema, input)) + .filter((errors) => errors.length > 0); + + // There should be 2 invalid inputs + expect(results.length).toEqual(2); + results.forEach((result) => { + // Only one error should be thrown + expect(result.length).toEqual(1); + expect(result[0].fieldKey).toEqual('field'); + }); + }); + + it('should allow array value for multiple resource', () => { + const { inputSchema, validator } = buildInputSchema({ + field: { + title: 'Field title', + description: 'My test field', + type: 'array', + resourceType: 'dataset', + nullable: true, + }, + }); + const inputs = [ + // 2 invalid inputs + { field: 'DATASET_ID' }, + { field: {} }, + // Valid + { field: [] }, + { field: ['DATASET_ID'] }, + { field: ['DATASET_ID_1', 'DATASET_ID_2', 'DATASET_ID_3'] }, + { field: null }, + ]; + + const results = inputs + .map((input) => validateInputUsingValidator(validator, inputSchema, input)) + .filter((errors) => errors.length > 0); + + // There should be 2 invalid inputs + expect(results.length).toEqual(2); + results.forEach((result) => { + // Only one error should be thrown + expect(result.length).toEqual(1); + expect(result[0].fieldKey).toEqual('field'); + }); + }); + }); + /* TODO - enable this tests when the datepicker validation is back on describe('special cases for datepicker string type', () => { it('should allow absolute dates when allowAbsolute is omitted', () => { From 81db5e94bb706dab2ca98375dc22c23a86db80da Mon Sep 17 00:00:00 2001 From: martinforejt Date: Tue, 29 Oct 2024 11:11:54 +0100 Subject: [PATCH 3/4] remove patternProperties condition --- packages/input_schema/src/input_schema.ts | 4 +- packages/input_schema/src/schema.json | 119 ++++++++++------------ test/input_schema.test.ts | 42 ++++++++ test/input_schema_definition.test.ts | 40 -------- 4 files changed, 96 insertions(+), 109 deletions(-) diff --git a/packages/input_schema/src/input_schema.ts b/packages/input_schema/src/input_schema.ts index c65aaaea..07f42647 100644 --- a/packages/input_schema/src/input_schema.ts +++ b/packages/input_schema/src/input_schema.ts @@ -137,11 +137,11 @@ function validateField(validator: Ajv, fieldSchema: Record, fie if ((fieldSchema as CommonResourceFieldDefinition).resourceType) { const definition = matchingDefinitions.filter((item) => !!item.properties.resourceType).pop(); if (!definition) throw new Error('Input schema validation failed to find "resource property" definition'); - validateAgainstSchemaOrThrow(validator, fieldSchema, definition, `schema.properties.${fieldKey}.resourceType`); + validateAgainstSchemaOrThrow(validator, fieldSchema, definition, `schema.properties.${fieldKey}`); return; } // Otherwise we use the other definition. - const definition = matchingDefinitions.filter((item) => !item.properties.enum).pop(); + const definition = matchingDefinitions.filter((item) => !item.properties.enum && !item.properties.resourceType).pop(); if (!definition) throw new Error('Input schema validation failed to find other than "enum property" definition'); validateAgainstSchemaOrThrow(validator, fieldSchema, definition, `schema.properties.${fieldKey}`); diff --git a/packages/input_schema/src/schema.json b/packages/input_schema/src/schema.json index ec8279ef..4763996d 100644 --- a/packages/input_schema/src/schema.json +++ b/packages/input_schema/src/schema.json @@ -32,32 +32,17 @@ "type": "object", "patternProperties": { "^": { - "if": { - "properties": { - "resourceType": { - "not": { - "type": "string" - } - } - } - }, - "then": { - "oneOf": [ - { "$ref": "#/definitions/stringProperty" }, - { "$ref": "#/definitions/stringEnumProperty" }, - { "$ref": "#/definitions/arrayProperty" }, - { "$ref": "#/definitions/objectProperty" }, - { "$ref": "#/definitions/integerProperty" }, - { "$ref": "#/definitions/booleanProperty" }, - { "$ref": "#/definitions/anyProperty" } - ] - }, - "else": { - "oneOf": [ - { "$ref": "#/definitions/resourceProperty" }, - { "$ref": "#/definitions/resourceArrayProperty" } - ] - } + "oneOf": [ + { "$ref": "#/definitions/stringProperty" }, + { "$ref": "#/definitions/stringEnumProperty" }, + { "$ref": "#/definitions/arrayProperty" }, + { "$ref": "#/definitions/objectProperty" }, + { "$ref": "#/definitions/integerProperty" }, + { "$ref": "#/definitions/booleanProperty" }, + { "$ref": "#/definitions/resourceProperty" }, + { "$ref": "#/definitions/resourceArrayProperty" }, + { "$ref": "#/definitions/anyProperty" } + ] } } } @@ -65,47 +50,6 @@ "additionalProperties": false, "required": ["title", "type", "properties", "schemaVersion"], "definitions": { - "resourceProperty": { - "title": "Resource property", - "type": "object", - "additionalProperties": false, - "properties": { - "type": { "enum": ["string"] }, - "title": { "type": "string" }, - "description": { "type": "string" }, - "editor": { "enum": ["resourcePicker", "hidden"] }, - "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, - "default": { "type": "string" }, - "prefill": { "type": "string" }, - "example": { "type": "string" }, - "nullable": { "type": "boolean" }, - "sectionCaption": { "type": "string" }, - "sectionDescription": { "type": "string" } - }, - "required": ["type", "title", "description", "resourceType"] - }, - "resourceArrayProperty": { - "title": "Resource array property", - "type": "object", - "additionalProperties": false, - "properties": { - "type": { "enum": ["array"] }, - "title": { "type": "string" }, - "description": { "type": "string" }, - "editor": { "enum": ["resourcePicker", "hidden"] }, - "default": { "type": "array" }, - "prefill": { "type": "array" }, - "example": { "type": "array" }, - "nullable": { "type": "boolean" }, - "minItems": { "type": "integer" }, - "maxItems": { "type": "integer" }, - "uniqueItems": { "type": "boolean" }, - "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, - "sectionCaption": { "type": "string" }, - "sectionDescription": { "type": "string" } - }, - "required": ["type", "title", "description", "resourceType"] - }, "stringEnumProperty": { "title": "Enum property", "type": "object", @@ -348,6 +292,47 @@ }, "required": ["type", "title", "description"] }, + "resourceProperty": { + "title": "Resource property", + "type": "object", + "additionalProperties": false, + "properties": { + "type": { "enum": ["string"] }, + "title": { "type": "string" }, + "description": { "type": "string" }, + "editor": { "enum": ["resourcePicker", "hidden"] }, + "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, + "default": { "type": "string" }, + "prefill": { "type": "string" }, + "example": { "type": "string" }, + "nullable": { "type": "boolean" }, + "sectionCaption": { "type": "string" }, + "sectionDescription": { "type": "string" } + }, + "required": ["type", "title", "description", "resourceType"] + }, + "resourceArrayProperty": { + "title": "Resource array property", + "type": "object", + "additionalProperties": false, + "properties": { + "type": { "enum": ["array"] }, + "title": { "type": "string" }, + "description": { "type": "string" }, + "editor": { "enum": ["resourcePicker", "hidden"] }, + "default": { "type": "array" }, + "prefill": { "type": "array" }, + "example": { "type": "array" }, + "nullable": { "type": "boolean" }, + "minItems": { "type": "integer" }, + "maxItems": { "type": "integer" }, + "uniqueItems": { "type": "boolean" }, + "resourceType": { "enum": ["dataset", "keyValueStore", "requestQueue"] }, + "sectionCaption": { "type": "string" }, + "sectionDescription": { "type": "string" } + }, + "required": ["type", "title", "description", "resourceType"] + }, "anyProperty": { "title": "Any property", "type": "object", diff --git a/test/input_schema.test.ts b/test/input_schema.test.ts index 80a1abbd..50e4290d 100644 --- a/test/input_schema.test.ts +++ b/test/input_schema.test.ts @@ -278,5 +278,47 @@ describe('input_schema.json', () => { 'Field schema.properties.something does not exist, but it is specified in schema.required. Either define the field or remove it from schema.required.', ); }); + + describe('special cases for resourceProperty', () => { + it('should not accept invalid resourceType', () => { + const schema = { + title: 'Test input schema', + type: 'object', + schemaVersion: 1, + properties: { + myField: { + title: 'Field title', + description: 'My test field', + type: 'string', + resourceType: 'xxx', + }, + }, + }; + expect(() => validateInputSchema(validator, schema)).toThrow( + 'Input schema is not valid (Field schema.properties.myField.resourceType must be equal to one of the allowed values: ' + + '"dataset", "keyValueStore", "requestQueue")', + ); + }); + + it('should not accept invalid editor', () => { + const schema = { + title: 'Test input schema', + type: 'object', + schemaVersion: 1, + properties: { + myField: { + title: 'Field title', + description: 'My test field', + type: 'string', + resourceType: 'keyValueStore', + editor: 'textfield', + }, + }, + }; + expect(() => validateInputSchema(validator, schema)).toThrow( + 'Input schema is not valid (Field schema.properties.myField.editor must be equal to one of the allowed values: "resourcePicker", "hidden")', + ); + }); + }); }); }); diff --git a/test/input_schema_definition.test.ts b/test/input_schema_definition.test.ts index 21db93f7..8d1abde6 100644 --- a/test/input_schema_definition.test.ts +++ b/test/input_schema_definition.test.ts @@ -501,46 +501,6 @@ describe('input_schema.json', () => { }, })).toBe(true); }); - - it('should not accept invalid resourceType', () => { - expect(ajv.validate(inputSchema, { - title: 'Test input schema', - type: 'object', - schemaVersion: 1, - properties: { - myField: { - title: 'Field title', - description: 'My test field', - type: 'string', - resourceType: 'xxx', - }, - }, - })).toBe(false); - expect(ajv.errorsText()).toContain('data/properties/myField/resourceType must be equal to one of the allowed values'); - expect(parseAjvError(ajv.errors![0], 'schema.properties.myField')?.message) - .toEqual('Field schema.properties.myField.resourceType must be equal to one of the allowed values: ' - + '"dataset", "keyValueStore", "requestQueue"'); - }); - - it('should not accept invalid editor', () => { - expect(ajv.validate(inputSchema, { - title: 'Test input schema', - type: 'object', - schemaVersion: 1, - properties: { - myField: { - title: 'Field title', - description: 'My test field', - type: 'string', - resourceType: 'keyValueStore', - editor: 'textfield', - }, - }, - })).toBe(false); - expect(ajv.errorsText()).toContain('data/properties/myField/editor must be equal to one of the allowed values'); - expect(parseAjvError(ajv.errors![0], 'schema.properties.myField')?.message) - .toEqual('Field schema.properties.myField.editor must be equal to one of the allowed values: "resourcePicker", "hidden"'); - }); }); }); }); From fbef59bc81a7e24c4d9dc3f35098541778a5b221 Mon Sep 17 00:00:00 2001 From: martinforejt Date: Wed, 30 Oct 2024 10:15:07 +0100 Subject: [PATCH 4/4] code style to keep consistency --- packages/input_schema/src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/input_schema/src/types.ts b/packages/input_schema/src/types.ts index aa3bd2e3..515246a0 100644 --- a/packages/input_schema/src/types.ts +++ b/packages/input_schema/src/types.ts @@ -70,7 +70,7 @@ export type ResourceFieldDefinition = CommonResourceFieldDefinition & { type: 'string' } -export type ResourceArrayFieldDefinition = CommonResourceFieldDefinition> & { +export type ResourceArrayFieldDefinition = CommonResourceFieldDefinition & { type: 'array' }