mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 14:43:08 +03:00
Fixed validating numbers as booleans in schema validator
refs https://github.com/TryGhost/Toolbox/issues/441 - I'm currently working on cleaning up our uses of `bool` and `boolean` in favor of `boolean`, and I've noticed we only handle converting numbers into booleans when the type is `bool`, so validation would otherwise fail - given these can be used interchangeably, we should also support converting the numbers into booleans when the type is `boolean` - this is going to get cleaned up again when I remove `bool` but this fixes the validation bug for now
This commit is contained in:
parent
f5774fad0c
commit
24670aa555
@ -63,7 +63,7 @@ function validateSchema(tableName, model, options) {
|
||||
|
||||
// validate boolean columns
|
||||
if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'type')
|
||||
&& schema[tableName][columnKey].type === 'bool') {
|
||||
&& (schema[tableName][columnKey].type === 'bool' || schema[tableName][columnKey].type === 'boolean')) {
|
||||
if (!(validator.isBoolean(strVal) || validator.isEmpty(strVal))) {
|
||||
message = tpl(messages.valueMustBeBoolean, {
|
||||
tableName: tableName,
|
||||
|
@ -64,7 +64,7 @@ describe('Validate Schema', function () {
|
||||
);
|
||||
});
|
||||
|
||||
it('transforms 0 and 1', function () {
|
||||
it('transforms 0 and 1 (bool)', function () {
|
||||
const post = models.Post.forge(testUtils.DataGenerator.forKnex.createPost({slug: 'test', featured: 0}));
|
||||
post.get('featured').should.eql(0);
|
||||
|
||||
@ -74,6 +74,14 @@ describe('Validate Schema', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('transforms 0 and 1 (boolean)', async function () {
|
||||
const user = models.User.forge(testUtils.DataGenerator.forKnex.createUser({email: 'test@example.com', comment_notifications: 0}));
|
||||
user.get('comment_notifications').should.eql(0);
|
||||
|
||||
await validateSchema('users', user, {method: 'insert'});
|
||||
user.get('comment_notifications').should.eql(false);
|
||||
});
|
||||
|
||||
it('keeps true or false', function () {
|
||||
const post = models.Post.forge(testUtils.DataGenerator.forKnex.createPost({slug: 'test', featured: true}));
|
||||
post.get('featured').should.eql(true);
|
||||
|
Loading…
Reference in New Issue
Block a user