From 42e586658b4bc39542a5311842f0040f7d29cb2b Mon Sep 17 00:00:00 2001 From: Murilo Ijanc Date: Mon, 27 Jul 2026 20:31:13 -0300 Subject: Name the municipality behind a CEP that does not exist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The API now hangs an optional faixa block off the 404 of GET /{cep}, and this exposes it as CepNotFoundException.Faixa. A CEP that fails is rarely random: it is usually someone who knows the city and guessed the number, most often the -000 that 957 municipalities never received. Knowing where the guess landed turns a dead-end "CEP not found" into a question worth asking the user. It stays an error. The block names the municipality the number would belong to, never an address, and the lookup still throws. CepIni and CepFim are the smallest and largest CEP that actually exist there, not the range the Correios publish — the published start for Areal is 25845000, exactly the CEP that fails — and the interval between them is sparse, which is what Ceps is for. Parsing is deliberately toothless: absent, misshapen or nameless, the block becomes null and the caller gets the same clean not-found it got before the field existed. A courtesy on top of a 404 must never be able to turn it into a parse failure. --- src/Cepis/Faixa.cs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/Cepis/Faixa.cs (limited to 'src/Cepis/Faixa.cs') diff --git a/src/Cepis/Faixa.cs b/src/Cepis/Faixa.cs new file mode 100644 index 0000000..e2477b6 --- /dev/null +++ b/src/Cepis/Faixa.cs @@ -0,0 +1,99 @@ +using System.Text.Json.Serialization; + +namespace Cepis; + +/// +/// The municipality a CEP that does not exist would have belonged to. +/// +/// +/// +/// This is context for an error, never an address. It rides on the 404 +/// of , hanging +/// off . The CEP that was asked for +/// still does not exist, and nothing here says otherwise: filling a form from +/// these fields would be inventing an address. +/// +/// +/// It is worth having because a CEP that fails is rarely random — it is usually +/// someone who knows the city and guessed the number. The classic guess is the +/// -000, which 957 municipalities never received. Knowing where the +/// guess landed is what turns "CEP not found" into "did you mean a street in +/// Areal/RJ?". +/// +/// +/// and are the smallest and the +/// largest CEP that actually exist in that municipality, not the +/// bounds the Correios publish. For Areal the DNE's own range starts at +/// 25845000 — exactly the CEP that just failed — while +/// is 25845001. +/// +/// +/// The range is sparse, and the pair is not a list of valid CEPs. Areal +/// has 118 of them () spread across the ~4,900 numbers +/// between its two ends, so a number picked in between overwhelmingly does not +/// exist either. Never iterate the interval, and never show it as a set of +/// usable CEPs. +/// +/// +/// The property names are the API's own, in Portuguese, with only the casing +/// adapted — the same rule the rest of the data follows. On the wire they are +/// localidade, uf, estado, regiao, ibge, +/// cep_ini, cep_fim and ceps. +/// +/// +public sealed record Faixa +{ + /// The municipality. Wire name localidade. + [JsonPropertyName("localidade")] + public string Localidade { get; init; } = ""; + + /// The two-letter state code. Wire name uf. + [JsonPropertyName("uf")] + public string UF { get; init; } = ""; + + /// The state's name. Wire name estado. + [JsonPropertyName("estado")] + public string Estado { get; init; } = ""; + + /// The region of the country. Wire name regiao. + [JsonPropertyName("regiao")] + public string Regiao { get; init; } = ""; + + /// The IBGE municipality code. Wire name ibge. + [JsonPropertyName("ibge")] + public string Ibge { get; init; } = ""; + + /// + /// The smallest CEP that exists in that municipality, unmasked. Wire name + /// cep_ini. + /// + /// + /// The smallest one in the database, not the start of the range the + /// Correios publish — the published start may itself be a CEP that was never + /// issued, which is often precisely the one the caller just tried. + /// + [JsonPropertyName("cep_ini")] + public string CepIni { get; init; } = ""; + + /// + /// The largest CEP that exists in that municipality, unmasked. Wire name + /// cep_fim. + /// + /// + /// Together with it bounds the CEPs of the + /// municipality; it does not enumerate them. See . + /// + [JsonPropertyName("cep_fim")] + public string CepFim { get; init; } = ""; + + /// + /// How many CEPs the municipality actually has. Wire name ceps. + /// + /// + /// This field exists to keep and + /// from being read as a contiguous interval. Areal's 118 CEPs sit inside a + /// span of about 4,900 numbers: the range is mostly empty. + /// + [JsonPropertyName("ceps")] + public int Ceps { get; init; } +} -- cgit v1.3