-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(schema): throw an error when enum values are corrupted in the sch…
…ema (#877)
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/agent/src/utils/forest-schema/column-schema-validation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ColumnSchema, ValidationError } from '@forestadmin/datasource-toolkit'; | ||
|
||
export default class ColumnSchemaValidation { | ||
static validate(column: ColumnSchema, name: string): void { | ||
if (!column.enumValues) return; | ||
|
||
if (!Array.isArray(column.enumValues) || !column.enumValues.every(v => typeof v === 'string')) { | ||
throw new ValidationError( | ||
`The enumValues of column '${name}' must be an array of string instead of ${JSON.stringify( | ||
column.enumValues, | ||
)}`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/agent/test/utils/forest-schema/column-schema-validation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import ColumnSchemaValidation from '../../../src/utils/forest-schema/column-schema-validation'; | ||
import * as factories from '../../__factories__'; | ||
|
||
describe('ColumnSchemaValidation', () => { | ||
describe('enumValues', () => { | ||
describe('when enumValues is empty', () => { | ||
it('should not throw', () => { | ||
const column = factories.columnSchema.build({ | ||
columnType: 'Enum', | ||
enumValues: [], | ||
}); | ||
expect(() => ColumnSchemaValidation.validate(column, 'isbn')).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('when enumValues is not an array', () => { | ||
it('should throw', () => { | ||
const column = factories.columnSchema.build({ | ||
columnType: 'Enum', | ||
enumValues: 'not an array' as unknown as string[], | ||
}); | ||
expect(() => ColumnSchemaValidation.validate(column, 'isbn')).toThrow( | ||
'The enumValues of column \'isbn\' must be an array of string instead of "not an array"', | ||
); | ||
}); | ||
}); | ||
|
||
describe('when enumValues is not an array of strings', () => { | ||
it('should throw', () => { | ||
const column = factories.columnSchema.build({ | ||
columnType: 'Enum', | ||
enumValues: [1, 2, 3] as unknown as string[], | ||
}); | ||
expect(() => ColumnSchemaValidation.validate(column, 'isbn')).toThrow( | ||
"The enumValues of column 'isbn' must be an array of string instead of [1,2,3]", | ||
); | ||
}); | ||
}); | ||
|
||
describe('when enumValues is an array of strings', () => { | ||
it('should not throw', () => { | ||
const column = factories.columnSchema.build({ | ||
columnType: 'Enum', | ||
enumValues: ['a', 'b', 'c'], | ||
}); | ||
expect(() => ColumnSchemaValidation.validate(column, 'isbn')).not.toThrow(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters