Skip to content

Make schemas in the compiler as WebAssembly and the playground - #17

Merged
xadaemon merged 2 commits into
trunkfrom
playground-schema
Nov 8, 2026
Merged

xadaemon merged 2 commits into
trunkfrom
playground-schema

Conversation

@xadaemon

@xadaemon xadaemon commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Brings asnkitcc schema (#15) to the browser: the npm compiler gets schema(), and the playground gets a Schema panel.

schema(asni, options) in asnkit-compiler (npm)

  • Options:
    • format: json-schema (the default), xsd or dtd.
    • types: a name or an array of names, as Type or Module.Type. Every type if not given.
    • skipUnsupported: leave out what a schema can't describe instead of failing.
  • Returns { text, types, skipped: [{ type, reason }] }.
  • Errors: what a schema can't describe throws an Error named SchemaError; bad options throw a TypeError.
  • Typed in js/index.d.ts, and documented in the README and the skill reference.

Shared code: a library in asnkitcc

  • The schema code is now a library target of asnkitcc (src/lib.rs, which exposes schema), and the wasm crate depends on it.
  • schema::generate() is pure: it returns the text and the skipped types. main.rs now does the file and stdout writing, the warnings and --verbose, so the CLI output is unchanged.
  • Why not in asnkit-compiler? It would then depend on asnkitrt, and the release workflow's cargo publish --dry-run -p asnkit-compiler would fail before the runtime is published.
  • The library is documented as being for the crates of this repository, and may change in any release.
  • Cost: the compiler .wasm grows from 508 KB to 795 KB (196 KB to 285 KB gzipped), because it reads the types through the Rust runtime.

Playground

  • New panel, "4 Schema": the schema of the selected type or of every type, as a JSON Schema of JER, an XML Schema of XER or a DTD of XER.
  • It has a "leave out what a schema cannot describe" toggle, shows the size and any types left out, and has a download button.
  • It updates when you compile, switch type or change an option.

A clearer --skip-unsupported error

When a type given with -t refers to a type that is left out, the error now traces back to the cause. Before:

error: it refers to AlgorithmIdentifier, which is left out.

After:

error: Certificate refers to AlgorithmIdentifier, which a schema can not tell: AlgorithmIdentifier.parameters: an ANY or an open type, …

The playground's X.509 example shows this error by default.

Fix: an XML Schema of a CHOICE that is an item of itself

In XER, an item of a SEQUENCE OF or SET OF that is a CHOICE, BOOLEAN or ENUMERATED has no element of its own (X.693 9.3.6). The XSD writer copied that type's content into every place it was used as an item. Shape in tests/files/sketch.asn has group SET OF member Shape, so the copying never stopped, and the XML Schema of the Sketch-Pad Journal failed with "the types are nested too deeply". This was already the case on trunk.

  • When such an item's type has a name, the schema now writes its content once as <xs:group name="Shape">, and items refer to it with <xs:group ref="Shape" minOccurs=… maxOccurs=…/>. XSD keeps model groups apart from types, so the group can share the type's name.
  • Items whose type has no name are still copied in, as before.
  • Tests:
    • tests/schema.rs checks the XSD of a Node ::= CHOICE { leaf …, group SEQUENCE OF Node } tree.
    • tests/test_schema.py writes that tree as XER with asnkitcc convert and checks that lxml accepts it and rejects three wrong documents. It fails on the old binary.
  • The playground's Journal, which nests group inside group, encoded as XER, now validates against its XSD. All 207 XER vectors still validate.

Checked locally

  • cargo fmt, and clippy -D warnings on the workspace and on wasm32-unknown-unknown
  • cargo test -p asnkitcc, with the new tests in tests/schema.rs
  • the npm tests: 13, 4 of them new for schema()
  • tests/test_schema.py against the CLI build, with ruff and mypy
  • cargo doc and cargo package of asnkitcc
  • schema() on every playground example in all three formats, in Node. The page itself was not opened in a browser.

The compiler as WebAssembly has schema(asni, options), which makes a
JSON Schema of JER, or an XML Schema or a DTD of BASIC-XER, of the
types of an .asni file, as asnkitcc schema does. Its options are format
(json-schema, the default, xsd or dtd), types, a name or an array of
them, and skipUnsupported. It gives the text, how many types it is of,
and those it leaves out with why, and what it can not tell is an Error
named SchemaError.

The schemas are made by a library of asnkitcc, which both share:
schema::generate() makes the text with no file to write, and the
command writes it, with the warnings. They are not in asnkit-compiler,
which would then need the runtime, which is not published before it in
a release. The WebAssembly is larger by about 290 KB, 90 KB gzipped,
as it reads the types with the runtime.

The playground makes the schema of the selected type, or of every
type, as each of the three, to read and to download.

A type given with -t that refers to one --skip-unsupported leaves out
tells what it refers to, down to the type that can not be told, and
why, rather than only the first type it refers to.

Signed-off-by: Matheus Xavier <mxavier@boxtheta.com>
An item of a SEQUENCE OF or a SET OF that is a CHOICE, a BOOLEAN or an
ENUMERATED has no element of its own in XER (X.693 9.3.6), and the XML
Schema had what is in its type in the sequence of the item. A CHOICE
that is an item of itself, as Shape of tests/files/sketch.asn, of which
group is a SET OF Shape, was then in itself without end, and failed as
nested too deeply.

Of a type with a name, the item is now the model group of that name,
<xs:group ref="Shape"/>, which is what is in the type, as model groups
are named apart from types. One with no name is in the sequence as
before.

tests/schema.rs holds the XML Schema of a tree to its model group, and
tests/test_schema.py the XER of a tree that the runtime writes to it,
with lxml.

Signed-off-by: Matheus Xavier <mxavier@boxtheta.com>
@xadaemon
xadaemon merged commit 1f3a524 into trunk Nov 8, 2026
22 checks passed
@xadaemon
xadaemon deleted the playground-schema branch November 8, 2026 00:11
@xadaemon xadaemon mentioned this pull request Nov 8, 2026
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