Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 89 lines (72 sloc) 2.331 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
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()
Something went wrong with that request. Please try again.