Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow null to be passed as instance value #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ function JsonField(db, modelName, fieldName, options) {
if (typeof instance.dataValues[fieldName] !== 'string' && instance.dataValues[fieldName]) {
instance.setDataValue(fieldName, JSON.stringify(instance.getDataValue(fieldName)));
return self;
} else if (instance.dataValues[fieldName] === 'null' || !instance.dataValues[fieldName]) {
} else if (instance.dataValues[fieldName] === 'null') {
instance.setDataValue(fieldName, null);
} else if (typeof instance.dataValues[fieldName] === 'undefined') {
instance.setDataValue(fieldName, undefined);
}
}
Expand Down Expand Up @@ -37,6 +39,10 @@ function JsonField(db, modelName, fieldName, options) {
model.defaultValue = JSON.stringify(options.defaultValue);
}

if (typeof options.allowNull === 'boolean') {
model.allowNull = options.allowNull;
}

return model;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sequelize-json",
"version": "2.1.2",
"version": "2.1.3",
"description": "Automatic serialization/parsing of JSON in your models",
"main": "index.js",
"directories": {
Expand Down
45 changes: 45 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ describe('Test the various uses of the JSON field', function() {
})
.catch(done);
});

it('Null should be passed as a possible value', function(done) {
User.create({
jsonField: null
})
.then(function(user) {
expect(user.jsonField).to.be.null;
done();
})
.catch(done);
});
});

describe('Test default values', function() {
Expand Down Expand Up @@ -179,6 +190,40 @@ describe('Test default values', function() {
})
.catch(done);
});
});

describe('Test allowNull', function() {

beforeEach(function(done) {
db = new Sequelize('database', 'username', 'password', {
dialect: 'sqlite',
logging: false
});

User = db.define('User', {
username: Sequelize.STRING,
jsonField: new JsonField(db, 'User', 'jsonField', { allowNull: false })
});

db
.sync({ force: true })
.then(function() {
done();
});
});

it('Should reject null values with a SequelizeUniqueConstraintError', function(done) {
User.create({
username: 'Scott',
jsonField: null
})
.then(function() {
fail('null Value should have been rejected')
done()
})
.catch(function(error) {
expect(error).to.be.an.instanceof(Sequelize.UniqueConstraintError)
done()
});
});
});