Skip to content

Option for minimal types, with the validation in a separate file - #14

Merged
rmsrosa merged 1 commit into
mainfrom
ccr-0c004460-kd2lly
Nov 16, 2026
Merged

rmsrosa merged 1 commit into
mainfrom
ccr-0c004460-kd2lly

Conversation

@rmsrosa

@rmsrosa rmsrosa commented Nov 16, 2026

Copy link
Copy Markdown
Owner

This adds the option to generate minimal types: the same structs and constructors, without the runtime checks. The code for checking against the schema can go in a separate, optional file.

Changes

  • generate_module(...; validation = ...):
    • :inline (the default) works as before: the constructors check the constraints of the schema.
    • :separate gives minimal types. Their constructors only convert values to the types of their fields (via _jsfrom), with no _jscheck, and the enum constructors do not check their values. Everything needed to check goes in validation.jl (or <name>_validation.jl next to the module file, without split): the runtime validator, the full metadata of each type (constraints, embedded subschemas, regexes) as _vspecs, and the root constraints. The module includes it only if it is present:
      isfile(joinpath(@__DIR__, "validation.jl")) && include("validation.jl")
      With the file present, the module also has validate, violations, isvalid and check_formats!, and tojson/writejson validate before writing (unless given validate = false). Reading is not checked; validate(readjson(T, file)) does that. Without the file, the module only reads and writes.
    • :none gives the minimal types without the validation file.
  • The structure is still enforced by the types: the types of the properties, required properties, which properties are allowed, and the enumeration types. A JSON value read into a union of generated types gets the first option that fits by its properties and its enum values (as in Speed up oneOf/anyOf of generated types and object validation #12). That is enough for BeerJSON's unit-discriminated unions. Options that differ only by other constraints cannot be told apart without validation; this is documented.
  • The runtime is split into src/runtime.jl (reading and writing) and src/runtime_validation.jl (the checks). They are connected by a constant _INLINE_CHECKS, emitted by the generator, and a _VALIDATOR hook that the validation code sets. checks!/with_checks moved to the validation part, and are exported only with :inline.
  • checks = false only applies with validation = :inline; any other combination throws an ArgumentError.
  • Conformance harness: a new --validation=separate option generates minimal modules, then reads each instance and runs validate on it.
  • README, guide and API docs (a new "Minimal types" section), the generate_module docstring, tests, and a committed example: examples/generated/AcmeNestedMinimal.jl plus AcmeNestedMinimal_validation.jl.

Sizes (BeerJSON)

code
:inline 280 KiB
:none 221 KiB (most of it is the types and their docstrings)
:separate 221 KiB + 90 KiB of validation.jl

Checks

  • Pkg.test() passes on Julia 1.13 (487 tests) and 1.10 (485 tests). The new tests:
    • all BeerJSON samples read and write back unchanged with :separate (split and single file), and are valid;
    • invalid values are built without errors and caught by violations/validate, and writing them throws;
    • unions are still chosen correctly;
    • a module with its validation file deleted still loads and reads and writes, and tojson(...; validate = true) then throws an ArgumentError;
    • a :none module has no validation code.
  • JSON Schema Test Suite: 2175/2288 tests pass with the default mode, the same as the baseline. With --validation=separate, the same 2175 tests pass, with identical per-test results; only the wording of some error messages differs. CI runs the default mode only.
  • The docs build with no errors.

Generated by Claude Code

- `generate_module(...; validation = :separate)`: the constructors of
  the generated types only convert values to the types of their fields,
  and the checks of the constraints (the constraints of each type, the
  subschemas checked at runtime and the validator) go in a separate file,
  `validation.jl` (or `<name>_validation.jl`), that the module includes
  only if it is present. It provides `validate`, `violations`, `isvalid`
  and `check_formats!`, and makes `tojson`/`writejson` validate first.
- `validation = :none`: minimal types, without the validation file.
- `validation = :inline` (the default) is as before.
- The runtime is split into `runtime.jl` (reading and writing) and
  `runtime_validation.jl` (the checks).
- The conformance harness takes `--validation=separate`.
- Docs, README, tests and an example (`AcmeNestedMinimal`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rmsrosa
rmsrosa merged commit e23f21c into main Nov 16, 2026
4 checks passed
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.

2 participants