feat(parser): first‑class support for JSON string.format #12

Merged
PuChenTW merged 2 commits from main into main 2025-05-10 23:09:14 +00:00
PuChenTW commented 2025-05-06 15:03:07 +00:00 (Migrated from github.com)
<html><head></head>

feat(parser): first‑class support for JSON string.format

Problem
StringTypeParser always returned bare str, so a schema like

{ "type": "string", "format": "email" }

generated weakly‑typed models and skipped format validation.

Solution

  • Added format_type_mapping → maps common format values to Pydantic types.

    format Pydantic type
    email pydantic.EmailStr
    uri pydantic.HttpUrl
    ipv4/ipv6 pydantic.IPvAnyAddress
    hostname str + regex fallback
    date datetime.date
    time datetime.time
    date-time datetime.datetime
  • Added format_pattern_mapping for formats without built‑ins (hostname).

  • Unknown format now raises ValueError early.

Tests

  • Parametrized success cases for each supported format.

  • Failure case for unknown format. All new tests pass.


Big thanks to @HideyoshiNakazone for creating and maintaining Jambo—this PR is a small way to give back.
<html><head></head><body><h3>feat(parser): first‑class support for JSON&nbsp;<code inline="">string.format</code></h3> <p><strong>Problem</strong><br> <code inline="">StringTypeParser</code> always returned bare <code inline="">str</code>, so a schema like</p> <pre><code class="language-json">{ "type": "string", "format": "email" } </code></pre> <p>generated weakly‑typed models and skipped format validation.</p> <p><strong>Solution</strong></p> <ul> <li> <p>Added <code inline="">format_type_mapping</code> → maps common <strong><code inline="">format</code></strong> values to Pydantic types.</p> format | Pydantic type -- | -- email | pydantic.EmailStr uri | pydantic.HttpUrl ipv4/ipv6 | pydantic.IPvAnyAddress hostname | str + regex fallback date | datetime.date time | datetime.time date-time | datetime.datetime </li> <li> <p>Added <code inline="">format_pattern_mapping</code> for formats without built‑ins (hostname).</p> </li> <li> <p>Unknown <code inline="">format</code> now raises <code inline="">ValueError</code> early.</p> </li> </ul> <p><strong>Tests</strong></p> <ul> <li> <p>Parametrized success cases for each supported format.</p> </li> <li> <p>Failure case for unknown format. All new tests pass.</p> </li> </ul> <hr> Big thanks to @HideyoshiNakazone for creating and maintaining Jambo—this PR is a small way to give back.
HideyoshiNakazone commented 2025-05-10 20:41:59 +00:00 (Migrated from github.com)

Hey @PuChenTW, thanks a lot for the contribution! I'll take a look at your MR. :)

Hey @PuChenTW, thanks a lot for the contribution! I'll take a look at your MR. :)
HideyoshiNakazone commented 2025-05-10 23:14:42 +00:00 (Migrated from github.com)

PR merged! Thanks for the contribution @PuChenTW !
I'm currently implementing the Ref property of the JSON Schema Specification. Once I'm done, I'll make a new feature release with your contribution.

Best regards,
Vitor Hideyoshi

PR merged! Thanks for the contribution @PuChenTW ! I'm currently implementing the Ref property of the [JSON Schema Specification](https://json-schema.org/specification). Once I'm done, I'll make a new feature release with your contribution. Best regards, Vitor Hideyoshi
Sign in to join this conversation.