Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

91 lines (72 sloc) 2.331 kb
juggling = require('../index')
Schema = juggling.Schema
AbstractClass = juggling.AbstractClass
Validatable = juggling.Validatable
require('./spec_helper').init module.exports
schema = new Schema 'memory'
User = schema.define 'User',
email: String
name: String
password: String
state: String
age: Number
gender: String
domain: String
pendingPeriod: Number
createdByAdmin: Boolean
createdByScript: Boolean
updatedAt: Date
validAttributes =
name: 'Anatoliy'
email: 'email@example.com'
state: ''
age: 26
gender: 'male'
domain: '1602'
createdByAdmin: false
createdByScript: true
getValidAttributes = ->
name: 'Anatoliy'
email: 'email@example.com'
state: ''
age: 26
gender: 'male'
domain: '1602'
createdByAdmin: false
createdByScript: true
it 'should validate presence', (test) ->
User.validatesPresenceOf 'email', 'name'
user = new User
test.ok not user.isValid(), 'User is not valid'
test.ok user.errors.email, 'Attr email in errors'
test.ok user.errors.name, 'Attr name in errors'
user.name = 'Anatoliy'
test.ok not user.isValid(), 'User is still not valid'
test.ok user.errors.email, 'Attr email still in errors'
test.ok not user.errors.name, 'Attr name valid'
user.email = 'anatoliy@localhost'
test.ok user.isValid(), 'User is valid'
test.ok not user.errors, 'No errors'
test.ok not user.errors.email, 'Attr email valid'
test.ok not user.errors.name, 'Attr name valid'
test.done()
it 'should allow to skip validations', (test) ->
User.validatesPresenceOf 'pendingPeriod', if: 'createdByAdmin'
User.validatesLengthOf 'domain', is: 2, unless: 'createdByScript'
user = new User validAttributes
test.ok user.isValid()
user.createdByAdmin = true
test.ok not user.isValid()
test.ok user.errors.pendingPeriod.length
user.pendingPeriod = 1
test.ok user.isValid()
user.createdByScript = false
test.ok not user.isValid()
test.ok user.errors.domain.length
user.domain = '12'
test.ok user.isValid()
User.validatesLengthOf 'domain', is: 3, unless: -> @domain != 'xyz'
test.ok user.isValid()
user.domain = 'xyz'
test.ok not user.isValid() # is: 3 passed, but is: 2 failed
test.done()
Jump to Line
Something went wrong with that request. Please try again.