35 lines
1.2 KiB
CoffeeScript
35 lines
1.2 KiB
CoffeeScript
import Ember from 'ember'
|
|
import buildMessage from 'ember-changeset-validations/utils/validation-errors'
|
|
|
|
_isPresent = (value) ->
|
|
if (value instanceof Ember.ObjectProxy || value instanceof Ember.ArrayProxy)
|
|
return Ember._isPresent(Ember.get(value, 'content'))
|
|
Ember.isPresent(value)
|
|
|
|
_testPresence = (key, value, presence, context = {}) ->
|
|
if presence
|
|
return Ember._isPresent(value) || buildMessage(key, 'present', value, context)
|
|
else
|
|
return Ember.isBlank(value) || buildMessage(key, 'blank', value, context)
|
|
|
|
export default (opts)->
|
|
(key, value, oldValue, changes, content) =>
|
|
if typeof opts is "function"
|
|
presence = true
|
|
condition = opts
|
|
else
|
|
presence = if "presence" in opts then opts.presence else true
|
|
condition = opts.condition
|
|
|
|
# since contentn is the model we have to emulate the changes for now
|
|
condition_object = Ember.assign(content.toJSON(), changes)
|
|
if condition.call(condition_object) # perform validation when condition returns true
|
|
if presence
|
|
_isPresent(value) || buildMessage(key, 'present', value, opts)
|
|
else
|
|
isBlank(value) || buildMessage(key, 'blank', value, opts)
|
|
else
|
|
true
|
|
#return _testPresence(key, value, opts)
|
|
|