Sha256: 797717a636c808862a57ca70042e03d24c6f82ca67c1d2383f00cd5c6af31acc

Contents?: true

Size: 1.21 KB

Versions: 30

Compression:

Stored size: 1.21 KB

Contents

# TODO Add base settings to validation
# model = window.model

# TODO implement method
# model[resource].validators_on 'field' # Get all validators related to this field

stampit     = require '../../../vendor/stampit'

cpfable = stampit
  validate_format: (value) ->
    value = value.replace /[\.\-]/g, ""

    # Wrong cpf size
    return false if value.length < 11

    # Dummy but valid values (000.000.000-00, 111.111.111-11, ...)
    return false if value.match /^(0+|1+|2+|3+|4+|5+|6+|7+|8+|9+)$/

    # Mod 11 validation
    c  = value.substr 0, 9
    dv = value.substr 9, 2
    d1 = 0
    v  = false
    i  = 0

    for i in [0..9]
      d1 += c.charAt(i) * (10 - i)

    return false if d1 == 0

    d1 = 11 - (d1 % 11)
    d1 = 0 if (d1 > 9 )

    return false if +dv.charAt(0) != d1

    d1 *= 2
    for i in [0..9]
      d1 += c.charAt(i) * (11 - i)


    d1 = 11 - (d1 % 11)
    d1 = 0 if (d1 > 9 )

    return false if +dv.charAt(1) != d1

    true


  validate_each: (record, attribute, value) ->
    record.errors.add attribute, 'cpf', @options if value and not @validate_format value




composed = stampit.compose require('./validatorable'), cpfable
composed.definition_key = 'validates_cpf_format'
module.exports = composed

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
ende-0.5.22 components/indefinido/indemma/master/src/lib/record/validations/cpf.coffee
ende-0.5.21 components/indefinido/indemma/master/src/lib/record/validations/cpf.coffee
ende-0.4.25 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.24 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.23 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.5.1 components/indefinido/indemma/master/src/lib/record/validations/cpf.coffee
ende-0.4.22 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.21 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.5.0 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.20 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.19 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.18 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.17 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.16 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.15 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.14 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.13 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.12 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.11 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.4.10 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee