Sha256: 2abea9afc3bd57145f9b202fb42d2e7ba77f1489e342a829e49f999b4149c654

Contents?: true

Size: 1.27 KB

Versions: 26

Compression:

Stored size: 1.27 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

validations = require '../validatable'
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 [1..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 [1..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 validations.validatable, cpfable
composed.definition_key = 'validates_cpf_format'
validations.manager.validators.cpf = composed

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
ende-0.3.13 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.12 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.11 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.10 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.9 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.8 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.7 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.6 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.5 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.4 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.3 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.2 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.1 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.3.0 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.2.21 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.2.20 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.2.19 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.2.18 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.2.17 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee
ende-0.2.16 vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee