Skip to content

Instantly share code, notes, and snippets.

@ThadeuLuz
Last active March 14, 2025 00:16
Show Gist options
  • Save ThadeuLuz/797b60972f74f3080b32642eb36481a5 to your computer and use it in GitHub Desktop.
Save ThadeuLuz/797b60972f74f3080b32642eb36481a5 to your computer and use it in GitHub Desktop.
Objeto JSON com Estados Brasileiros por DDD e lista de DDDs por Estados.
{
"estadoPorDdd": {
"11": "SP",
"12": "SP",
"13": "SP",
"14": "SP",
"15": "SP",
"16": "SP",
"17": "SP",
"18": "SP",
"19": "SP",
"21": "RJ",
"22": "RJ",
"24": "RJ",
"27": "ES",
"28": "ES",
"31": "MG",
"32": "MG",
"33": "MG",
"34": "MG",
"35": "MG",
"37": "MG",
"38": "MG",
"41": "PR",
"42": "PR",
"43": "PR",
"44": "PR",
"45": "PR",
"46": "PR",
"47": "SC",
"48": "SC",
"49": "SC",
"51": "RS",
"53": "RS",
"54": "RS",
"55": "RS",
"61": "DF",
"62": "GO",
"63": "TO",
"64": "GO",
"65": "MT",
"66": "MT",
"67": "MS",
"68": "AC",
"69": "RO",
"71": "BA",
"73": "BA",
"74": "BA",
"75": "BA",
"77": "BA",
"79": "SE",
"81": "PE",
"82": "AL",
"83": "PB",
"84": "RN",
"85": "CE",
"86": "PI",
"87": "PE",
"88": "CE",
"89": "PI",
"91": "PA",
"92": "AM",
"93": "PA",
"94": "PA",
"95": "RR",
"96": "AP",
"97": "AM",
"98": "MA",
"99": "MA"
},
"dddsPorEstado": {
"AC": ["68"],
"AL": ["82"],
"AM": ["92", "97"],
"AP": ["96"],
"BA": ["71", "73", "74", "75", "77"],
"CE": ["85", "88"],
"DF": ["61"],
"ES": ["27", "28"],
"GO": ["62", "64"],
"MA": ["98", "99"],
"MG": ["31", "32", "33", "34", "35", "37", "38"],
"MS": ["67"],
"MT": ["65", "66"],
"PA": ["91", "93", "94"],
"PB": ["83"],
"PE": ["81", "87"],
"PI": ["86", "89"],
"PR": ["41", "42", "43", "44", "45", "46"],
"RJ": ["21", "22", "24"],
"RN": ["84"],
"RO": ["69"],
"RR": ["95"],
"RS": ["51", "53", "54", "55"],
"SC": ["47", "48", "49"],
"SE": ["79"],
"SP": ["11", "12", "13", "14", "15", "16", "17", "18", "19"],
"TO": ["63"]
}
}
@EdilsonVieira
Copy link

Uma por cidade seria ótimo! Alguém tem?

@Apollo11zz
Copy link

Apollo11zz commented Mar 14, 2025

Exemplo de validação customizada para DTO para aplicações .net

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace API.EXEMPLO.Utils.CustomDataAnnotations
{
    public class TelefoneAttribute : ValidationAttribute
    {
        #region Mensagens de erro personalizadas
        private const string FormatoInvalidoMessage = "O formato do telefone é inválido.";
        private const string TamanhoInvalidoMessage = "O telefone deve ter 10 ou 11 dígitos.";
        private const string DDDInvalidoMessage = "O DDD do telefone é inválido.";
        #endregion

        #region Lista de DDDs válidos
        private static readonly HashSet<string> DDDsValidos = new HashSet<string>
        {
            "11", "12", "13", "14", "15", "16", "17", "18", "19", // SP
            "21", "22", "24", // RJ
            "27", "28", // ES
            "31", "32", "33", "34", "35", "37", "38", // MG
            "41", "42", "43", "44", "45", "46", // PR
            "47", "48", "49", // SC
            "51", "53", "54", "55", // RS
            "61", // DF
            "62", "64", // GO
            "63", // TO
            "65", "66", // MT
            "67", // MS
            "68", // AC
            "69", // RO
            "71", "73", "74", "75", "77", // BA
            "79", // SE
            "81", "87", // PE
            "82", // AL
            "83", // PB
            "84", // RN
            "85", "88", // CE
            "86", "89", // PI
            "91", "93", "94", // PA
            "92", "97", // AM
            "95", // RR
            "96", // AP
            "98", "99" // MA
        };
        #endregion

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
                return ValidationResult.Success;

            string telefone = value.ToString();

            // Remove todos os caracteres não numéricos
            telefone = Regex.Replace(telefone, @"[^\d]", "");

            // Valida o comprimento do telefone
            if (telefone.Length != 10 && telefone.Length != 11)
                return new ValidationResult(TamanhoInvalidoMessage);

            // Valida se contém apenas dígitos
            if (!Regex.IsMatch(telefone, @"^\d+$"))
                return new ValidationResult(FormatoInvalidoMessage);

            if (!ValidarDDD(telefone))
                return new ValidationResult(DDDInvalidoMessage);

            return ValidationResult.Success;
        }

        private bool ValidarDDD(string telefone)
        {
            string ddd = telefone.Substring(0, 2);

            return DDDsValidos.Contains(ddd);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment