Validation contract
validateDocument() checks that a canonical document is well-formed Greek: that
its tokens are structurally sound, and that each mark suits its letter and its
context. This contract explains why validation is a step of its own rather than
an option of conversion, what it checks, and at which stage of a pipeline it
applies.
Why validation is a separate step
Section titled “Why validation is a separate step”validateDocument() remains explicitly separate from parse(), encode(),
and convert(). Validation is not an encode or convert option.
The conversion pipeline is intentionally permissive:
- parsers preserve unknown input as literal tokens;
- the document model can represent unusual or invalid combinations so they can be inspected and, where possible, encoded deterministically;
- conversion returns a string and does not hide a second diagnostics or exception channel;
- validation can return multiple structured diagnostics instead of stopping at the first error.
Making validation implicit would require either throwing from otherwise permissive APIs or changing their return types. An option would also make it unclear whether the source document or the orthographically transformed target document is being validated.
Canonical-document check
Section titled “Canonical-document check”Applications that need to inspect the interpreted document before encoding should make the stages explicit:
import { encode, parse, validateDocument,} from "@humanities/greek-conversion/document";
const options = { preset: "tlg-core" } as const;const document = parse("A)/NQRWPOS", "beta-code", options);const diagnostics = validateDocument(document);
if (diagnostics.length > 0) { // Report diagnostics or reject the input.}
encode(document, "greek", options); // → ἄνθρωποςThis checks the structure and Greek graphemes of the parsed document. It does
not certify that the source string follows the syntax of its declared format.
For example, parse("*(", "beta-code") retains the incomplete uppercase
prefix as literal text, and validateDocument() returns no diagnostics for
that structurally valid document. Unknown input can likewise remain literal.
There is currently no separate source-syntax diagnostic API.
Diagnostics contain a stable code, a token index in the canonical Document,
and a human-readable message. The index is deliberately not a source-code-unit
offset: a parser can normalize combining sequences or map several source
characters to one grapheme. For a document read with parseDetailed(),
spans[diagnostic.index] is where that token was read in the input.
Validation is pure and does not mutate the document. It currently checks conflicting accents, breathings and quantities, plus whether accents, breathings, coronis, circumflex, diaeresis, iota subscript, and explicit quantity are valid for their grapheme and context. It also reports unknown letters or diacritics and glyph variants that do not fit their letter or case on manually modified graphemes. The document API contract explains ownership and copying of mutable tokens.
Malformed token kinds, non-text literals, non-boolean case flags, and non-Set
diacritics also receive diagnostics. Structural checks run over the whole
document first; if any fail, contextual and combinatorial checks are deferred
until those structural problems are corrected. This avoids interpreting the
neighbors of an ill-formed token as valid Greek context.
Diagnostic codes
Section titled “Diagnostic codes”The structural codes are reported first; the others only once the document has none of them.
| Code | Reported on |
|---|---|
invalid-token |
A token that is not an object, or whose kind is neither "grapheme" nor "literal" |
invalid-literal |
A literal whose value is not text |
invalid-case |
A grapheme whose uppercase is not a boolean |
invalid-diacritics |
A grapheme whose diacritics is not a Set |
invalid-letter |
A grapheme whose letter is not a known Greek letter |
invalid-diacritic |
A diacritic the model does not know |
invalid-glyph-variant |
An unknown glyphVariant, or one that does not fit its letter or its case |
conflicting-accents |
More than one accent on a grapheme |
conflicting-breathings |
Both breathings on a grapheme |
conflicting-coronis-breathing |
A coronis together with a breathing |
conflicting-quantities |
A macron together with a breve |
incompatible-diaeresis-breathing |
A diaeresis together with a breathing |
invalid-accent |
An accent on a consonant |
invalid-circumflex |
A circumflex on a vowel that cannot be long (epsilon, omicron) |
invalid-breathing |
A breathing on a consonant other than rho |
invalid-coronis |
A coronis on a consonant or on an initial vowel |
invalid-diaeresis |
A diaeresis on a letter other than iota or upsilon |
invalid-iota-subscript |
An iota subscript on a letter other than alpha, eta, or omega |
invalid-quantity |
A macron or a breve on a letter that cannot bear it |
The union is open: a minor release may add a code (see the stability policy).
Validation boundary
Section titled “Validation boundary”The normal validation boundary is the document immediately returned by
parse(). This answers whether its interpreted Greek structure is valid before
any requested lossy target policy is applied.
Callers that directly construct or transform a Document should validate the
exact document they intend to encode. Orthography helpers are immutable, so an
application may also validate their returned document when it needs to audit a
custom pipeline.
Any future source-syntax diagnostics would need source positions and a separate
contract from validateDocument()’s token indices. A convenience strict API,
if needed, should likewise define its return or error behavior explicitly
without making convert() mode-dependent.