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.

clockblocker added a commit that referenced this pull request Nov 28, 2026
- P0-1: Eliminate flattened_field_offsets<T>() and unconstructed dummy[sizeof(T)]
  entirely; extract live-object field pointers directly in write(),
  read_with_default(), and to_schema() (skipping extraction when _defaults ==
  nullptr), add std::launder on destroy_field_erased, and reject aggregates
  with reference members in both C++20 (NonRefAggProbe) and C++26
  (std::meta::is_reference).
- P0-2: Replace all unevaluated-context generic lambdas in TableStructParser
  and DirectFieldNames with named visitor structs, and replace all 5 consteval
  make_table lambdas with direct static consteval pack-expansion helpers.
- P1-1: Add use_table_struct_parser_ = true to boost_serialization/Parser.hpp
  (10/10 built-in NamedTuple specializations opted in).
- P1-2: Gate Parser_named_tuple.hpp's use_table_struct_parser_ on
  R::UnsizedObjectReader so external ViewType-inspecting custom readers stay
  on NamedTupleParser without breaking; also catch exceptions inside
  NamedTupleParser::read_object_or_array_with_default's noexcept boundary.
- P1-3: Replace global #ifndef RFL_TABLE_PARSER_MIN_FIELDS macro definition
  with internal constexpr kTableStructParserMinFields (with temporary TODO(#7)
  sweep hook).
- P1-4 & Rule #14: Add direct <concepts>, <new>, and fail_fast_v.hpp includes
  in TableStructParser.hpp, remove unused is_field.hpp in DirectFieldNames.hpp,
  and align parsing helpers to namespace rfl::parsing (zero rfl::internal::parsing
  or detail namespaces, avoiding shadowing rfl::internal across include/rfl/parsing/).
- P1-5: Launder &t on return from Parser_default::read_struct_with_default
  after const-subobject replacement.
- P1-6: Expand test_descriptor_table_stress.cpp with N == 64 bitmask boundary
  (Wide64Fields), Flatten<EmptyStruct>, const Flatten<InnerPlain>, "___" /
  "a__b" case transforms, AllowRawPtrs wrapper pointers, reference-member
  rejection, ExternalViewTypeReader, rfl::env, and NamedTuple/Field fallbacks.
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