Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions packages/input_schema/src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,58 @@
"stringProperty": {
"title": "String property",
"type": "object",
"additionalProperties": false,
"additionalProperties": true,
"properties": {
"type": { "enum": ["string"] },
"title": { "type": "string" },
"description": { "type": "string" },
"default": { "type": "string" },
"prefill": { "type": "string" },
"example": { "type": "string" },
"pattern": { "type": "string" },
"nullable": { "type": "boolean" },
"minLength": { "type": "integer" },
"maxLength": { "type": "integer" },
"editor": { "enum": ["javascript", "python", "textfield", "textarea", "hidden"] },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" }
"isSecret": { "type": "boolean" }
},
"required": ["type", "title", "description", "editor"]
"required": ["type", "title", "description", "editor"],
"if": {
"properties": {
"isSecret": {
"not": {
"const": true
}
}
}
},
"then": {
"additionalProperties": false,
"properties": {
"type": { "enum": ["string"] },
"title": { "type": "string" },
"description": { "type": "string" },
"default": { "type": "string" },
"prefill": { "type": "string" },
"example": { "type": "string" },
"pattern": { "type": "string" },
"nullable": { "type": "boolean" },
"minLength": { "type": "integer" },
"maxLength": { "type": "integer" },
"editor": { "enum": ["javascript", "python", "textfield", "textarea", "hidden"] },
"isSecret": { "type": "boolean" },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" }
}
},
"else": {
"additionalProperties": false,
"properties": {
"type": { "enum": ["string"] },
"title": { "type": "string" },
"description": { "type": "string" },
"example": { "type": "string" },
"nullable": { "type": "boolean" },
"editor": { "enum": ["textfield", "textarea", "hidden"] },
"isSecret": { "type": "boolean" },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" }
}
}
},
"arrayProperty": {
"title": "Array property",
Expand Down
43 changes: 43 additions & 0 deletions test/input_schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,49 @@ describe('input_schema.json', () => {
);
});

it('should throw correct error on field in if/else definition', () => {
const schema = {
title: 'Test input schema',
type: 'object',
schemaVersion: 1,
properties: {
myField: {
title: 'Field title',
type: 'string',
description: 'Some description ...',
editor: 'textfield',
maxLength: true,
},
},
};

expect(() => validateInputSchema(validator, schema)).toThrow(
'Input schema is not valid (Field schema.properties.myField.maxLength must be integer)',
);
});

it('should throw correct error in string type else definition', () => {
const schema = {
title: 'Test input schema',
type: 'object',
schemaVersion: 1,
properties: {
myField: {
title: 'Field title',
type: 'string',
isSecret: true,
description: 'Some description ...',
editor: 'textfield',
maxLength: true,
},
},
};

expect(() => validateInputSchema(validator, schema)).toThrow(
'Input schema is not valid (Property schema.properties.myField.maxLength is not allowed.)',
);
});

it('should throw error on field structure level for a type ENUM STRING', () => {
const schema = {
title: 'Test input schema',
Expand Down
78 changes: 78 additions & 0 deletions test/input_schema_definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,83 @@ describe('input_schema.json', () => {

turnOffConsoleWarnErrors();
});

describe('special cases for isSecret string type', () => {
const isSchemaValid = (fields: object, isSecret?: boolean) => {
return ajv.validate(inputSchema, {
title: 'Test input schema',
type: 'object',
schemaVersion: 1,
properties: {
myField: {
title: 'Field title',
description: 'My test field',
type: 'string',
isSecret,
...fields,
},
},
});
};

it('should not allow all editors', () => {
['textfield', 'textarea', 'hidden'].forEach((editor) => {
expect(isSchemaValid({ editor }, true)).toBe(true);
});
['javascript', 'python'].forEach((editor) => {
expect(isSchemaValid({ editor }, true)).toBe(false);
});
});

it('should not allow some fields', () => {
['minLength', 'maxLength'].forEach((intField) => {
expect(isSchemaValid({ [intField]: 10 }, true)).toBe(false);
});
['default', 'prefill', 'pattern'].forEach((stringField) => {
expect(isSchemaValid({ [stringField]: 'bla' }, true)).toBe(false);
});
});

it('should work without isSecret with all editors and properties', () => {
expect(ajv.validate(inputSchema, {
title: 'Test input schema',
type: 'object',
schemaVersion: 1,
properties: {
myField: {
title: 'Field title',
description: 'My test field',
type: 'string',
editor: 'textarea',
isSecret: false,
minLength: 2,
maxLength: 100,
default: 'blablablablabla',
prefill: 'blablablablablablablablablablabla',
},
},
})).toBe(true);

expect(ajv.validate(inputSchema, {
title: 'Test input schema',
type: 'object',
schemaVersion: 1,
properties: {
myField: {
title: 'Field title',
description: 'My test field',
type: 'string',
editor: 'textarea',
isSecret: false,
minLength: 2,
maxLength: 100,
default: 'blablablablabla',
prefill: 'blablablablablablablablablablabla',
bla: 'bla', // Validation failed because additional property
},
},
})).toBe(false);
});
});
});
});