diff options
Diffstat (limited to 'server/models/pod.js')
-rw-r--r-- | server/models/pod.js | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/server/models/pod.js b/server/models/pod.js index fff6970a7..84f78f200 100644 --- a/server/models/pod.js +++ b/server/models/pod.js | |||
@@ -3,6 +3,7 @@ | |||
3 | const map = require('lodash/map') | 3 | const map = require('lodash/map') |
4 | 4 | ||
5 | const constants = require('../initializers/constants') | 5 | const constants = require('../initializers/constants') |
6 | const customPodsValidators = require('../helpers/custom-validators').pods | ||
6 | 7 | ||
7 | // --------------------------------------------------------------------------- | 8 | // --------------------------------------------------------------------------- |
8 | 9 | ||
@@ -10,14 +11,27 @@ module.exports = function (sequelize, DataTypes) { | |||
10 | const Pod = sequelize.define('Pod', | 11 | const Pod = sequelize.define('Pod', |
11 | { | 12 | { |
12 | host: { | 13 | host: { |
13 | type: DataTypes.STRING | 14 | type: DataTypes.STRING, |
15 | allowNull: false, | ||
16 | validate: { | ||
17 | isHost: function (value) { | ||
18 | const res = customPodsValidators.isHostValid(value) | ||
19 | if (res === false) throw new Error('Host not valid.') | ||
20 | } | ||
21 | } | ||
14 | }, | 22 | }, |
15 | publicKey: { | 23 | publicKey: { |
16 | type: DataTypes.STRING(5000) | 24 | type: DataTypes.STRING(5000), |
25 | allowNull: false | ||
17 | }, | 26 | }, |
18 | score: { | 27 | score: { |
19 | type: DataTypes.INTEGER, | 28 | type: DataTypes.INTEGER, |
20 | defaultValue: constants.FRIEND_SCORE.BASE | 29 | defaultValue: constants.FRIEND_SCORE.BASE, |
30 | allowNull: false, | ||
31 | validate: { | ||
32 | isInt: true, | ||
33 | max: constants.FRIEND_SCORE.MAX | ||
34 | } | ||
21 | } | 35 | } |
22 | }, | 36 | }, |
23 | { | 37 | { |
@@ -42,12 +56,6 @@ module.exports = function (sequelize, DataTypes) { | |||
42 | return Pod | 56 | return Pod |
43 | } | 57 | } |
44 | 58 | ||
45 | // TODO: max score -> constants.FRIENDS_SCORE.MAX | ||
46 | // TODO: validation | ||
47 | // PodSchema.path('host').validate(validator.isURL) | ||
48 | // PodSchema.path('publicKey').required(true) | ||
49 | // PodSchema.path('score').validate(function (value) { return !isNaN(value) }) | ||
50 | |||
51 | // ------------------------------ METHODS ------------------------------ | 59 | // ------------------------------ METHODS ------------------------------ |
52 | 60 | ||
53 | function toFormatedJSON () { | 61 | function toFormatedJSON () { |
@@ -82,15 +90,17 @@ function incrementScores (ids, value, callback) { | |||
82 | score: this.sequelize.literal('score +' + value) | 90 | score: this.sequelize.literal('score +' + value) |
83 | } | 91 | } |
84 | 92 | ||
85 | const query = { | 93 | const options = { |
86 | where: { | 94 | where: { |
87 | id: { | 95 | id: { |
88 | $in: ids | 96 | $in: ids |
89 | } | 97 | } |
90 | } | 98 | }, |
99 | // In this case score is a literal and not an integer so we do not validate it | ||
100 | validate: false | ||
91 | } | 101 | } |
92 | 102 | ||
93 | return this.update(update, query).asCallback(callback) | 103 | return this.update(update, options).asCallback(callback) |
94 | } | 104 | } |
95 | 105 | ||
96 | function list (callback) { | 106 | function list (callback) { |