Added test to validate schema structure

refs https://github.com/TryGhost/Toolbox/issues/441

- this is only v1 of the test I would like but it validates the keys on
  a column definition are part of an allowlist
- this has already uncovered a bug with `maxLength` (vs `maxlength`)
This commit is contained in:
Daniel Lockyer 2022-10-12 19:19:33 +07:00
parent 178df69ae2
commit 845f8d965e
No known key found for this signature in database

View File

@ -3,7 +3,77 @@ const _ = require('lodash');
const schema = require('../../../../../core/server/data/schema/schema');
const VALID_KEYS = {
bigInteger: [
'nullable'
],
bool: [
'nullable',
'defaultTo'
],
boolean: [
'nullable',
'defaultTo'
],
dateTime: [
'nullable',
'index'
],
integer: [
'nullable',
'unsigned',
'defaultTo',
'index'
],
string: [
'maxlength',
'maxLength', // this is the result of a bug and should NOT be used, will be deleted soon
'nullable',
'primary',
'unique',
'validations',
'defaultTo',
'references',
'cascadeDelete',
'setNullDelete',
'index'
],
text: [
'fieldtype',
'maxlength',
'nullable',
'validations'
]
};
describe('schema validations', function () {
it('matches the required format', function () {
// The top-level export should be an object of table names to definitions
should(schema).be.Object();
// Each table should be an object, and each column should be an object
_.each(schema, function (table, tableName) {
should(table).be.Object();
_.each(table, function (column, columnName) {
if (['@@INDEXES@@', '@@UNIQUE_CONSTRAINTS@@'].includes(columnName)) {
return;
}
// Ensure the column is an object
should(column).be.Object();
// Ensure the `type` key exists on a column
should.exist(column.type, `${tableName}.${columnName}.type should exist`);
// Ensure the column type is one of the ones we allow
should(column.type).be.equalOneOf(Object.keys(VALID_KEYS));
should(_.difference(Object.keys(column), [...VALID_KEYS[column.type], 'type'])).be.Array().with.length(0);
});
});
});
it('has correct isIn validation structure', async function () {
const tablesOnlyValidation = _.cloneDeep(schema);