Skip to content

Add rfl::CodePoints validator for UTF-8 string length - #7

Closed
clockblocker wants to merge 1 commit into
mainfrom
code-points-validator
Closed

clockblocker wants to merge 1 commit into
mainfrom
code-points-validator

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Problem

rfl::Size<V> for string validates the number of bytes.
The JSON schema however interprited that as Unicode code points

So for non-ASCII strings the two disagree.


AI-authored:

Take "héllo": it is 6 bytes and 5 code points, so rfl::Size<rfl::Maximum<5>> rejects it while the schema it generates accepts it.

What's added

  • rfl::CodePoints<V> (include/rfl/CodePoints.hpp, exported from rfl.hpp). It counts UTF-8 code points, meaning every byte that is not a continuation byte (10xxxxxx), and runs V on that count.
  • to_schema passes through to rfl::Size<V>, so the schema output is the same (minLength/maxLength) and it works with AllOf/AnyOf without touching to_schema.cpp.
  • A short section in docs/content/size_validation.md.

Why a separate type instead of a flag on Size

  • Backward compatible: rfl::Size behaves exactly as before.
  • A "count code points" flag would mean nothing for vectors, sets and other containers, which Size also handles.

Caveats

  • It counts code points, not grapheme clusters. An emoji with a skin-tone modifier counts as 2.
  • It does not check that the input is well-formed UTF-8.

Tests

tests/json/test_code_points.cpp (picked up by the existing GLOB_RECURSE in tests/json/CMakeLists.txt) covers:

  • ASCII strings within and over the limit
  • multi-byte strings (héllo, 日本語, an emoji) that pass CodePoints but fail the matching byte-based Size limit
  • Minimum
  • schema output (minLength/maxLength, the AllOf form, and that it matches Size)

All 286 tests in reflect-cpp-json-tests pass locally (macOS, Apple clang, Release, bundled deps).

🤖 Generated with Claude Code

rfl::Size validates .size(), which is the byte count for std::string,
but its JSON schema emits minLength/maxLength, which JSON Schema
defines in Unicode code points. For non-ASCII strings the validator
and the schema disagree.

rfl::CodePoints<V> counts UTF-8 code points (bytes that are not
continuation bytes) and applies V to that count. Its schema output is
identical to rfl::Size, so it composes with AllOf/AnyOf the same way.
rfl::Size is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@clockblocker

Copy link
Copy Markdown
Owner Author

Merged upstream in munich-data-engineering#774.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant