Skip to content

Commit

Permalink
feat(validation): make length validation more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
filipedeschamps committed Jul 17, 2016
1 parent cfcdc28 commit 237dff4
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/cep-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import { get as _get } from 'lodash'
export default function (cepRawValue) {
return new Promise((resolve, reject) => {
Promise.resolve(cepRawValue)
.then(validateInput)
.then(validateInputType)
.then(removeSpecialCharacters)
.then(validateInputLength)
.then(leftPadWithZeros)
.then(fetchCorreiosService)
.then(parseXML)
.then(extractValuesFromParsedXML)
.then(finish)
.catch(handleError)

function validateInput (cepRawValue) {
function validateInputType (cepRawValue) {
var cepTypeOf = typeof cepRawValue

if (cepTypeOf === 'number' || cepTypeOf === 'string') {
Expand All @@ -42,6 +43,15 @@ export default function (cepRawValue) {
return cepWithLeftPad
}

function validateInputLength (cepWithLeftPad) {

if (cepWithLeftPad.length <= 8) {
return cepWithLeftPad
}

throw new TypeError('Cep deve conter exatamente 8 caracteres')
}

function fetchCorreiosService (cepWithLeftPad) {
return new Promise(function (resolve, reject) {
var options = {
Expand Down

0 comments on commit 237dff4

Please sign in to comment.