aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pod.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-12-28 15:49:23 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-12-28 15:49:23 +0100
commit67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677 (patch)
treebae6a9b0c3133c9cc38a2972222b5991f0cf614e /server/models/pod.js
parent552cc9d646e78edae8b0fe61564d4e49db0b6206 (diff)
downloadPeerTube-67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677.tar.gz
PeerTube-67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677.tar.zst
PeerTube-67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677.zip
Server: add database field validations
Diffstat (limited to 'server/models/pod.js')
-rw-r--r--server/models/pod.js34
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 @@
3const map = require('lodash/map') 3const map = require('lodash/map')
4 4
5const constants = require('../initializers/constants') 5const constants = require('../initializers/constants')
6const 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
53function toFormatedJSON () { 61function 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
96function list (callback) { 106function list (callback) {