Skip to content

Instantly share code, notes, and snippets.

@felpasl
Last active June 23, 2021 20:20
Show Gist options
  • Save felpasl/31f41001db761c18687f46cf1883d502 to your computer and use it in GitHub Desktop.
Save felpasl/31f41001db761c18687f46cf1883d502 to your computer and use it in GitHub Desktop.
validate CPF DataWeave / Mulesoft
%dw 2.0
output application/json
import * from dw::core::Strings
var cpf = leftPad(payload.cpf, 11, "0")
var size = sizeOf(cpf)
var chars = splitBy(cpf, "") [0 to 8]
var verify = splitBy(cpf, "") [9 to 10]
fun sumcpf1(cpf) =
chars[0] * 10 +
chars[1] * 9 +
chars[2] * 8 +
chars[3] * 7 +
chars[4] * 6 +
chars[5] * 5 +
chars[6] * 4 +
chars[7] * 3 +
chars[8] * 2
fun firstdigit(cpf) =
if(11- mod(sumcpf1(cpf), 11)>10)
0
else
11 - mod(sumcpf1(cpf), 11)
fun sumcpf2(cpf) =
chars[0] * 11 +
chars[1] * 10 +
chars[2] * 9 +
chars[3] * 8 +
chars[4] * 7 +
chars[5] * 6 +
chars[6] * 5 +
chars[7] * 4 +
chars[8] * 3 +
verify[0] * 2
fun seconddigit(cpf) =
if(11- mod(sumcpf2(cpf), 11)>10)
0
else
11 - mod(sumcpf2(cpf), 11)
fun isCpf(cpf) =
(firstdigit(cpf) ++ seconddigit(cpf)) == joinBy(verify,"")
---
{
verify: joinBy(verify,""),
correct: firstdigit(payload.cpf) ++ seconddigit(payload.cpf),
isvalid: isCpf(payload.cpf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment