Skip to content

Commit

Permalink
task model: harmonizing validation
Browse files Browse the repository at this point in the history
by keeping the singular form in validation function names
when the attribute is singular
  • Loading branch information
maxlath committed Jun 25, 2018
1 parent 978c423 commit dee15e9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
14 changes: 5 additions & 9 deletions server/models/task.coffee
Expand Up @@ -11,9 +11,9 @@ module.exports =
{ type, suspectUri, suggestionUri, state, lexicalScore, relationScore, hasEncyclopediaOccurence } = newTask
state or= 'requested'

validations.pass 'types', type
validations.pass 'states', state
validations.pass 'suspect', suspectUri
validations.pass 'type', type
validations.pass 'state', state
validations.pass 'suspectUri', suspectUri
validations.pass 'lexicalScore', lexicalScore
validations.pass 'relationScore', relationScore
validations.pass 'hasEncyclopediaOccurence', hasEncyclopediaOccurence
Expand All @@ -31,12 +31,8 @@ module.exports =
update: (task, attribute, value)->
_.types arguments, [ 'object', 'string', 'string|number' ]

validations.pass 'attributes', attribute

# Todo : find a way to check update values according to valid attribute,
# without the current code smell (conditonal if)
if attribute is 'state'
validations.pass 'states', value
validations.pass 'attribute', attribute
validations.pass attribute, value

now = Date.now()

Expand Down
8 changes: 4 additions & 4 deletions server/models/validations/task.coffee
Expand Up @@ -9,10 +9,10 @@ module.exports =
pass: pass
# in attributes/task.coffee, attributes keys should match
# db keys to verify if attribute is updatable
attributes: (attribute)-> attribute in _.keys attributes
types: (taskType)-> taskType in attributes.type
states: (taskState)-> taskState in attributes.state
suspect: entityUri
attribute: (attribute)-> attribute in _.keys attributes
type: (taskType)-> taskType in attributes.type
state: (taskState)-> taskState in attributes.state
suspectUri: entityUri
lexicalScore: _.isNumber
relationScore: _.isNumber
hasEncyclopediaOccurence: _.isBoolean
3 changes: 1 addition & 2 deletions test/models/026-task.coffee
Expand Up @@ -2,7 +2,6 @@ CONFIG = require 'config'
__ = CONFIG.universalPath
_ = __.require 'builders', 'utils'
should = require 'should'

Task = __.require 'models', 'task'

validDoc = ->
Expand Down Expand Up @@ -84,7 +83,7 @@ describe 'task model', ->
it 'should throw if invalid attribute to update', (done)->
taskDoc = -> Task.update validDoc(), 'blob', 'dismissed'
try taskDoc()
catch err then err.message.should.startWith 'invalid attributes'
catch err then err.message.should.startWith 'invalid attribute'
taskDoc.should.throw()
done()

Expand Down

0 comments on commit dee15e9

Please sign in to comment.